HiveBrain v1.2.0
Get Started
← Back to all entries
snippetterraformMinor

How to assign an ACL for each S3 Bucket in a tuple/list using Terraform?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
eachbucketacltupleforusinghowlistassignterraform

Problem

I have a terraform file set to create a list of S3 buckets, and I was assigning acl to each bucket the old way:

resource "aws_s3_bucket" "this" {
    count = length(var.s3_bucket_names)
    bucket = var.s3_bucket_names[count.index]
    acl = var.acl
    tags = var.tags
}


However, I want to know how to do it using aws_s3_bucket_acl but I can't reference the bucket because it is a tuple and not a string. I've tried a lot of things but I've got nothing so far.

This is what I want to do:

resource "aws_s3_bucket_acl" "this_acl" {
    bucket = HERE I DON'T KNOW HOW TO REFERENCE A TUPLE OR LIST
    acl = var.acl
}


What should I do to reference a tuple since aws_s3_bucket.this.*.id doesn't work with a tuple/list?

Thank you in advance.

Solution

I just found a solution, I've created "count" for every resource:

resource "aws_s3_bucket" "this" {
    count = length(var.s3_bucket_names)
    bucket = var.s3_bucket_names[count.index]
    tags = var.tags
}

resource "aws_s3_bucket_acl" "this_acl" {
    count = length(aws_s3_bucket.this)
    bucket = aws_s3_bucket.this[count.index].id
    acl = var.acl
}

Code Snippets

resource "aws_s3_bucket" "this" {
    count = length(var.s3_bucket_names)
    bucket = var.s3_bucket_names[count.index]
    tags = var.tags
}

resource "aws_s3_bucket_acl" "this_acl" {
    count = length(aws_s3_bucket.this)
    bucket = aws_s3_bucket.this[count.index].id
    acl = var.acl
}

Context

StackExchange DevOps Q#15949, answer score: 2

Revisions (0)

No revisions yet.