Created
July 12, 2018 19:25
-
-
Save arnobroekhof/5e51e081a85740a80a045bb424a9e825 to your computer and use it in GitHub Desktop.
Terraform 0.11.x loop through a map and create a s3 bucket with objects with specific content based on a maps --> key, value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## construct a map with object keys and values | |
variable "object_list" { | |
type = "map" | |
default = { | |
content1 = "this is example content 1", | |
content2 = "this is example content 2" | |
} | |
} | |
## construct the bucket name | |
variable "bucket_name" { | |
type = "string" | |
default = "testbucket" | |
} | |
## create the bucket | |
resource "aws_s3_bucket" "testbucket" { | |
bucket = "${var.bucket_name}" | |
acl = "private" | |
} | |
## loop through the items in the map object_list and create the object one by one | |
resource "aws_s3_bucket_object" "bucket_objects" { | |
count = "${length(var.object_list)}" | |
bucket = "${var.bucket_name}" | |
key = "${element(keys(var.object_list), count.index)}" # lookup the key name based on the current count index. | |
content = "${lookup(var.object_list, "${element(keys(var.object_list), count.index)}")}" # lookup the key's value based on a double interpolation. | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment