Skip to main content

Organize configuration for several environments

When a project has both a staging and a production environment, copying the configuration for each environment easily leads to differences and is hard to maintain. A Terraform module lets you write the configuration once, then reuse it for every environment with different sizes. Each environment has its own folder and state, so a change in staging never affects production.

Prerequisites​

Folder structure​

infra/
├── modules/
│ └── web-stack/ # shared module
│ ├── versions.tf
│ ├── variables.tf
│ ├── main.tf
│ └── outputs.tf
└── environments/
├── staging/ # one folder and one state per environment
│ └── main.tf
└── production/
└── main.tf
FolderContent
modules/web-stackThe Subnet, Instances and Security Group of one environment. It contains no values specific to any environment.
environments/<name>Calls the module with the values of that environment. You run terraform in this folder.

Write the module​

The provider that the module uses. Without this block, Terraform looks for the wrong provider hashicorp/fptcloud and returns an error at terraform init:

# modules/web-stack/versions.tf
terraform {
required_providers {
fptcloud = {
source = "fpt-corp/fptcloud"
}
}
}

The input variables of the module:

# modules/web-stack/variables.tf
variable "vpc_id" { type = string }
variable "name_prefix" { type = string }
variable "cidr" { type = string }
variable "image_name" { type = string }
variable "storage_policy_id" { type = string }
variable "ssh_public_key" { type = string }
variable "office_cidr" { type = string }
# modules/web-stack/variables.tf
variable "instance_count" {
type = number
default = 1
}

variable "cpu" {
type = number
default = 2
}

variable "memory_mb" {
type = number
default = 4096
}

variable "attach_security_groups" {
type = bool
default = true
}

The resources of the module. The flavor is selected by cpu and memory_mb, so each environment only needs to declare its size:

# modules/web-stack/main.tf
data "fptcloud_flavor" "this" {
vpc_id = var.vpc_id

filter {
key = "cpu"
values = [tostring(var.cpu)]
}
filter {
key = "memory_mb"
values = [tostring(var.memory_mb)]
}
}
# modules/web-stack/main.tf
resource "fptcloud_subnet" "this" {
vpc_id = var.vpc_id
name = "${var.name_prefix}-subnet"
type = "NAT_ROUTED"
cidr = var.cidr
gateway_ip = cidrhost(var.cidr, 1)
}
# modules/web-stack/main.tf
resource "fptcloud_instance" "this" {
count = var.instance_count

vpc_id = var.vpc_id
name = format("%s-web-%02d", var.name_prefix, count.index + 1)
image_name = var.image_name
flavor_name = data.fptcloud_flavor.this.flavors[0].name
subnet_id = fptcloud_subnet.this.id
storage_size_gb = 40
storage_policy_id = var.storage_policy_id
ssh_key = var.ssh_public_key
status = "POWERED_ON"
}
# modules/web-stack/main.tf
data "fptcloud_instance" "this" {
count = var.instance_count

vpc_id = var.vpc_id
id = fptcloud_instance.this[count.index].id
}
# modules/web-stack/main.tf
resource "fptcloud_security_group" "this" {
vpc_id = var.vpc_id
name = "${var.name_prefix}-web-sg"
subnet_id = fptcloud_subnet.this.id
type = "ACL"
apply_to = var.attach_security_groups ? data.fptcloud_instance.this[*].private_ip : []
}
# modules/web-stack/main.tf
locals {
rules = {
http = { port = "80", source = "ALL" }
https = { port = "443", source = "ALL" }
ssh = { port = "22", source = var.office_cidr }
}
}
# modules/web-stack/main.tf
resource "fptcloud_security_group_rule" "this" {
for_each = local.rules

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

The outputs of the module:

# modules/web-stack/outputs.tf
output "subnet_id" {
value = fptcloud_subnet.this.id
}

output "private_ips" {
value = data.fptcloud_instance.this[*].private_ip
}

Call the module from each environment​

The staging environment uses one small Instance:

# environments/staging/main.tf
terraform {
required_providers {
fptcloud = {
source = "fpt-corp/fptcloud"
version = "~> 0.3"
}
}
}

provider "fptcloud" {}

data "fptcloud_vpc" "this" {
name = "example-vpc-staging"
}
# environments/staging/main.tf
data "fptcloud_image" "ubuntu" {
vpc_id = data.fptcloud_vpc.this.id

filter {
key = "name"
values = ["UBUNTU-22.04"]
match_by = "substring"
}
}

data "fptcloud_storage_policy" "ssd" {
vpc_id = data.fptcloud_vpc.this.id

filter {
key = "name"
values = ["Premium-SSD"]
}
}
# environments/staging/main.tf
module "web" {
source = "../../modules/web-stack"

vpc_id = data.fptcloud_vpc.this.id
name_prefix = "shop-staging"
cidr = "10.60.1.0/24"
image_name = data.fptcloud_image.ubuntu.images[0].name
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
ssh_public_key = file(pathexpand("~/.ssh/id_rsa.pub"))
office_cidr = "203.0.113.0/24"
instance_count = 1
}

output "web_private_ips" {
value = module.web.private_ips
}

The production environment uses the same module with different values only. For example, production has 3 Instances, each with 4 vCPU and 8 GB of RAM:

module "web" {
source = "../../modules/web-stack"

# ... same arguments as staging, with the values changed for production
name_prefix = "shop-production"
cidr = "10.70.1.0/24"
instance_count = 3
cpu = 4
memory_mb = 8192
}

The rest of environments/production/main.tf is the same as staging, with the VPC name of production.

Deploy each environment​

Run Terraform in the folder of the environment that you want to change:

cd environments/staging
terraform init
terraform plan
terraform apply

Each folder has its own state file, so terraform destroy in staging does not touch production.

tip

Apply a change to staging first, check it, and only then apply it to production. Because the two environments share the same module, a change that works well in staging gives a similar result in production.

Store the state in a shared location​

By default, the state is in the terraform.tfstate file on your machine. When several people manage the infrastructure together, store the state in a shared backend that supports locking and encryption, so that everyone works on the same state. See the backend types in the Terraform backend documentation.

warning

Do not commit the state file to Git. The state can contain sensitive information, and two people editing the same state file at once can easily corrupt it.

Delete an environment​

Detach the Security Group first, then delete the environment, as described in Build a complete web application infrastructure. To pass a value to the module variable, add attach_security_groups = var.attach_security_groups to the module block and declare the attach_security_groups variable in the environment folder.

Next steps​