Skip to content

Instantly share code, notes, and snippets.

@aaronsteers
Last active March 14, 2020 23:07
Show Gist options
  • Save aaronsteers/19eb4d6cba926327f8b25089cb79259b to your computer and use it in GitHub Desktop.
Save aaronsteers/19eb4d6cba926327f8b25089cb79259b to your computer and use it in GitHub Desktop.

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"
      ]
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment