Skip to content

Commit 46d5a85

Browse files
authored
feat: ALB/NLB log delivery support (terraform-aws-modules#96)
1 parent 9138a96 commit 46d5a85

File tree

7 files changed

+82
-3
lines changed

7 files changed

+82
-3
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ These features of S3 bucket configurations are supported:
1313
- object locking
1414
- Cross-Region Replication (CRR)
1515
- ELB log delivery bucket policy
16+
- ALB/NLB log delivery bucket policy
1617

1718
## Usage
1819

@@ -48,6 +49,22 @@ module "s3_bucket_for_logs" {
4849
}
4950
```
5051

52+
### Bucket with ALB/NLB access log delivery policy attached
53+
54+
```hcl
55+
module "s3_bucket_for_logs" {
56+
source = "terraform-aws-modules/s3-bucket/aws"
57+
58+
bucket = "my-s3-bucket-for-logs"
59+
acl = "log-delivery-write"
60+
61+
# Allow deletion of non-empty bucket
62+
force_destroy = true
63+
64+
attach_lb_log_delivery_policy = true
65+
}
66+
```
67+
5168
## Conditional creation
5269

5370
Sometimes you need to have a way to create S3 resources conditionally but Terraform does not allow to use `count` inside `module` block, so the solution is to specify argument `create_bucket`.
@@ -113,6 +130,7 @@ No modules.
113130
| [aws_iam_policy_document.combined](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
114131
| [aws_iam_policy_document.deny_insecure_transport](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
115132
| [aws_iam_policy_document.elb_log_delivery](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
133+
| [aws_iam_policy_document.lb_log_delivery](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
116134

117135
## Inputs
118136

@@ -122,6 +140,7 @@ No modules.
122140
| <a name="input_acl"></a> [acl](#input\_acl) | (Optional) The canned ACL to apply. Defaults to 'private'. Conflicts with `grant` | `string` | `"private"` | no |
123141
| <a name="input_attach_deny_insecure_transport_policy"></a> [attach\_deny\_insecure\_transport\_policy](#input\_attach\_deny\_insecure\_transport\_policy) | Controls if S3 bucket should have deny non-SSL transport policy attached | `bool` | `false` | no |
124142
| <a name="input_attach_elb_log_delivery_policy"></a> [attach\_elb\_log\_delivery\_policy](#input\_attach\_elb\_log\_delivery\_policy) | Controls if S3 bucket should have ELB log delivery policy attached | `bool` | `false` | no |
143+
| <a name="input_attach_lb_log_delivery_policy"></a> [attach\_lb\_log\_delivery\_policy](#input\_attach\_lb\_log\_delivery\_policy) | Controls if S3 bucket should have ALB/NLB log delivery policy attached | `bool` | `false` | no |
125144
| <a name="input_attach_policy"></a> [attach\_policy](#input\_attach\_policy) | Controls if S3 bucket should have bucket policy attached (set to `true` to use value of `policy` as bucket policy) | `bool` | `false` | no |
126145
| <a name="input_attach_public_policy"></a> [attach\_public\_policy](#input\_attach\_public\_policy) | Controls if a user defined public bucket policy will be attached (set to `false` to allow upstream to apply defaults to the bucket) | `bool` | `true` | no |
127146
| <a name="input_block_public_acls"></a> [block\_public\_acls](#input\_block\_public\_acls) | Whether Amazon S3 should block public ACLs for this bucket. | `bool` | `false` | no |

examples/complete/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Configuration in this directory creates S3 bucket which demos such capabilities:
44
- static web-site hosting
5-
- access logging (for S3 and ELB)
5+
- access logging (for S3, ELB and ALB/NLB)
66
- versioning
77
- CORS
88
- lifecycle rules

examples/complete/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module "log_bucket" {
5555
acl = "log-delivery-write"
5656
force_destroy = true
5757
attach_elb_log_delivery_policy = true
58+
attach_lb_log_delivery_policy = true
5859
attach_deny_insecure_transport_policy = true
5960
}
6061

main.tf

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
locals {
2-
attach_policy = var.attach_elb_log_delivery_policy || var.attach_deny_insecure_transport_policy || var.attach_policy
2+
attach_policy = var.attach_elb_log_delivery_policy || var.attach_lb_log_delivery_policy || var.attach_deny_insecure_transport_policy || var.attach_policy
33
}
44

55
resource "aws_s3_bucket" "this" {
@@ -247,6 +247,7 @@ data "aws_iam_policy_document" "combined" {
247247

248248
source_policy_documents = compact([
249249
var.attach_elb_log_delivery_policy ? data.aws_iam_policy_document.elb_log_delivery[0].json : "",
250+
var.attach_lb_log_delivery_policy ? data.aws_iam_policy_document.lb_log_delivery[0].json : "",
250251
var.attach_deny_insecure_transport_policy ? data.aws_iam_policy_document.deny_insecure_transport[0].json : "",
251252
var.attach_policy ? var.policy : ""
252253
])
@@ -280,6 +281,57 @@ data "aws_iam_policy_document" "elb_log_delivery" {
280281
}
281282
}
282283

284+
# ALB/NLB
285+
286+
data "aws_iam_policy_document" "lb_log_delivery" {
287+
count = var.create_bucket && var.attach_lb_log_delivery_policy ? 1 : 0
288+
289+
statement {
290+
sid = "AWSLogDeliveryWrite"
291+
292+
principals {
293+
type = "Service"
294+
identifiers = ["delivery.logs.amazonaws.com"]
295+
}
296+
297+
effect = "Allow"
298+
299+
actions = [
300+
"s3:PutObject",
301+
]
302+
303+
resources = [
304+
"${aws_s3_bucket.this[0].arn}/*",
305+
]
306+
307+
condition {
308+
test = "StringEquals"
309+
variable = "s3:x-amz-acl"
310+
values = ["bucket-owner-full-control"]
311+
}
312+
}
313+
314+
statement {
315+
sid = "AWSLogDeliveryAclCheck"
316+
317+
effect = "Allow"
318+
319+
principals {
320+
type = "Service"
321+
identifiers = ["delivery.logs.amazonaws.com"]
322+
}
323+
324+
actions = [
325+
"s3:GetBucketAcl",
326+
]
327+
328+
resources = [
329+
"${aws_s3_bucket.this[0].arn}",
330+
]
331+
332+
}
333+
}
334+
283335
data "aws_iam_policy_document" "deny_insecure_transport" {
284336
count = var.create_bucket && var.attach_deny_insecure_transport_policy ? 1 : 0
285337

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ variable "attach_elb_log_delivery_policy" {
1010
default = false
1111
}
1212

13+
variable "attach_lb_log_delivery_policy" {
14+
description = "Controls if S3 bucket should have ALB/NLB log delivery policy attached"
15+
type = bool
16+
default = false
17+
}
18+
1319
variable "attach_deny_insecure_transport_policy" {
1420
description = "Controls if S3 bucket should have deny non-SSL transport policy attached"
1521
type = bool

wrappers/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You may want to use a single Terragrunt configuration file to manage multiple re
66

77
This wrapper does not implement any extra functionality.
88

9-
# Usage with Terragrunt
9+
## Usage with Terragrunt
1010

1111
`terragrunt.hcl`:
1212

wrappers/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module "wrapper" {
55

66
create_bucket = lookup(each.value, "create_bucket", true)
77
attach_elb_log_delivery_policy = lookup(each.value, "attach_elb_log_delivery_policy", false)
8+
attach_lb_log_delivery_policy = lookup(each.value, "attach_lb_log_delivery_policy", false)
89
attach_deny_insecure_transport_policy = lookup(each.value, "attach_deny_insecure_transport_policy", false)
910
attach_policy = lookup(each.value, "attach_policy", false)
1011
attach_public_policy = lookup(each.value, "attach_public_policy", true)

0 commit comments

Comments
 (0)