Skip to content

Commit 5564e56

Browse files
committed
Enable access and connection logs for AWS ALB module
1 parent e53a549 commit 5564e56

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

modules/alb/main.tf

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
11
resource "aws_alb" "this" {
22
name = var.name
33
security_groups = [aws_security_group.this.id]
4-
subnets = var.subnet_ids
5-
tags = var.tags
4+
5+
dynamic "connection_logs" {
6+
for_each = var.enable_connection_logs ? [1] : []
7+
content {
8+
bucket = var.s3_bucket_name != "" ? var.s3_bucket_name : aws_s3_bucket.lb_logs[0].id
9+
prefix = "connectionlogs"
10+
enabled = true
11+
}
12+
}
13+
14+
dynamic "access_logs" {
15+
for_each = var.enable_access_logs ? [1] : []
16+
content {
17+
bucket = var.s3_bucket_name != "" ? var.s3_bucket_name : aws_s3_bucket.lb_logs[0].id
18+
prefix = "accesslogs"
19+
enabled = true
20+
}
21+
}
22+
subnets = var.subnet_ids
23+
tags = var.tags
24+
}
25+
26+
resource "aws_s3_bucket" "lb_logs" {
27+
count = var.s3_bucket_name == "" ? 1 : 0
28+
bucket = var.s3_bucket_name == "" ? "${var.name}-alb-logs-${random_id.suffix.hex}" : var.s3_bucket_name
29+
}
30+
31+
resource "random_id" "suffix" {
32+
byte_length = 4
633
}
734

835
resource "aws_security_group" "this" {

modules/alb/variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,29 @@ variable "description" {
33
type = string
44
}
55

6+
variable "enable_access_logs" {
7+
type = bool
8+
default = false
9+
description = "Enable or disable ALB access logs. If set to true, logs will be stored in an S3 bucket."
10+
}
11+
12+
variable "enable_connection_logs" {
13+
type = bool
14+
default = false
15+
description = "Enable or disable ALB connection logs. If set to true, logs will be stored in an S3 bucket."
16+
}
17+
618
variable "name" {
719
description = "Name for this load balancer"
820
type = string
921
}
1022

23+
variable "s3_bucket_name" {
24+
type = string
25+
default = ""
26+
description = "Optional S3 bucket name for storing ALB access logs. If not provided, a new bucket will be created."
27+
}
28+
1129
variable "security_group_name" {
1230
type = string
1331
description = "Name for the load balancer security group; defaults to name"

0 commit comments

Comments
 (0)