I'm trying to create an aws vpc using terraform aws vpc module - and it should be created in different region depending on input variable.
Here is what I got in main.tf file:
provider "aws" {
region = var.region
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = local.workspace["vpc_name"]
cidr = local.workspace["vpc_cidr"]
azs = local.workspace["vpc_azs"]
private_subnets = local.workspace["vpc_private_subnets"]
public_subnets = local.workspace["vpc_public_subnets"]```
And here is what I have in variables.tf:
variable "region" {}
...
locals {
env = {
default = {}
...
app-dev = {
env_name = "dev"
vpc_name = "app-vpc-dev"
vpc_cidr = "10.12.0.0/16"
vpc_azs = ["us-west-2a", "us-west-2b"]
vpc_private_subnets = ["10.12.60.0/24", "10.12.61.0/24"]
vpc_public_subnets = ["10.12.160.0/24", "10.12.161.0/24"]
}
}
environmentvars = contains(keys(local.env), terraform.workspace) ? terraform.workspace : "default"
workspace = merge(local.env["default"], local.env[local.environmentvars])
}
And then I want to apply it with different variable,like:
terraform apply -var="region=us-west-2"
or
terraform apply -var="region=eu-west-2"
And it should create vpc in different region. So "us-west-2a", "us-west-2b" in variables should be substituted with "eu-west-2a", "eu-west-2b". Is this possible?
I was trying to do it this way:
- Add data aws_availability_zones to main.tf:
data "aws_availability_zones" "available" {
state = "available"
}
- change vpc_azs variable in variable.tf with:
vpc_azs = "${data.aws_availability_zones.available.names}"
But it doesn't work - it adds resources in one region and when adding to another one, tries to replace route tables instead of creating new one.
Plan: 24 to add, 0 to change, 4 to destroy.
# module.vpc.aws_route_table_association.private[0] must be replaced
-/+ resource "aws_route_table_association" "private" {
~ id = "rtbassoc-035aef7e37dad1ac1" -> (known after apply)
~ route_table_id = "rtb-026bfeee8c7e042a6" -> (known after apply)
~ subnet_id = "subnet-070f46911b6ac5d20" -> (known after apply) # forces replacement
}
# module.vpc.aws_route_table_association.private[1] must be replaced
-/+ resource "aws_route_table_association" "private" {
~ id = "rtbassoc-079f04e405eca2ed3" -> (known after apply)
~ route_table_id = "rtb-0aabe71401689c1be" -> (known after apply)
~ subnet_id = "subnet-03fe27c23fd3178f3" -> (known after apply) # forces replacement
}
# module.vpc.aws_route_table_association.public[0] must be replaced
-/+ resource "aws_route_table_association" "public" {
~ id = "rtbassoc-02af1c10bea83bdd2" -> (known after apply)
~ route_table_id = "rtb-000cfffca8956073c" -> (known after apply)
~ subnet_id = "subnet-0951342807d06a0f3" -> (known after apply) # forces replacement
}
# module.vpc.aws_route_table_association.public[1] must be replaced
-/+ resource "aws_route_table_association" "public" {
~ id = "rtbassoc-051ff11315ef42d31" -> (known after apply)
~ route_table_id = "rtb-000cfffca8956073c" -> (known after apply)
~ subnet_id = "subnet-0da8a20c3f25fcc44" -> (known after apply) # forces replacement
}
So maybe this can be done in some other way?