Skip to content

Commit 85851e0

Browse files
authored
feat: Allow buckets to be individually customized (#13)
* feat: Allow buckets to be individually customized TOOLS-2606 * Move acl, block_public_acls, block_public_policy, restrict_public_buckets, ignore_public_acls into s3_bucket_config
1 parent ad6af90 commit 85851e0

File tree

3 files changed

+36
-68
lines changed

3 files changed

+36
-68
lines changed

iam_roles.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ data "aws_iam_policy_document" "s3_bucket_access_role" {
1919

2020
data "aws_iam_policy_document" "s3_bucket_access" {
2121
statement {
22-
resources = [for s in var.s3_bucket_names : "arn:aws:s3:::${s}"]
22+
resources = [for s in keys(var.s3_bucket_config) : "arn:aws:s3:::${s}"]
2323
effect = "Allow"
2424
actions = ["s3:*"]
2525
sid = "terraform0"
2626
}
2727

2828
statement {
29-
resources = [for s in var.s3_bucket_names : "arn:aws:s3:::${s}/*"]
29+
resources = [for s in keys(var.s3_bucket_config) : "arn:aws:s3:::${s}/*"]
3030
effect = "Allow"
3131
actions = ["s3:*"]
3232
sid = "terraform1"

s3_bucket.tf

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Give an s3 bucket to each bucket name passed to the module
22
resource "aws_s3_bucket" "s3_buckets" {
3-
for_each = var.s3_bucket_names
3+
for_each = var.s3_bucket_config
44

5-
bucket = each.value
6-
acl = var.acl
5+
bucket = each.key
6+
acl = lookup(each.value, "acl", "private")
77
tags = var.tags
88

99
dynamic "lifecycle_rule" {
10-
for_each = var.lifecycle_rule_inputs == null ? [] : var.lifecycle_rule_inputs
10+
for_each = lookup(each.value, "lifecycle_rule_inputs", [])
1111

1212
content {
1313
enabled = lifecycle_rule.value.enabled
@@ -24,7 +24,7 @@ resource "aws_s3_bucket" "s3_buckets" {
2424
}
2525

2626
dynamic "cors_rule" {
27-
for_each = var.cors_rule_inputs == null ? [] : var.cors_rule_inputs
27+
for_each = lookup(each.value, "cors_rule_inputs", [])
2828

2929
content {
3030
allowed_headers = cors_rule.value.allowed_headers
@@ -38,14 +38,14 @@ resource "aws_s3_bucket" "s3_buckets" {
3838

3939
# Make sure no object could ever be public
4040
resource "aws_s3_bucket_public_access_block" "s3_buckets" {
41-
for_each = var.s3_bucket_names
41+
for_each = var.s3_bucket_config
4242

43-
bucket = each.value
43+
bucket = each.key
4444

45-
block_public_acls = var.block_public_acls
46-
block_public_policy = var.block_public_policy
47-
restrict_public_buckets = var.restrict_public_buckets
48-
ignore_public_acls = var.ignore_public_acls
45+
block_public_acls = lookup(each.value, "block_public_acls", true)
46+
block_public_policy = lookup(each.value, "block_public_policy", true)
47+
restrict_public_buckets = lookup(each.value, "restrict_public_buckets", true)
48+
ignore_public_acls = lookup(each.value, "ignore_public_acls", true)
4949

5050
depends_on = [aws_s3_bucket.s3_buckets]
5151
}

vars.tf

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,29 @@ variable "aws_region" {
44
default = "us-east-2"
55
}
66

7-
variable "s3_bucket_names" {
8-
type = set(string)
9-
description = "one or many of your s3 bucket name(s)"
7+
variable "s3_bucket_config" {
8+
type = map(object({
9+
acl = string
10+
block_public_acls = bool
11+
block_public_policy = bool
12+
restrict_public_buckets = bool
13+
ignore_public_acls = bool
14+
cors_rule_inputs = list(object({
15+
allowed_headers = list(string)
16+
allowed_methods = list(string)
17+
allowed_origins = list(string)
18+
expose_headers = list(string)
19+
max_age_seconds = number
20+
}))
21+
lifecycle_rule_inputs = list(object({
22+
enabled = string
23+
abort_incomplete_multipart_upload_days = string
24+
expiration_inputs = list(object({
25+
days = number
26+
}))
27+
}))
28+
}))
29+
default = null
1030
}
1131

1232
variable "remote_principals_arns" {
@@ -23,56 +43,4 @@ variable "tags" {
2343
variable "role_name" {
2444
type = string
2545
description = "name to give your role that will be able to be assume by remote principal(s)"
26-
}
27-
28-
variable "acl" {
29-
type = string
30-
default = "private"
31-
description = "The canned ACL to apply."
32-
}
33-
34-
variable "block_public_acls" {
35-
type = string
36-
default = true
37-
description = "PUT Object calls will fail if the request includes an object ACL."
38-
}
39-
40-
variable "block_public_policy" {
41-
type = string
42-
default = true
43-
description = "Reject calls to PUT Bucket policy if the specified bucket policy allows public access."
44-
}
45-
46-
variable "restrict_public_buckets" {
47-
type = string
48-
default = true
49-
description = "Ignore public ACLs on this bucket and any objects that it contains."
50-
}
51-
52-
variable "ignore_public_acls" {
53-
type = string
54-
default = true
55-
description = "Only the bucket owner and AWS Services can access this buckets if it has a public policy."
56-
}
57-
58-
variable "lifecycle_rule_inputs" {
59-
type = list(object({
60-
enabled = string
61-
abort_incomplete_multipart_upload_days = string
62-
expiration_inputs = list(object({
63-
days = number
64-
}))
65-
}))
66-
default = null
67-
}
68-
69-
variable "cors_rule_inputs" {
70-
type = list(object({
71-
allowed_headers = list(string)
72-
allowed_methods = list(string)
73-
allowed_origins = list(string)
74-
expose_headers = list(string)
75-
max_age_seconds = number
76-
}))
77-
default = null
7846
}

0 commit comments

Comments
 (0)