问题描述
我正在尝试使用terraform aws vpc模块创建aws vpc-应该根据输入变量在不同的区域中创建它。
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"]```
这是 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])
}
然后我想将其应用于其他变量,例如:
terraform apply -var="region=us-west-2"
或
terraform apply -var="region=eu-west-2"
它应该在不同区域创建vpc。因此,变量中的“ us-west-2a”,“ us-west-2b”应替换为“ eu-west-2a”,“ eu-west-2b”。这可能吗?
我正试图通过这种方式做到这一点:
data "aws_availability_zones" "available" {
state = "available"
}
- 在 variable.tf 中将vpc_azs变量更改为:
vpc_azs = "${data.aws_availability_zones.available.names}"
但是它不起作用-它在一个区域中添加资源,并且在添加到另一区域中时,尝试替换路由表而不是创建新的路由表。
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
}
那么也许可以通过其他方式完成?
解决方法
没有意识到我需要用于不同区域的不同工作区来保存状态,然后关闭。
app-dev-us = {
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"]
}
app-dev-eu = {
env_name = "dev"
vpc_name = "app-vpc-dev"
vpc_cidr = "10.13.0.0/16"
vpc_azs = ["eu-central-1a","eu-central-1b"]
vpc_private_subnets = ["10.13.60.0/24","10.13.61.0/24"]
vpc_public_subnets = ["10.13.160.0/24","10.13.161.0/24"]
}