Skip to content

Commit 9669752

Browse files
authored
feat(s3-static-website): make website bucket ACLs optional (#95)
1 parent 01d002b commit 9669752

5 files changed

Lines changed: 104 additions & 5 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ In order to run all checks at any point run the following command:
4242

4343
| Name | Version |
4444
|------|---------|
45-
| <a name="provider_aws.acm_provider"></a> [aws.acm\_provider](#provider\_aws.acm\_provider) | >= 4.0 |
46-
| <a name="provider_aws.main"></a> [aws.main](#provider\_aws.main) | >= 4.0 |
45+
| <a name="provider_aws.acm_provider"></a> [aws.acm\_provider](#provider\_aws.acm\_provider) | 6.35.1 |
46+
| <a name="provider_aws.main"></a> [aws.main](#provider\_aws.main) | 6.35.1 |
4747

4848
## Modules
4949

@@ -110,7 +110,8 @@ In order to run all checks at any point run the following command:
110110
| <a name="input_name_prefix"></a> [name\_prefix](#input\_name\_prefix) | Name prefix for resources on AWS | `any` | n/a | yes |
111111
| <a name="input_route53_hosted_zone_id"></a> [route53\_hosted\_zone\_id](#input\_route53\_hosted\_zone\_id) | The Route 53 hosted zone ID to use if create\_route53\_hosted\_zone is false | `string` | `""` | no |
112112
| <a name="input_tags"></a> [tags](#input\_tags) | Resource tags | `map(string)` | `{}` | no |
113-
| <a name="input_website_bucket_acl"></a> [website\_bucket\_acl](#input\_website\_bucket\_acl) | (Optional) The canned ACL to apply. Valid values are private, public-read, public-read-write, aws-exec-read, authenticated-read, and log-delivery-write. Defaults to private. | `string` | `"private"` | no |
113+
| <a name="input_website_bucket_acl"></a> [website\_bucket\_acl](#input\_website\_bucket\_acl) | (Optional) The canned ACL to apply when website\_bucket\_acl\_enabled is true. Valid values are private, public-read, public-read-write, aws-exec-read, authenticated-read, and log-delivery-write. Defaults to private. | `string` | `"private"` | no |
114+
| <a name="input_website_bucket_acl_enabled"></a> [website\_bucket\_acl\_enabled](#input\_website\_bucket\_acl\_enabled) | (Optional) Whether to manage and apply bucket ACL settings. Keep true for backward compatibility. Set to false to disable ACL usage (BucketOwnerEnforced), which helps avoid cross-account object upload 403 errors. Defaults to true. | `bool` | `true` | no |
114115
| <a name="input_website_bucket_force_destroy"></a> [website\_bucket\_force\_destroy](#input\_website\_bucket\_force\_destroy) | (Optional, Default:false) A boolean that indicates all objects (including any locked objects) should be deleted from the bucket so that the bucket can be destroyed without error. These objects are not recoverable. | `bool` | `false` | no |
115116
| <a name="input_website_bucket_policy"></a> [website\_bucket\_policy](#input\_website\_bucket\_policy) | (Optional) Map containing the IAM policy for the website bucket. Defaults to null and the policy will be generated automatically. See examples/custom-website-bucket-policy/main.tf for configuration example. | `any` | `null` | no |
116117
| <a name="input_website_cors_additional_allowed_origins"></a> [website\_cors\_additional\_allowed\_origins](#input\_website\_cors\_additional\_allowed\_origins) | (Optional) Specifies which origins are allowed besides the domain name specified | `list(string)` | `[]` | no |

examples/acl-disabled/main.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module "test_website" {
2+
source = "../../"
3+
name_prefix = "test-website"
4+
5+
providers = {
6+
aws.main = aws.main
7+
aws.acm_provider = aws.acm_provider
8+
}
9+
10+
website_domain_name = "test.com"
11+
website_bucket_acl_enabled = false
12+
13+
create_acm_certificate = false
14+
acm_certificate_arn_to_use = "arn:aws:acm:us-east-1:123456789000:certificate/01234567-89a-bcde-f012-3456789abcde"
15+
16+
create_route53_hosted_zone = true
17+
18+
aws_accounts_with_read_view_log_bucket = ["mock_account"]
19+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
terraform {
2+
required_version = ">= 0.13"
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
version = ">= 4.0"
7+
}
8+
}
9+
}
10+
11+
provider "aws" {
12+
alias = "main"
13+
region = "us-east-1"
14+
skip_credentials_validation = true
15+
skip_requesting_account_id = true
16+
skip_metadata_api_check = true
17+
s3_use_path_style = true
18+
19+
endpoints {
20+
apigateway = "http://localstack:4566"
21+
cloudformation = "http://localstack:4566"
22+
cloudwatch = "http://localstack:4566"
23+
dynamodb = "http://localstack:4566"
24+
es = "http://localstack:4566"
25+
firehose = "http://localstack:4566"
26+
iam = "http://localstack:4566"
27+
kinesis = "http://localstack:4566"
28+
lambda = "http://localstack:4566"
29+
route53 = "http://localstack:4566"
30+
redshift = "http://localstack:4566"
31+
s3 = "http://localstack:4566"
32+
secretsmanager = "http://localstack:4566"
33+
ses = "http://localstack:4566"
34+
sns = "http://localstack:4566"
35+
sqs = "http://localstack:4566"
36+
ssm = "http://localstack:4566"
37+
stepfunctions = "http://localstack:4566"
38+
sts = "http://localstack:4566"
39+
}
40+
}
41+
42+
provider "aws" {
43+
alias = "acm_provider"
44+
region = "us-east-1"
45+
skip_credentials_validation = true
46+
skip_requesting_account_id = true
47+
skip_metadata_api_check = true
48+
s3_use_path_style = true
49+
50+
endpoints {
51+
apigateway = "http://localstack:4566"
52+
cloudformation = "http://localstack:4566"
53+
cloudwatch = "http://localstack:4566"
54+
dynamodb = "http://localstack:4566"
55+
es = "http://localstack:4566"
56+
firehose = "http://localstack:4566"
57+
iam = "http://localstack:4566"
58+
kinesis = "http://localstack:4566"
59+
lambda = "http://localstack:4566"
60+
route53 = "http://localstack:4566"
61+
redshift = "http://localstack:4566"
62+
s3 = "http://localstack:4566"
63+
secretsmanager = "http://localstack:4566"
64+
ses = "http://localstack:4566"
65+
sns = "http://localstack:4566"
66+
sqs = "http://localstack:4566"
67+
ssm = "http://localstack:4566"
68+
stepfunctions = "http://localstack:4566"
69+
sts = "http://localstack:4566"
70+
}
71+
}

variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,17 @@ variable "website_domain_name" {
4747
}
4848

4949
variable "website_bucket_acl" {
50-
description = "(Optional) The canned ACL to apply. Valid values are private, public-read, public-read-write, aws-exec-read, authenticated-read, and log-delivery-write. Defaults to private."
50+
description = "(Optional) The canned ACL to apply when website_bucket_acl_enabled is true. Valid values are private, public-read, public-read-write, aws-exec-read, authenticated-read, and log-delivery-write. Defaults to private."
5151
type = string
5252
default = "private"
5353
}
5454

55+
variable "website_bucket_acl_enabled" {
56+
description = "(Optional) Whether to manage and apply bucket ACL settings. Keep true for backward compatibility. Set to false to disable ACL usage (BucketOwnerEnforced), which helps avoid cross-account object upload 403 errors. Defaults to true."
57+
type = bool
58+
default = true
59+
}
60+
5561
variable "website_bucket_force_destroy" {
5662
description = "(Optional, Default:false) A boolean that indicates all objects (including any locked objects) should be deleted from the bucket so that the bucket can be destroyed without error. These objects are not recoverable."
5763
type = bool

website.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ resource "aws_s3_bucket_ownership_controls" "website" {
7373

7474
bucket = aws_s3_bucket.website.id
7575
rule {
76-
object_ownership = "BucketOwnerPreferred"
76+
object_ownership = var.website_bucket_acl_enabled ? "BucketOwnerPreferred" : "BucketOwnerEnforced"
7777
}
7878
}
7979

8080
resource "aws_s3_bucket_acl" "website" {
81+
count = var.website_bucket_acl_enabled ? 1 : 0
82+
8183
depends_on = [aws_s3_bucket_ownership_controls.website]
8284
provider = aws.main
8385

0 commit comments

Comments
 (0)