Skip to main content

Network examples

The network decides which address your Instance receives, where it can receive traffic from and whether it can be reached from the Internet. This page gives Terraform examples for Subnets, Security Groups and Floating IPs, with notes that help you avoid common errors.

Prerequisites​

Shared configuration​

# shared.tf
data "fptcloud_vpc" "example" {
name = "example-vpc"
}

data "fptcloud_instance" "app" {
vpc_id = data.fptcloud_vpc.example.id
name = "app-server-01"
}

Subnet​

A Subnet is a range of IP addresses in a VPC. An Instance receives its private IP from the Subnet that it is placed in. See also Subnet overview.

Subnet for applications​

A Subnet of type NAT_ROUTED lets Instances reach the Internet in the outbound direction through a NAT gateway. It suits web and API servers that need to download patches or call external services:

# subnet.tf
resource "fptcloud_subnet" "app" {
vpc_id = data.fptcloud_vpc.example.id
name = "app-subnet"
type = "NAT_ROUTED"
cidr = "10.10.1.0/24"
gateway_ip = "10.10.1.1"
}

Isolated Subnet for databases​

A Subnet of type ISOLATED has no route to the Internet. The Instances inside it only communicate internally within the VPC, which suits databases and caches:

# subnet.tf
resource "fptcloud_subnet" "db" {
vpc_id = data.fptcloud_vpc.example.id
name = "db-subnet"
type = "ISOLATED"
cidr = "10.10.2.0/24"
gateway_ip = "10.10.2.1"
}

Subnet with a static IP range and its own DNS​

Use static_ip_pool to reserve an address range for static allocation, and primary_dns_ip and secondary_dns_ip to specify the DNS servers:

# subnet.tf
resource "fptcloud_subnet" "internal" {
vpc_id = data.fptcloud_vpc.example.id
name = "internal-subnet"
type = "NAT_ROUTED"
cidr = "10.10.3.0/24"
gateway_ip = "10.10.3.1"
static_ip_pool = "10.10.3.100-10.10.3.200"
primary_dns_ip = "10.10.0.10"
secondary_dns_ip = "10.10.0.11"
}

Write static_ip_pool in the form <first address>-<last address>, without spaces, and keep it inside cidr.

Several Subnets from one address range​

The cidrsubnet() function splits a large range into /24 ranges, so you do not have to calculate addresses by hand:

# subnets.tf
locals {
base_cidr = "10.20.0.0/16"
tiers = { web = 1, app = 2, db = 3 }
}

resource "fptcloud_subnet" "tier" {
for_each = local.tiers

vpc_id = data.fptcloud_vpc.example.id
name = "${each.key}-subnet"
type = each.key == "db" ? "ISOLATED" : "NAT_ROUTED"
cidr = cidrsubnet(local.base_cidr, 8, each.value)
gateway_ip = cidrhost(cidrsubnet(local.base_cidr, 8, each.value), 1)
}

This configuration creates web-subnet (10.20.1.0/24), app-subnet (10.20.2.0/24) and db-subnet (10.20.3.0/24).

warning

The CIDR range of a Subnet must not overlap with another Subnet in the VPC, and gateway_ip must be inside the CIDR range. Changing name, type, cidr, gateway_ip or static_ip_pool recreates the Subnet, which also recreates every Instance in that Subnet.

Security Group​

A Security Group is a virtual firewall that controls the inbound and outbound traffic of an Instance. Traffic that does not match any rule is denied. See also Security Group overview.

The type argument is required when you create a Security Group. Set type = "ACL".

Security Group for web servers​

The example below allows HTTP and HTTPS from anywhere, and SSH only from the office network. The Security Group is attached to Instances through the apply_to argument, which takes a list of the private IPs of the Instances to protect:

# security.tf
resource "fptcloud_security_group" "web" {
vpc_id = data.fptcloud_vpc.example.id
name = "web-sg"
subnet_id = fptcloud_subnet.app.id
type = "ACL"
apply_to = [data.fptcloud_instance.app.private_ip]
}
# security.tf
locals {
web_rules = {
http = { port = "80", source = "ALL" }
https = { port = "443", source = "ALL" }
ssh = { port = "22", source = "203.0.113.0/24" } # Your office network
}
}
# security.tf
resource "fptcloud_security_group_rule" "web" {
for_each = local.web_rules

vpc_id = data.fptcloud_vpc.example.id
security_group_id = fptcloud_security_group.web.id
direction = "INGRESS"
action = "ALLOW"
protocol = "TCP"
port_range = each.value.port
sources = [each.value.source]
description = "Allow ${each.key}"
}

Using a map with for_each lets you add or remove rules without affecting the other rules.

Security Group for databases​

Allow only the application servers to connect to PostgreSQL (port 5432), with the CIDR range of the application Subnet as the source:

# security.tf
data "fptcloud_instance" "db" {
vpc_id = data.fptcloud_vpc.example.id
name = "db-server-01"
}

resource "fptcloud_security_group" "db" {
vpc_id = data.fptcloud_vpc.example.id
name = "db-sg"
subnet_id = fptcloud_subnet.db.id
type = "ACL"
apply_to = [data.fptcloud_instance.db.private_ip]
}
# security.tf
resource "fptcloud_security_group_rule" "postgres" {
vpc_id = data.fptcloud_vpc.example.id
security_group_id = fptcloud_security_group.db.id
direction = "INGRESS"
action = "ALLOW"
protocol = "TCP"
port_range = "5432"
sources = [fptcloud_subnet.app.cidr]
description = "PostgreSQL from the app subnet"
}

One Security Group for several Instances​

Put the private IPs of all the Instances to protect in apply_to. The example below collects the IPs of every Instance whose name starts with web-server-. The list is read again each time you run terraform apply, so a new Instance is added to the Security Group on the next apply:

# security.tf
data "fptcloud_instances" "all" {
vpc_id = data.fptcloud_vpc.example.id
}

locals {
web_ips = [
for vm in data.fptcloud_instances.all.instances : vm.private_ip
if startswith(vm.name, "web-server-")
]
}

Then set apply_to = local.web_ips on the Security Group.

Rule values​

ArgumentValue
directionINGRESS for inbound traffic, EGRESS for outbound traffic.
actionAlways ALLOW. Security Groups do not support deny rules, and traffic that matches no rule is already denied.
protocolTCP, UDP, ICMP or ALL.
port_rangeOne port ("22"), a port range ("8000-8100") or "ALL". It must be "ALL" when protocol = "ALL".
sourcesOne IPv4 CIDR range, one IPv4 address or "ALL". For an EGRESS rule, this is the destination address.

Give each rule one source and one port or port range. To open a port to several sources, or several ports to one source, create one rule for each with for_each, as in the web server example above. If you put several values in sources, FPT Cloud splits them into separate rules, and terraform destroy may not delete all of them.

warning

Rules cannot be edited in place. Changing any argument of a rule, including description, deletes the old rule and then creates a new one. During the short time between the two steps, the traffic of that rule may be blocked.

Attach a Security Group with apply_to​

Attach a Security Group with the apply_to argument of the Security Group, not with the security_group_ids argument of the Instance. security_group_ids only takes effect when the Instance is created, and FPT Cloud also reports the Security Groups attached through apply_to, so the plan always shows a change.

A newly created Instance may not report its private IP for a short time. Read the IP through the fptcloud_instance data source as in the examples above. If apply_to is empty after the first apply, run terraform apply again.

Delete a Security Group​

FPT Cloud can only delete a Security Group when it is no longer attached to any resource. Before you delete a Security Group together with its Instances:

  1. Set apply_to = [] on the Security Group.
  2. Run terraform apply.
  3. Run terraform destroy.

Build a complete web application infrastructure shows how to use a variable to do this step more simply.

Floating IP​

A Floating IP is a public IP address that you allocate separately and then assign to an Instance. Because the IP is not tied to the Instance, you can move the IP to another Instance without changing the DNS configuration. See also Floating IP overview.

Allocate and assign a Floating IP​

# public-ip.tf
resource "fptcloud_floating_ip" "app" {
vpc_id = data.fptcloud_vpc.example.id
}

resource "fptcloud_floating_ip_association" "app" {
vpc_id = data.fptcloud_vpc.example.id
floating_ip_id = fptcloud_floating_ip.app.id
instance_id = data.fptcloud_instance.app.id
}

output "public_ip" {
value = fptcloud_floating_ip.app.ip_address
}

fptcloud_floating_ip holds the IP address, and fptcloud_floating_ip_association assigns that address to the Instance. Splitting them into two resources keeps the IP address unchanged when you move it to another Instance.

Move a Floating IP to another Instance​

Change the instance_id of the existing fptcloud_floating_ip_association, then run terraform apply. Terraform removes the IP from the old Instance and assigns it to the new Instance, and the IP address stays the same:

data "fptcloud_instance" "standby" {
vpc_id = data.fptcloud_vpc.example.id
name = "app-server-02"
}

resource "fptcloud_floating_ip_association" "app" {
vpc_id = data.fptcloud_vpc.example.id
floating_ip_id = fptcloud_floating_ip.app.id
instance_id = data.fptcloud_instance.standby.id # previously data.fptcloud_instance.app.id
}

Each Floating IP should have only one fptcloud_floating_ip_association. When you move the IP, edit the existing resource instead of creating a new one.

tip

Assign a Floating IP with fptcloud_floating_ip_association instead of the public_ip argument of the Instance. public_ip only takes effect when the Instance is created, so you cannot use it to move or remove the IP later.

Next steps​