Last active
August 19, 2020 15:56
-
-
Save joatmon08/9e88fcd61e5902bd273d460e76e274f9 to your computer and use it in GitHub Desktop.
S3 Bucket Example for Permissions Toggle
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
variable "enable_object_editor" { | |
default = false | |
type = bool | |
description = "enables object editor for current aws account" | |
} | |
data "aws_caller_identity" "current" {} | |
resource "aws_s3_bucket" "my_bucket" { | |
bucket = "my_bucket" | |
} | |
locals { | |
my_bucket_policy = [ | |
{ | |
actions = [ | |
"s3:ListBucket", | |
] | |
resources = [ | |
"arn:aws:s3:::${aws_s3_bucket.my_bucket.bucket}", | |
] | |
principals = [ | |
"*" | |
] | |
} | |
] | |
edit_statement = [ | |
{ | |
actions = [ | |
"s3:*Object", | |
] | |
resources = [ | |
"arn:aws:s3:::${aws_s3_bucket.my_bucket.bucket}", | |
] | |
principals = [ | |
"${data.aws_caller_identity.current.arn}" | |
] | |
} | |
] | |
statements = var.enable_object_editor ? concat(local.my_bucket_policy, local.edit_statement) : local.my_bucket_policy | |
} | |
data "aws_iam_policy_document" "my_bucket" { | |
dynamic "statement" { | |
for_each = local.statements | |
content { | |
actions = statement.value["actions"] | |
resources = statement.value["resources"] | |
principals { | |
type = "AWS" | |
identifiers = statement.value["principals"] | |
} | |
} | |
} | |
} | |
resource "aws_s3_bucket_policy" "my_bucket" { | |
bucket = aws_s3_bucket.my_bucket.id | |
policy = data.aws_iam_policy_document.my_bucket.json | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment