Skip to content

Commit c5d4d86

Browse files
authored
default to creating bucket policy instead of ACL (#16)
* default to creating bucket policy instead of ACL * Update README.md
1 parent 236a8c9 commit c5d4d86

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

README.md

+34-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,47 @@
11
# terraform-aws-s3logging-bucket
2-
[![tflint](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/workflows/tflint/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions?query=workflow%3Atflint+event%3Apush+branch%3Amaster)
3-
[![tfsec](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/workflows/tfsec/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions?query=workflow%3Atfsec+event%3Apush+branch%3Amaster)
4-
[![yamllint](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/workflows/yamllint/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions?query=workflow%3Ayamllint+event%3Apush+branch%3Amaster)
5-
[![misspell](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/workflows/misspell/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions?query=workflow%3Amisspell+event%3Apush+branch%3Amaster)
6-
[![pre-commit-check](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/workflows/pre-commit-check/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions?query=workflow%3Apre-commit-check+event%3Apush+branch%3Amaster)
7-
<a href="https://twitter.com/intent/follow?screen_name=RhythmicTech"><img src="https://img.shields.io/twitter/follow/RhythmicTech?style=social&logo=twitter" alt="follow on Twitter"></a>
2+
[![tflint](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions/workflows/tflint.yaml/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions?query=workflow%3Atflint+event%3Apush+branch%3Amaster)
3+
[![tfsec](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions/workflows/tfsec.yaml/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions?query=workflow%3Atfsec+event%3Apush+branch%3Amaster)
4+
[![yamllint](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions/workflows/yamllint.yaml/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions?query=workflow%3Ayamllint+event%3Apush+branch%3Amaster)
5+
[![misspell](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions/workflows/misspell.yaml/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions?query=workflow%3Amisspell+event%3Apush+branch%3Amaster)
6+
[![pre-commit-check](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions/workflows/pre-commit.yaml/badge.svg?branch=master&event=push)](https://github.com/rhythmictech/terraform-aws-s3logging-bucket/actions?query=workflow%3Apre-commit-check+event%3Apush+branch%3Amaster)
7+
![X (formerly Twitter) Follow](https://img.shields.io/twitter/follow/RhythmicTech)
88

99

1010
Create and manage a bucket suitable for access logging for other S3 buckets.
1111

1212

1313
## Usage
14+
Basic usage:
1415
```
1516
module "s3logging-bucket" {
1617
source = "rhythmictech/s3logging-bucket/aws"
1718
}
1819
```
1920

21+
Combine with other S3-based modules, like our cloudtrail bucket module:
22+
```
23+
module "s3logging-bucket" {
24+
source = "rhythmictech/s3logging-bucket/aws"
25+
version = "3.3.0"
26+
}
27+
28+
module "cloudtrail-bucket" {
29+
source = "git::https://github.com/rhythmictech/terraform-aws-cloudtrail-bucket?ref=v4.0.0"
30+
31+
logging_bucket = module.s3logging-bucket.s3_bucket_name
32+
region = var.region
33+
}
34+
35+
module "cloudtrail-logging" {
36+
source = "git::https://github.com/rhythmictech/terraform-aws-cloudtrail-logging?ref=v1.3.0"
37+
38+
cloudtrail_bucket = module.cloudtrail-bucket.s3_bucket_name
39+
kms_key_id = module.cloudtrail-bucket.kms_key_id
40+
region = var.region
41+
}
42+
```
43+
44+
2045
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
2146
## Requirements
2247

@@ -42,6 +67,8 @@ No modules.
4267
| [aws_s3_bucket.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
4368
| [aws_s3_bucket_acl.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_acl) | resource |
4469
| [aws_s3_bucket_lifecycle_configuration.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_lifecycle_configuration) | resource |
70+
| [aws_s3_bucket_ownership_controls.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_ownership_controls) | resource |
71+
| [aws_s3_bucket_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
4572
| [aws_s3_bucket_public_access_block.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
4673
| [aws_s3_bucket_server_side_encryption_configuration.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
4774
| [aws_s3_bucket_versioning.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_versioning) | resource |
@@ -56,6 +83,7 @@ No modules.
5683
| <a name="input_bucket_suffix"></a> [bucket\_suffix](#input\_bucket\_suffix) | Suffix to apply to the bucket (use `bucket_name` or `bucket_suffix`). When using `bucket_suffix`, the bucket name will be `[account_id]-[region]-s3logging-[bucket_suffix].` | `string` | `"default"` | no |
5784
| <a name="input_kms_key_id"></a> [kms\_key\_id](#input\_kms\_key\_id) | KMS key to encrypt bucket with. | `string` | `null` | no |
5885
| <a name="input_lifecycle_rules"></a> [lifecycle\_rules](#input\_lifecycle\_rules) | lifecycle rules to apply to the bucket | <pre>list(object(<br> {<br> id = string<br> enabled = optional(bool, true)<br> expiration = optional(number)<br> prefix = optional(number)<br> noncurrent_version_expiration = optional(number)<br> transition = optional(list(object({<br> days = number<br> storage_class = string<br> })))<br> }))</pre> | <pre>[<br> {<br> "id": "expire-noncurrent-objects-after-ninety-days",<br> "noncurrent_version_expiration": 90<br> },<br> {<br> "id": "transition-to-IA-after-30-days",<br> "transition": [<br> {<br> "days": 30,<br> "storage_class": "STANDARD_IA"<br> }<br> ]<br> },<br> {<br> "expiration": 2557,<br> "id": "delete-after-seven-years"<br> }<br>]</pre> | no |
86+
| <a name="input_object_ownership"></a> [object\_ownership](#input\_object\_ownership) | Specifies S3 object ownership control. Defaults to BucketOwnerPreferred for backwards-compatibility. Recommended value is BucketOwnerEnforced. | `string` | `"BucketOwnerEnforced"` | no |
5987
| <a name="input_tags"></a> [tags](#input\_tags) | Tags to add to supported resources | `map(string)` | `{}` | no |
6088
| <a name="input_versioning_enabled"></a> [versioning\_enabled](#input\_versioning\_enabled) | Whether or not to use versioning on the bucket. This can be useful for audit purposes since objects in a logging bucket should not be updated. | `bool` | `true` | no |
6189

main.tf

+30
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,33 @@ resource "aws_s3_bucket_versioning" "this" {
103103
status = "Enabled"
104104
}
105105
}
106+
107+
resource "aws_s3_bucket_policy" "this" {
108+
count = var.object_ownership == "BucketOwnerEnforced" ? 1 : 0
109+
110+
bucket = aws_s3_bucket.this.id
111+
112+
policy = <<EOF
113+
{
114+
"Version": "2012-10-17",
115+
"Statement": [
116+
{
117+
"Sid": "S3ServerAccessLogsPolicy",
118+
"Effect": "Allow",
119+
"Principal": {
120+
"Service": "logging.s3.amazonaws.com"
121+
},
122+
"Action": [
123+
"s3:PutObject"
124+
],
125+
"Resource": "arn:aws:s3:::${aws_s3_bucket.this.bucket}/*",
126+
"Condition": {
127+
"StringEquals": {
128+
"aws:SourceAccount": "${local.account_id}"
129+
}
130+
}
131+
}
132+
]
133+
}
134+
EOF
135+
}

variables.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ variable "lifecycle_rules" {
5252
}
5353

5454
variable "object_ownership" {
55-
default = "BucketOwnerPreferred"
55+
default = "BucketOwnerEnforced"
5656
description = "Specifies S3 object ownership control. Defaults to BucketOwnerPreferred for backwards-compatibility. Recommended value is BucketOwnerEnforced."
5757
}
5858

0 commit comments

Comments
 (0)