Skip to content

Commit 3c094b3

Browse files
authored
fix: Fixed ELB log delivery policy for old and new regions (#219)
1 parent cc34d1d commit 3c094b3

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ No modules.
155155
| [aws_s3_bucket_website_configuration.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_website_configuration) | resource |
156156
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
157157
| [aws_canonical_user_id.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/canonical_user_id) | data source |
158-
| [aws_elb_service_account.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/elb_service_account) | data source |
159158
| [aws_iam_policy_document.combined](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
160159
| [aws_iam_policy_document.deny_insecure_transport](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
161160
| [aws_iam_policy_document.elb_log_delivery](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
162161
| [aws_iam_policy_document.inventory_and_analytics_destination_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
163162
| [aws_iam_policy_document.lb_log_delivery](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
164163
| [aws_iam_policy_document.require_latest_tls](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
164+
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
165165

166166
## Inputs
167167

examples/complete/main.tf

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ locals {
1313
region = "eu-west-1"
1414
}
1515

16-
1716
data "aws_caller_identity" "current" {}
1817

1918
data "aws_canonical_user_id" "current" {}

main.tf

+47-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
data "aws_region" "current" {}
2+
13
data "aws_canonical_user_id" "this" {}
24

35
data "aws_caller_identity" "current" {}
@@ -527,31 +529,61 @@ data "aws_iam_policy_document" "combined" {
527529
}
528530

529531
# AWS Load Balancer access log delivery policy
530-
data "aws_elb_service_account" "this" {
531-
count = local.create_bucket && var.attach_elb_log_delivery_policy ? 1 : 0
532+
locals {
533+
# List of AWS regions where permissions should be granted to the specified Elastic Load Balancing account ID ( https://docs.aws.amazon.com/elasticloadbalancing/latest/application/enable-access-logging.html#attach-bucket-policy )
534+
elb_service_accounts = {
535+
us-east-1 = "127311923021"
536+
us-east-2 = "033677994240"
537+
us-west-1 = "027434742980"
538+
us-west-2 = "797873946194"
539+
af-south-1 = "098369216593"
540+
ap-east-1 = "754344448648"
541+
ap-south-1 = "718504428378"
542+
ap-northeast-1 = "582318560864"
543+
ap-northeast-2 = "600734575887"
544+
ap-northeast-3 = "383597477331"
545+
ap-southeast-1 = "114774131450"
546+
ap-southeast-2 = "783225319266"
547+
ap-southeast-3 = "589379963580"
548+
ca-central-1 = "985666609251"
549+
eu-central-1 = "054676820928"
550+
eu-west-1 = "156460612806"
551+
eu-west-2 = "652711504416"
552+
eu-west-3 = "009996457667"
553+
eu-south-1 = "635631232127"
554+
eu-north-1 = "897822967062"
555+
me-south-1 = "076674570225"
556+
sa-east-1 = "507241528517"
557+
us-gov-west-1 = "048591011584"
558+
us-gov-east-1 = "190560391635"
559+
}
532560
}
533561

534562
data "aws_iam_policy_document" "elb_log_delivery" {
535563
count = local.create_bucket && var.attach_elb_log_delivery_policy ? 1 : 0
536564

537565
# Policy for AWS Regions created before August 2022 (e.g. US East (N. Virginia), Asia Pacific (Singapore), Asia Pacific (Sydney), Asia Pacific (Tokyo), Europe (Ireland))
538-
statement {
539-
sid = ""
566+
dynamic "statement" {
567+
for_each = { for k, v in local.elb_service_accounts : k => v if k == data.aws_region.current.name }
540568

541-
principals {
542-
type = "AWS"
543-
identifiers = data.aws_elb_service_account.this[*].arn
544-
}
569+
content {
570+
sid = format("ELBRegion%s", title(statement.key))
545571

546-
effect = "Allow"
572+
principals {
573+
type = "AWS"
574+
identifiers = [format("arn:aws:iam::%s:root", statement.value)]
575+
}
547576

548-
actions = [
549-
"s3:PutObject",
550-
]
577+
effect = "Allow"
551578

552-
resources = [
553-
"${aws_s3_bucket.this[0].arn}/*",
554-
]
579+
actions = [
580+
"s3:PutObject",
581+
]
582+
583+
resources = [
584+
"${aws_s3_bucket.this[0].arn}/*",
585+
]
586+
}
555587
}
556588

557589
# Policy for AWS Regions created after August 2022 (e.g. Asia Pacific (Hyderabad), Asia Pacific (Melbourne), Europe (Spain), Europe (Zurich), Middle East (UAE))
@@ -576,7 +608,6 @@ data "aws_iam_policy_document" "elb_log_delivery" {
576608
}
577609

578610
# ALB/NLB
579-
580611
data "aws_iam_policy_document" "lb_log_delivery" {
581612
count = local.create_bucket && var.attach_lb_log_delivery_policy ? 1 : 0
582613

0 commit comments

Comments
 (0)