Terraform 总是说对 s3 存储桶策略的模板文件进行更改

Terraform always says changes on templatefile for s3 bucket policy

我的 s3 存储桶策略有问题,它似乎正确添加了策略,甚至在 AWS 中对其进行了验证,它显示了 policy.tpl 中设置的确切策略,但它一直说有变化

我试过将操作和资源更改为数组,我听说这可能有帮助.. 尝试从策略中删除“版本”,SID,一直说每次我 运行它

policy.tf

resource "aws_s3_bucket_policy" "bucket" {
  bucket = aws_s3_bucket.bucket.id
  policy = local.policy
}

locals.tf

locals {
  template_dir       = "${path.module}/templates"
  template_vars      = {
    encrypt          = var.s3_require_encryption_enabled
    bucket_arn       = aws_s3_bucket.bucket.arn
    extra_statements = var.s3_bucket_policy
  }
  policy             = templatefile("${local.template_dir}/policy.tpl", local.template_vars)
}

templates/policy.tpl

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid" : "",
            "Effect" : "Deny",
            "Principal" : "*",
            "Action" : "s3:*",
            "Resource" : "${bucket_arn}/*",
            "Condition": {
                "Bool": {
                    "aws:SecureTransport": "false"
                }
            }        
        }
    ]
  }

在 AWS 中

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::test-bucket-us-east-1/*",
            "Condition": {
                "Bool": {
                    "aws:SecureTransport": "false"
                }
            }
        }
    ]
}

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  #aws_s3_bucket_policy.bucket will be updated in-place
  ~ resource "aws_s3_bucket_policy" "bucket" {
        bucket = "test-bucket-us-east-1"
        id     = "test-bucket-us-east-1"
      + policy = jsonencode(
            {
              + Statement = [
                  + {
                      + Action    = "s3:*"
                      + Condition = {
                          + Bool = {
                              + aws:SecureTransport = "false"
                            }
                        }
                      + Effect    = "Deny"
                      + Principal = "*"
                      + Resource  = "arn:aws:s3:::test-bucket-us-east-1/*"
                      + Sid       = ""
                    },
                ]
              + Version   = "2008-10-17"
            }
        )
    }

Plan: 0 to add, 1 to change, 0 to destroy.

根据评论,基础存储桶策略存在问题。

PutBucketPolicy

Content-MD5 The MD5 hash of the request body.

For requests made using the AWS Command Line Interface (CLI) or AWS SDKs, this field is calculated automatically.)

所以资源 aws_s3_bucket_policy 正在尝试更新策略。