Home > Net >  Terraform For Publishing AWS Lambda Using S3 Metadata
Terraform For Publishing AWS Lambda Using S3 Metadata

Time:12-30

I have been having some trouble trying to reference a user defined metadata field on a zip file on Amazon S3. I am trying to use this metadata field to help Terraform detect whether the zip file has changed so that I can publish a new version of a Lambda Function.

These are the metadata fields on S3:

System defined  Content-Type    application/zip
User defined    x-amz-meta-sha256   241461f66ba6bec6418679888f9c8b5210c1f183aabfe86d1cc2ab71cdffdcfc

This is what I have for the Terraform config:

data "aws_s3_bucket_object" "lambda" {
  bucket = "bucket"
  key    = "lambda/lambda.zip"
}

resource "aws_lambda_function" "lambda" {
  function_name = "lambda_name"
  s3_bucket = "bucket"
  s3_key = "lambda/lambda.zip"
  source_code_hash = "${data.aws_s3_bucket_object.lambda.metadata.x-amz-meta-sha256}"
  handler = "index.handler"
  runtime = "nodejs14.x"
  publish = true
  role = aws_iam_role.lambda_exec.arn
  vpc_config {
    subnet_ids = "subnet_ids"
    security_group_ids = "security_group_ids"
  }
}

I have been getting this error when I try to run 'terraform plan':

Error: Missing map element

  on ../../../modules/lambda/main.tf line 14, in resource "aws_lambda_function" "lambda":
  14:   source_code_hash = "${data.aws_s3_bucket_object.lambda.metadata.x-amz-meta-sha256}"
    ├────────────────
    │ data.aws_s3_bucket_object.lambda.metadata is map of string with 1 element

This map does not have an element with the key "x-amz-meta-sha256".

I am not sure if 'metadata' is a native Terraform map datatype or not either.

CodePudding user response:

It should be:

source_code_hash = data.aws_s3_bucket_object.lambda.metadata.Sha256
  • Related