【发布时间】:2021-01-15 21:59:26
【问题描述】:
我正在使用 Terraform 开发我希望在 AWS 中拥有的基础架构 (IaC)。为了测试,我使用的是 EC2 实例。
此代码必须能够部署跨多个帐户和**每个开发者的多个区域(环境)**。这是一个例子:
account-999
developer1: us-east-2
developer2: us-west-1
developerN: us-east-1
account-666:
Staging: us-east-1
Production: eu-west-2
我创建了两个 .tfvars 变量,account-999.env.tfvars 和 account-666.env.tfvars,内容如下:
-
profile="account-999"和profile="account-666"分别
这是我的 main.tf,其中包含带有 EC2 实例的 aws 提供程序:
provider "aws" {
version = "~> 2.0"
region = "us-east-1"
profile = var.profile
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"]
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}
还有variable.tf 文件:
variable "profile" {
type=string
}
variable "region" {
description = "Region by developer"
type = map
default = {
developer1 = "us-west-2"
developer2 = "us-east-2"
developerN = "ap-southeast-1"
}
}
但我不确定我是否管理得很好。例如,region 变量仅包含 account-999 帐户的值。我该如何解决?
另一方面,有了这种结构,是否可以实现模块?
【问题讨论】:
-
使用模块和提供者别名可以最好地解决区域问题,特别是如果您使用 0.13 和新的模块元参数功能。
标签: amazon-web-services terraform environment terraform-provider-aws infrastructure-as-code