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

feature: NLB Log Delivery Support #95

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ These features of S3 bucket configurations are supported:
- object locking
- Cross-Region Replication (CRR)
- ELB log delivery bucket policy
- NLB log delivery bucket policy

## Usage

Expand Down Expand Up @@ -48,6 +49,22 @@ module "s3_bucket_for_logs" {
}
```

### Bucket with NLB access log delivery policy attached

```hcl
module "s3_bucket_for_logs" {
source = "terraform-aws-modules/s3-bucket/aws"

bucket = "my-s3-bucket-for-logs"
acl = "log-delivery-write"

# Allow deletion of non-empty bucket
force_destroy = true

attach_nlb_log_delivery_policy = true
}
```

## Conditional creation

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`.
Expand Down Expand Up @@ -122,6 +139,7 @@ No modules.
| <a name="input_acl"></a> [acl](#input\_acl) | (Optional) The canned ACL to apply. Defaults to 'private'. Conflicts with `grant` | `string` | `"private"` | no |
| <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 |
| <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 |
| <a name="input_attach_nlb_log_delivery_policy"></a> [attach\_nlb\_log\_delivery\_policy](#input\_attach\_elb\_log\_delivery\_policy) | Controls if S3 bucket should have NLB log delivery policy attached | `bool` | `false` | no |
| <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 |
| <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 |
| <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 |
Expand Down
54 changes: 53 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
locals {
attach_policy = var.attach_elb_log_delivery_policy || var.attach_deny_insecure_transport_policy || var.attach_policy
attach_policy = var.attach_elb_log_delivery_policy || var.attach_nlb_log_delivery_policy || var.attach_deny_insecure_transport_policy || var.attach_policy
}

resource "aws_s3_bucket" "this" {
Expand Down Expand Up @@ -247,6 +247,7 @@ data "aws_iam_policy_document" "combined" {

source_policy_documents = compact([
var.attach_elb_log_delivery_policy ? data.aws_iam_policy_document.elb_log_delivery[0].json : "",
var.attach_nlb_log_delivery_policy ? data.aws_iam_policy_document.nlb_log_delivery[0].json : "",
var.attach_deny_insecure_transport_policy ? data.aws_iam_policy_document.deny_insecure_transport[0].json : "",
var.attach_policy ? var.policy : ""
])
Expand Down Expand Up @@ -280,6 +281,57 @@ data "aws_iam_policy_document" "elb_log_delivery" {
}
}

# NLB

data "aws_iam_policy_document" "nlb_log_delivery" {
count = var.create_bucket && var.attach_nlb_log_delivery_policy ? 1 : 0

statement {
sid = "AWSLogDeliveryWrite"

principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}

effect = "Allow"

actions = [
"s3:PutObject",
]

resources = [
"${aws_s3_bucket.this[0].arn}/*",
]

condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}

statement {
sid = "AWSLogDeliveryAclCheck"

effect = "Allow"

principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}

actions = [
"s3:GetBucketAcl",
]

resources = [
"${aws_s3_bucket.this[0].arn}",
]

}
}

data "aws_iam_policy_document" "deny_insecure_transport" {
count = var.create_bucket && var.attach_deny_insecure_transport_policy ? 1 : 0

Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ variable "attach_elb_log_delivery_policy" {
default = false
}

variable "attach_nlb_log_delivery_policy" {
description = "Controls if S3 bucket should have NLB log delivery policy attached"
type = bool
default = false
}

variable "attach_deny_insecure_transport_policy" {
description = "Controls if S3 bucket should have deny non-SSL transport policy attached"
type = bool
Expand Down