I have a bunch of files that are published to an S3 bucket managed using the terraform script below. To make file tracking easy, I used a combination of for_each
when declaring an aws_s3_object and fileset()
to grab the list of files from my local volume.
I want to create an SSM Parameter that contains a common separated string of the list of files that I have published to S3 using the aws_s3_object (ie: "scripts/app/script1.py, scripts/app/script2.py, scripts/app/script3.py")
My approach to this problem is to use a combination of locals
block and for
statement to iterate over each key of aws_s3_object.app_files
map
and construct the string, but I can't figure out how to write this for
statement as the examples from the documentation seem to suggest that the results of the for
statement will result in a list or object and not a string.
Is it even possible to do this? And if so, how can this be achieved?
resource "aws_s3_object" "app_files" {
for_each = fileset("scripts/", "**.py")
bucket = "myBucket"
key = "dev/app/${each.value}"
server_side_encryption = "aws:kms"
bucket_key_enabled = true
source = "scripts/${each.value}"
source_hash = filemd5("scripts/${each.value}")
tags = {
Name = each.value
}
}
CodePudding user response:
The easiest would be to use join:
locals {
joined_keys = join(",", keys(aws_s3_object.app_files))
}
Sometimes, json
string may be more useful:
locals {
joined_keys_json = jsonencode(keys(aws_s3_object.app_files))
}