Amazon web services Terraform-具有多个环境(区域)的多个帐户
我正在开发我希望在AWS中使用Terraform的基础设施(IaC)。为了测试,我使用了一个EC2实例 此代码必须能够跨多个帐户部署,并且每个开发人员可以部署**多个区域(环境)*。这是一个例子:Amazon web services Terraform-具有多个环境(区域)的多个帐户,amazon-web-services,terraform,environment,terraform-provider-aws,infrastructure-as-code,Amazon Web Services,Terraform,Environment,Terraform Provider Aws,Infrastructure As Code,我正在开发我希望在AWS中使用Terraform的基础设施(IaC)。为了测试,我使用了一个EC2实例 此代码必须能够跨多个帐户部署,并且每个开发人员可以部署**多个区域(环境)*。这是一个例子: terraform workspace new account-999 terraform workspace new account-666 账户-999 developer1: us-east-2 developer2: us-west-1 developerN: us-east-1 账户-6
terraform workspace new account-999
terraform workspace new account-666
账户-999
developer1: us-east-2
developer2: us-west-1
developerN: us-east-1
账户-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"
}
}
以及变量.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
account的值。我怎样才能解决这个问题?
另一方面,使用这种结构,是否可以实现模块?您可以使用
提供程序别名来实现这一点。可以找到有关提供程序别名的更多信息
另一种方法是使用地形工作空间
。以下是一个例子:
terraform workspace new account-999
terraform workspace new account-666
这是aws凭据文件的一个示例:
[account-999]
aws_access_key_id=xxx
aws_secret_access_key=xxx
[account-666]
aws_access_key_id=xxx
aws_secret_access_key=xxx
对该帐户的引用可在提供程序块中使用:
provider "aws" {
region = "us-east-1"
profile = "${terraform.workspace}"
}
你甚至可以把这两种方法结合起来 使用模块和提供程序别名可以最好地解决区域问题,特别是当您使用0.13和模块的新元参数特性时。