我在将 terraform/terragrunt 部署到多个帐户时遇到问题。我想在我的“主”帐户中管理 DNS 和 KMS,并将其他所有内容部署到我的 dev/uat/prod 环境中。
我已经使用提供商配置了多个 AWS 账户。一个是通过 terragrunt 自动创建的,另一个是在我的 main.tf 文件中创建的。
我已经尝试了许多不同的方法来让它发挥作用。为这两个提供者设置别名,只为一个提供者设置别名,并从等式中完全删除 terragrunt。在每种情况下,terraform 都将应用于我的“主”帐户以获取所有资源。
下面是我的代码示例:
backend = "s3"
# generate = {
# path = "backend.tf"
# if_exists = "overwrite"
# }
config = {
bucket = "arm-terraform-state"
key = "${path_relative_to_include()}/terraform.tfstate"
region = "us-west-2"
dynamodb_table = "arm-terraform-state-lock"
}
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
}
generate "provider" {
path = "provider.tf"
if_exists = "overwrite_terragrunt"
contents = <<EOF
provider "aws" {
alias = "main"
region = "${local.aws_region}"
profile = "${local.account}"
}
EOF
}
locals {
region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl"))
account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))
aws_region = local.region_vars.locals.aws_region
account = local.account_vars.locals.aws_profile
}
以上是我的 Terragrunt 代码。这是我的模块:
resource "aws_iam_role_policy" "logging_role_policy" {
provider = aws.main
name = format("aws-sftp-logging-policy-%s-%s",var.product_name,var.env)
role= aws_iam_role.logging_role.id
policy = data.aws_iam_policy_document.sftp_logging.json
}
############
# Route 53 #
############
resource "aws_route53_record" "sftp_record" {
provider = aws.master
zone_id = data.aws_route53_zone.facteus.zone_id
name = format("%s-%s",var.product_name,var.env)
type = "CNAME"
ttl = "30"
records = [aws_transfer_server.aws_transfer_service.endpoint]
}