Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bucket name in policy statements, fixing #81 #82

Merged
merged 11 commits into from
May 3, 2024
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Available targets:

| Name | Source | Version |
|------|--------|---------|
| <a name="module_bucket_name"></a> [bucket\_name](#module\_bucket\_name) | cloudposse/label/null | 0.25.0 |
| <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | cloudposse/s3-log-storage/aws | 1.4.2 |
| <a name="module_this"></a> [this](#module\_this) | cloudposse/label/null | 0.25.0 |

Expand Down
1 change: 1 addition & 0 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

| Name | Source | Version |
|------|--------|---------|
| <a name="module_bucket_name"></a> [bucket\_name](#module\_bucket\_name) | cloudposse/label/null | 0.25.0 |
| <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | cloudposse/s3-log-storage/aws | 1.4.2 |
| <a name="module_this"></a> [this](#module\_this) | cloudposse/label/null | 0.25.0 |

Expand Down
23 changes: 20 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
locals {
enabled = module.this.enabled
generate_bucket_name = local.enabled && try(length(var.bucket_name) == 0, true) # Use `try` to handle `null` value
bucket_name = local.generate_bucket_name ? module.bucket_name.id : var.bucket_name
}

module "bucket_name" {
source = "cloudposse/label/null"
version = "0.25.0"

enabled = local.generate_bucket_name

id_length_limit = 63 # https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html

context = module.this.context
}

data "aws_elb_service_account" "default" {
count = module.this.enabled ? 1 : 0
}
Expand All @@ -16,7 +33,7 @@ data "aws_iam_policy_document" "default" {
"s3:PutObject"
]
resources = [
"arn:${data.aws_partition.current.partition}:s3:::${module.this.id}/*",
"arn:${data.aws_partition.current.partition}:s3:::${local.bucket_name}/*",
]
}

Expand All @@ -31,7 +48,7 @@ data "aws_iam_policy_document" "default" {
"s3:PutObject"
]
resources = [
"arn:${data.aws_partition.current.partition}:s3:::${module.this.id}/*",
"arn:${data.aws_partition.current.partition}:s3:::${local.bucket_name}/*",
]
condition {
test = "StringEquals"
Expand All @@ -51,7 +68,7 @@ data "aws_iam_policy_document" "default" {
"s3:GetBucketAcl"
]
resources = [
"arn:${data.aws_partition.current.partition}:s3:::${module.this.id}",
"arn:${data.aws_partition.current.partition}:s3:::${local.bucket_name}",
]
}
}
Expand Down