Skip to content

Commit cac6f0f

Browse files
committed
Add access logging for load-balancer
1 parent 173b2c0 commit cac6f0f

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

ecs-cluster/keycloak.tf

+62
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,62 @@ resource "aws_security_group" "ecs-task-keycloak" {
8484
}
8585
}
8686

87+
# Load balancer logs
88+
resource "aws_s3_bucket" "alb-logs" {
89+
bucket_prefix = "${var.name}-logs-"
90+
}
91+
92+
data "aws_iam_policy_document" "alb-logs" {
93+
statement {
94+
principals {
95+
type = var.loadbalancer-logging-iam-principal.type
96+
identifiers = [var.loadbalancer-logging-iam-principal.identifier]
97+
}
98+
actions = [
99+
"s3:PutObject"
100+
]
101+
resources = [
102+
"${aws_s3_bucket.alb-logs.arn}/access-logs/*"
103+
]
104+
}
105+
}
106+
107+
resource "aws_s3_bucket_policy" "alb-logs" {
108+
bucket = aws_s3_bucket.alb-logs.id
109+
policy = data.aws_iam_policy_document.alb-logs.json
110+
}
111+
112+
resource "aws_s3_bucket_server_side_encryption_configuration" "alb-logs-encryption" {
113+
bucket = aws_s3_bucket.alb-logs.id
114+
115+
rule {
116+
apply_server_side_encryption_by_default {
117+
sse_algorithm = "AES256"
118+
}
119+
}
120+
}
121+
122+
resource "aws_s3_bucket_versioning" "alb-logs" {
123+
bucket = aws_s3_bucket.alb-logs.id
124+
versioning_configuration {
125+
status = "Enabled"
126+
}
127+
}
128+
129+
resource "aws_s3_bucket_lifecycle_configuration" "alb-logs" {
130+
bucket = aws_s3_bucket.alb-logs.id
131+
rule {
132+
id = "delete-access-logs-${var.expire-access-logs-days}-days"
133+
filter {
134+
prefix = "access-logs/"
135+
}
136+
expiration {
137+
days = var.expire-access-logs-days
138+
}
139+
status = "Enabled"
140+
}
141+
}
142+
87143
# Load balancer
88144

89145
resource "aws_lb" "keycloak" {
@@ -96,6 +152,12 @@ resource "aws_lb" "keycloak" {
96152
enable_deletion_protection = true
97153

98154
preserve_host_header = true
155+
156+
access_logs {
157+
bucket = aws_s3_bucket.alb-logs.id
158+
prefix = "access-logs"
159+
enabled = true
160+
}
99161
}
100162

101163
resource "aws_alb_target_group" "keycloak" {

ecs-cluster/variables.tf

+18
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ variable "desired-count" {
8787
default = 1
8888
}
8989

90+
variable "loadbalancer-logging-iam-principal" {
91+
type = map(string)
92+
description = "IAM principal type and identifier for the elastic load balancer logger. This is complicated, see https://docs.aws.amazon.com/elasticloadbalancing/latest/application/enable-access-logging.html#attach-bucket-policy"
93+
94+
# For eu-west-2 this is a hard-coded AWS account ID belonging to AWS
95+
# and not Service: logdelivery.elasticloadbalancing.amazonaws.com
96+
default = {
97+
type = "AWS"
98+
identifier = "arn:aws:iam::652711504416:root"
99+
}
100+
}
101+
102+
variable "expire-access-logs-days" {
103+
type = number
104+
description = "Automatically delete access logs after this number of days"
105+
default = 3653
106+
}
107+
90108
variable "default-tags" {
91109
type = map(any)
92110
default = {

0 commit comments

Comments
 (0)