Skip to content

Commit 69f0f4e

Browse files
authored
feat: Added Account-level Public Access Block module, and minor fixes as fallback (#299)
1 parent 8b855f8 commit 69f0f4e

File tree

17 files changed

+343
-7
lines changed

17 files changed

+343
-7
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ These features of S3 bucket configurations are supported:
1616
- Cross-Region Replication (CRR)
1717
- ELB log delivery bucket policy
1818
- ALB/NLB log delivery bucket policy
19+
- Account-level Public Access Block
1920

2021
## Usage
2122

@@ -117,8 +118,11 @@ Users of Terragrunt can achieve similar results by using modules provided in the
117118

118119
- [Complete](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/complete) - Complete S3 bucket with most of supported features enabled
119120
- [Cross-Region Replication](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/s3-replication) - S3 bucket with Cross-Region Replication (CRR) enabled
120-
- [S3 Bucket Notifications](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/notification) - S3 bucket notifications to Lambda functions, SQS queues, and SNS topics.
121-
- [S3 Bucket Object](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/object) - Manage S3 bucket objects.
121+
- [S3 Notifications](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/notification) - S3 bucket notifications to Lambda functions, SQS queues, and SNS topics.
122+
- [S3 Object](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/object) - Manage S3 bucket objects.
123+
- [S3 Analytics](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/s3-analytics) - S3 bucket Analytics Configurations.
124+
- [S3 Inventory](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/s3-inventory) - S3 bucket Inventory configuration.
125+
- [S3 Account-level Public Access Block](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/account-public-access) - Manage S3 account-level Public Access Block.
122126

123127
<!-- BEGIN_TF_DOCS -->
124128
## Requirements
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# S3 account-level Public Access Block
2+
3+
Configuration in this directory creates S3 account-level Public Access Block.
4+
5+
## Usage
6+
7+
To run this example you need to execute:
8+
9+
```bash
10+
$ terraform init
11+
$ terraform plan
12+
$ terraform apply
13+
```
14+
15+
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
16+
17+
<!-- BEGIN_TF_DOCS -->
18+
## Requirements
19+
20+
| Name | Version |
21+
|------|---------|
22+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
23+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.70 |
24+
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |
25+
26+
## Providers
27+
28+
No providers.
29+
30+
## Modules
31+
32+
| Name | Source | Version |
33+
|------|--------|---------|
34+
| <a name="module_account_public_access"></a> [account\_public\_access](#module\_account\_public\_access) | ../../modules/account-public-access | n/a |
35+
36+
## Resources
37+
38+
No resources.
39+
40+
## Inputs
41+
42+
No inputs.
43+
44+
## Outputs
45+
46+
| Name | Description |
47+
|------|-------------|
48+
| <a name="output_s3_account_public_access_block_id"></a> [s3\_account\_public\_access\_block\_id](#output\_s3\_account\_public\_access\_block\_id) | AWS account ID |
49+
<!-- END_TF_DOCS -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
provider "aws" {
2+
region = local.region
3+
4+
# Make it faster by skipping something
5+
skip_metadata_api_check = true
6+
skip_region_validation = true
7+
skip_credentials_validation = true
8+
}
9+
10+
locals {
11+
region = "eu-west-1"
12+
}
13+
14+
module "account_public_access" {
15+
source = "../../modules/account-public-access"
16+
17+
block_public_acls = true
18+
block_public_policy = true
19+
ignore_public_acls = true
20+
restrict_public_buckets = true
21+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "s3_account_public_access_block_id" {
2+
description = "AWS account ID"
3+
value = module.account_public_access.s3_account_public_access_block_id
4+
}

examples/account-public-access/variables.tf

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 5.70"
8+
}
9+
random = {
10+
source = "hashicorp/random"
11+
version = ">= 2.0"
12+
}
13+
}
14+
}

main.tf

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ resource "aws_s3_bucket_logging" "this" {
3939
bucket = aws_s3_bucket.this[0].id
4040

4141
target_bucket = var.logging["target_bucket"]
42-
target_prefix = try(var.logging["target_prefix"], null)
43-
42+
target_prefix = var.logging["target_prefix"]
4443

4544
dynamic "target_object_key_format" {
4645
for_each = try([var.logging["target_object_key_format"]], [])
@@ -55,7 +54,7 @@ resource "aws_s3_bucket_logging" "this" {
5554
}
5655

5756
dynamic "simple_prefix" {
58-
for_each = contains(keys(target_object_key_format.value), "simple_prefix") ? [true] : []
57+
for_each = length(try(target_object_key_format.value["partitioned_prefix"], [])) == 0 || can(target_object_key_format.value["simple_prefix"]) ? [true] : []
5958

6059
content {}
6160
}
@@ -166,7 +165,7 @@ resource "aws_s3_bucket_versioning" "this" {
166165

167166
versioning_configuration {
168167
# Valid values: "Enabled" or "Suspended"
169-
status = try(var.versioning["enabled"] ? "Enabled" : "Suspended", tobool(var.versioning["status"]) ? "Enabled" : "Suspended", title(lower(var.versioning["status"])))
168+
status = try(var.versioning["enabled"] ? "Enabled" : "Suspended", tobool(var.versioning["status"]) ? "Enabled" : "Suspended", title(lower(var.versioning["status"])), "Enabled")
170169

171170
# Valid values: "Enabled" or "Disabled"
172171
mfa_delete = try(tobool(var.versioning["mfa_delete"]) ? "Enabled" : "Disabled", title(lower(var.versioning["mfa_delete"])), null)
@@ -381,7 +380,6 @@ resource "aws_s3_bucket_replication_configuration" "this" {
381380
content {
382381
id = try(rule.value.id, null)
383382
priority = try(rule.value.priority, null)
384-
prefix = try(rule.value.prefix, null)
385383
status = try(tobool(rule.value.status) ? "Enabled" : "Disabled", title(lower(rule.value.status)), "Enabled")
386384

387385
dynamic "delete_marker_replication" {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# S3 account-level Public Access Block
2+
3+
Manages S3 account-level Public Access Block configuration.
4+
5+
## Note
6+
7+
Each AWS account may only have one S3 Public Access Block configuration.
8+
9+
<!-- BEGIN_TF_DOCS -->
10+
## Requirements
11+
12+
| Name | Version |
13+
|------|---------|
14+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
15+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.74 |
16+
17+
## Providers
18+
19+
| Name | Version |
20+
|------|---------|
21+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.74 |
22+
23+
## Modules
24+
25+
No modules.
26+
27+
## Resources
28+
29+
| Name | Type |
30+
|------|------|
31+
| [aws_s3_account_public_access_block.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_account_public_access_block) | resource |
32+
33+
## Inputs
34+
35+
| Name | Description | Type | Default | Required |
36+
|------|-------------|------|---------|:--------:|
37+
| <a name="input_account_id"></a> [account\_id](#input\_account\_id) | AWS account ID | `string` | `null` | no |
38+
| <a name="input_block_public_acls"></a> [block\_public\_acls](#input\_block\_public\_acls) | Whether Amazon S3 should block public ACLs for buckets in this account. | `bool` | `false` | no |
39+
| <a name="input_block_public_policy"></a> [block\_public\_policy](#input\_block\_public\_policy) | Whether Amazon S3 should block public bucket policies for buckets in this account. | `bool` | `false` | no |
40+
| <a name="input_create"></a> [create](#input\_create) | Whether to create this resource or not? | `bool` | `true` | no |
41+
| <a name="input_ignore_public_acls"></a> [ignore\_public\_acls](#input\_ignore\_public\_acls) | Whether Amazon S3 should ignore public ACLs for buckets in this account. | `bool` | `false` | no |
42+
| <a name="input_restrict_public_buckets"></a> [restrict\_public\_buckets](#input\_restrict\_public\_buckets) | Whether Amazon S3 should restrict public bucket policies for buckets in this account. | `bool` | `false` | no |
43+
44+
## Outputs
45+
46+
| Name | Description |
47+
|------|-------------|
48+
| <a name="output_s3_account_public_access_block_id"></a> [s3\_account\_public\_access\_block\_id](#output\_s3\_account\_public\_access\_block\_id) | AWS account ID |
49+
<!-- END_TF_DOCS -->

modules/account-public-access/main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "aws_s3_account_public_access_block" "this" {
2+
count = var.create ? 1 : 0
3+
4+
account_id = var.account_id
5+
6+
block_public_acls = var.block_public_acls
7+
block_public_policy = var.block_public_policy
8+
ignore_public_acls = var.ignore_public_acls
9+
restrict_public_buckets = var.restrict_public_buckets
10+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "s3_account_public_access_block_id" {
2+
description = "AWS account ID"
3+
value = try(aws_s3_account_public_access_block.this[0].id, "")
4+
}

0 commit comments

Comments
 (0)