A typical issue with lambda, is that Terraform doesn’t seem to realise the zipped payload is updated unless the file name changes.
- Terraform does not update AWS canary code
- https://github.com/hashicorp/terraform-provider-aws/issues/18089#issuecomment-1007767809
Say I have code that looks like:
data "archive_file" "canary_archive_file" {
for_each = var.endpoints
type = "zip"
output_path = "${path.module}/tmp/${each.key}.zip"
source {
content = templatefile("${path.module}/canary-lambda.js.tpl", {
endpoint = each.value.url
})
filename = "nodejs/node_modules/index.js"
}
}
It’s not apparent how to set the output_path based on the checksum (md5?) of the source content. Subsequently I need to use that path in the zip_file parameter for aws_synthetics_canary.
>Solution :
I think you can do what you want using md5 function:
locals {
file_content = { for k, v in var.endpoints:
k => templatefile("${path.module}/canary-lambda.js.tpl", {
endpoint = v.url
})
}
}
data "archive_file" "canary_archive_file" {
for_each = var.endpoints
type = "zip"
output_path = "${path.module}/tmp/${each.key}-${md5(local.file_content[each.key])}.zip"
source {
content = local.file_content[each.key]
filename = "nodejs/node_modules/index.js"
}
}