amazon-web-services - 使用依赖存储桶名称的模板设置 S3 存储桶策略时如何避免循环错误?

标签 amazon-web-services terraform terraform-provider-aws terraform-template-file

我有一个 terraform运行时失败的文件 terraform plan我得到错误:

Error: Cycle: module.hosting.data.template_file.bucket_policy, module.hosting.aws_s3_bucket.website

这是有道理的,因为存储桶指的是策略,反之亦然:
data "template_file" "bucket_policy" {
  template = file("${path.module}/policy.json")
  vars = {
    bucket = aws_s3_bucket.website.arn
  }
}

resource "aws_s3_bucket" "website" {
  bucket = "xxx-website"

  website {
    index_document = "index.html"
  }

  policy = data.template_file.bucket_policy.rendered
}

如何避免这种双向引用?

最佳答案

您可以使用 aws_s3_bucket_policy 资源。这允许您创建没有循环依赖关系的资源。

这样,Terraform 可以:

  • 创建存储桶
  • 使用存储桶 ARN
  • 创建模板文件
  • 创建策略,引用模板文件并将其附加到存储桶。

  • 代码看起来像这样:
    data "template_file" "bucket_policy" {
      template = file("${path.module}/policy.json")
      vars = {
        bucket = aws_s3_bucket.website.arn
      }
    }
    
    resource "aws_s3_bucket" "website" {
      bucket = "xxx-website"
    
      website {
        index_document = "index.html"
      }
    }
    
    resource "aws_s3_bucket_policy" "b" {
      bucket = "${aws_s3_bucket.website.id}"
    
      policy = data.template_file.bucket_policy.rendered
    }
    

    关于amazon-web-services - 使用依赖存储桶名称的模板设置 S3 存储桶策略时如何避免循环错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61869536/

    相关文章:

    javascript - AWS Dynamodb 配置中缺少凭证,如果使用 AWS_CONFIG_FILE,则设置 AWS_SDK_LOAD_CONFIG=1

    amazon-web-services - AWS Hadoop MapReduce-平均字数

    Terraform 提供者/模块中的变量共享

    Terraform 创建 Lambda 函数时出错 : ResourceConflictException with the resource just created by Terraform apply

    amazon-web-services - 创建堆栈时的 AWS CloudFormation 模板验证

    terraform - 我想识别terraform执行环境的公网ip并加入安全组

    Terraform 文件夹结构

    Terraform 资源重建动态 AWS RDS 实例计数

    amazon-web-services - terraform错误: unsupported argument in module in tf plan

    java - 无法解析符号 DynamoDBMapper