There's no easy way to go from a full S3 path to the isolated bucket and key path, so I created the below snippet to do this.
bucket = split("/", split("//", var.s3_path_to_lambda_zip)[1])[0]
key = join("/", slice(
split("/", split("//", var.s3_path_to_lambda_zip)[1]),
1,
length(split("/", split("//", var.s3_path_to_lambda_zip)[1]))
))
While not easy to read, the outcome is clear. We want the "bucket" part of the path to be
passed as input to bucket
and the "key" part of the path to passed as input to key
.
Here's a working example:
resource "aws_s3_bucket_object" "s3_lambda_zip" {
count = local.is_disabled ? 0 : 1
bucket = split("/", split("//", var.s3_path_to_lambda_zip)[1])[0]
key = join("/", slice(
split("/", split("//", var.s3_path_to_lambda_zip)[1]),
1,
length(split("/", split("//", var.s3_path_to_lambda_zip)[1]))
))
source = "/path/to/my/local/file"
}
When appending a suffix to the key path, use this extended snippet:
s3_bucket = split("/", split("//", var.data_lake_storage_path)[1])[0]
s3_key_prefix = join("/",
[
join("/", slice(
split("/", split("//", var.data_lake_storage_path)[1]),
1,
length(split("/", split("//", var.data_lake_storage_path)[1]))
)),
"key/path/suffix.txt"
]
)