简体   繁体   中英

add metadata to S3 object in terraform

I am trying to add metadata during the upload of a new file based on an existing file in S3. The code I am using is the following:

provider "aws" {
  region  = "xxx"
  profile = "xxx"
}

data "aws_s3_bucket_object" "index_cdn" {
  bucket = "bucket name"
  key    = "index.html"
}

resource "aws_s3_bucket_object" "index" {
  bucket       = "bucket name"
  key          = "index_new.html"
  source       = "${path.module}/index.html"
  content_type = "text/html"
  
  metadata     = lower(data.aws_s3_bucket_object.index_cdn.metadata)
}

output "metadata" {
  value = data.aws_s3_bucket_object.index_cdn.metadata
}

It fails with the following error message.

Error: Incorrect attribute value type

  on main.tf line xx, in resource "aws_s3_bucket_object" "index":
  xx:   metadata     = lower(data.aws_s3_bucket_object.index_cdn.metadata)

Inappropriate value for attribute "metadata": map of string required.

When I run the code without the code block resource "aws_s3_bucket_object" "index" than the output is the following:

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

Changes to Outputs:
  + metadata = {
      + "Codebuild-Buildarn"       = "arn:aws:codebuild:xxxxx"
      + "Codebuild-Content-Md5"    = "716d3e5bc7c972f89033aad7dd6c9a9f"
      + "Codebuild-Content-Sha256" = "d015e0a093938b21135c2ba5abc23278d4c5961d7e18aa8e3b9a748cc09e6bc7"
    }

Any idea how to resolve it? Thanks for the help.

应该是, keys应该是小写的:

 metadata     = {for k, v in data.aws_s3_bucket_object.index_cdn.metadata: lower(k) => v}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM