gpt4 book ai didi

amazon-s3 - 在 Terraform 中将多个 AWS 账户作为环境处理的最佳方式是什么?

转载 作者:行者123 更新时间:2023-12-01 09:44:50 29 4
gpt4 key购买 nike

我们希望将我们的每个 terraform 环境都放在一个单独的 AWS 账户中,以防止意外部署到生产环境。这如何最好地完成?

最佳答案

我们假设一个帐户专用于生产,另一个帐户专用于 PreProduction,并且可能其他沙盒环境也有唯一的帐户,可能是在每个管理员的基础上。另一个假设是,您在每个 AWS 账户中都有一个特定于您的环境的 S3 存储桶。此外,我们希望您的 AWS 账户凭证在 ~/.aws/credentials 中进行管理(或者可能使用 IAM 角色)。

Terraform 后端配置

有两种状态。对于主要状态,我们使用 Partial Configuration 的概念。 .我们不能通过模块或其他方式将变量传递到后端配置中,因为它是在确定之前读取的。

Terraform 配置设置

这意味着我们声明后端缺少一些细节,然后将它们作为参数提供给 terraform init .一旦初始化,它就会设置到 .terraform目录被删除。

terraform {
backend "s3" {
encrypt = true
key = "name/function/terraform.tfstate"
}
}

工作流程注意事项

我们只需要改变我们的初始化方式。我们使用 -backend-config关于 terraform init 的论点.这提供了配置的缺失部分。我通过我的 ~/.bash_profile 中的 bash 别名提供了所有缺失的部分像这样。
alias terrainit='terraform init \
-backend-config "bucket=s3-state-bucket-name" \
-backend-config "dynamodb_table=table-name" \
-backend-config "region=region-name"'

意外错误配置结果

如果适当需要 -backend-config参数被省略,初始化将提示您输入它们。如果提供不正确,很可能会因为权限原因导致失败。此外,必须将远程状态配置为匹配,否则它也会失败。为了部署到生产环境,必须在识别适当的帐户环境时出现多个错误。

Terraform 远程状态

下一个问题是远程状态也需要改变,不能通过从后端配置拉取配置;但是,可以通过变量设置远程状态。

模块设置

为了方便切换帐户,我们设置了一个非常简单的模块,它接收单个变量 aws-account并返回一堆远程状态可以使用适当值的输出。我们还可以包含其他特定于环境/帐户的内容。该模块是一个简单的 main.tf使用键为 aws-account 的映射变量以及特定于该帐户的值。然后我们有一堆输出,可以像这样简单地查找 map 变量。
variable "aws-region" {
description = "aws region for the environment"
type = "map"
default = {
Production = "us-west-2"
PP = "us-east-2"
}
}

output "aws-region" {
description = “The aws region for the account
value = "${lookup(var.aws-region, var.aws-account, "invalid AWS account specified")}"
}

Terraform 配置设置

首先,我们必须将 aws-account 传递给模块。这可能会接近 main.tf 的顶部。 .
module "environment" {
source = "./aws-account"
aws-account = "${var.aws-account}"
}

然后将变量声明添加到您的 variables.tf .
variable "aws-account" {
description = "The environment name used to identify appropriate AWS account resources used to configure remote states. Pre-Production should be identified by the string PP. Production should be identified by the string Production. Other values may be added for other accounts later."
}

现在我们已经从模块输出了帐户特定的变量,它们可以像这样在远程状态声明中使用。
data "terraform_remote_state" "vpc" {
backend = "s3"
config {
key = "name/vpc/terraform.tfstate"
region = "${module.environment.aws-region}"
bucket = "${module.environment.s3-state-bucket-name}"
}
}

工作流程考虑

如果在这样设置后工作流没有任何变化,将提示用户提供 aws-account 的值每当执行计划/应用等时,通过这样的提示进行变量。提示内容为 variables.tf中变量的描述.
$ terraform plan
var.aws-account
The environment name used to identify appropriate AWS account
resources used to configure remote states. Pre-Production should be
identified by the string PP. Production should be identified by the
string Production. Other values may be added for other accounts later.

Enter a value:

您可以通过在命令行上提供变量来跳过提示,如下所示
terraform plan -var="aws-account=PP"

意外错误配置结果

如果 aws-account未指定变量,它将被请求。如果提供了 aws-account 模块不知道的无效值,它将多次返回错误,包括字符串“指定的无效 AWS 账户”,因为这是查找的默认值。如果正确传递了 aws-account,但它与 terraform init 中标识的值不匹配,它将失败,因为正在使用的 aws 凭证将无权访问正在标识的 S3 存储桶。

关于amazon-s3 - 在 Terraform 中将多个 AWS 账户作为环境处理的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51451062/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com