Cross-Environment Deployment
Prerequisites
- AWS account with appropriate permissions
- Terraform >= 1.0
- Terraform workspaces configured for each environment
Step 1: Create Environment Configuration
Define environment-specific variables:
# environments/dev.tfvars
environment = "dev"
region_code = "use1"
# environments/stg.tfvars
environment = "stg"
region_code = "use1"
# environments/prod.tfvars
environment = "prod"
region_code = "use1"
Step 2: Configure the Naming Module
variable "environment" {
type = string
description = "Environment name"
}
variable "region_code" {
type = string
description = "Region code"
}
module "naming" {
source = "registry.patterneddesigns.ca/standardnat/naming-convention/aws"
version = "2.0.0"
environment = var.environment
project = "myapp"
region_code = var.region_code
}
Step 3: Define Resources
resource "aws_s3_bucket" "data" {
bucket = "${module.naming.prefix}-data"
tags = module.naming.tags
}
resource "aws_sqs_queue" "main" {
name = "${module.naming.prefix}-main"
tags = module.naming.tags
}
Step 4: Deploy to Each Environment
# Deploy to development
terraform workspace select dev
terraform apply -var-file=environments/dev.tfvars
# Deploy to staging
terraform workspace select stg
terraform apply -var-file=environments/stg.tfvars
# Deploy to production
terraform workspace select prod
terraform apply -var-file=environments/prod.tfvars
Expected Results
Each environment will have consistently named resources:
| Environment | S3 Bucket | SQS Queue |
|---|---|---|
| dev | dev-myapp-use1-data | dev-myapp-use1-main |
| stg | stg-myapp-use1-data | stg-myapp-use1-main |
| prod | prod-myapp-use1-data | prod-myapp-use1-main |