Skip to content

Commit

Permalink
configure alb logs with a dedicated variable
Browse files Browse the repository at this point in the history
  • Loading branch information
tkjwa committed Mar 21, 2023
1 parent cb99cfb commit 5022e87
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
14 changes: 7 additions & 7 deletions alb-logs-bucket.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
resource "aws_s3_bucket" "alb_logs_bucket" {
count = var.production ? 1 : 0
count = var.alb_logs ? 1 : 0

bucket = "${var.env_name}-alb-logs"
force_destroy = true
force_destroy = var.production ? false : true
}

resource "aws_s3_bucket_server_side_encryption_configuration" "alb_logs_bucket_encryption" {
count = var.production ? 1 : 0
count = var.alb_logs ? 1 : 0

bucket = aws_s3_bucket.alb_logs_bucket[0].id
expected_bucket_owner = local.account_id
Expand All @@ -18,7 +18,7 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "alb_logs_bucket_e
}

resource "aws_s3_bucket_public_access_block" "alb_logs_bucket_pab" {
count = var.production ? 1 : 0
count = var.alb_logs ? 1 : 0

bucket = aws_s3_bucket.alb_logs_bucket[0].id
block_public_acls = true
Expand All @@ -28,15 +28,15 @@ resource "aws_s3_bucket_public_access_block" "alb_logs_bucket_pab" {
}

resource "aws_s3_bucket_policy" "alb_logs_bucket_policy" {
count = var.production ? 1 : 0
count = var.alb_logs ? 1 : 0

bucket = aws_s3_bucket.alb_logs_bucket[0].id
policy = data.aws_iam_policy_document.alb_logs_bucket_policy[0].json
}

# see https://docs.aws.amazon.com/elasticloadbalancing/latest/application/enable-access-logging.html#attach-bucket-policy
data "aws_iam_policy_document" "alb_logs_bucket_policy" {
count = var.production ? 1 : 0
count = var.alb_logs ? 1 : 0

statement {
sid = "ALBAllowWriteLogs"
Expand All @@ -52,7 +52,7 @@ data "aws_iam_policy_document" "alb_logs_bucket_policy" {
# Versioning should not be needed as ALB will never update or overwrite files
# but it allows to ensure that the log files have not been altered
resource "aws_s3_bucket_versioning" "alb_logs_bucket_versioning" {
count = var.production ? 1 : 0
count = var.alb_logs ? 1 : 0

bucket = aws_s3_bucket.alb_logs_bucket[0].id
expected_bucket_owner = local.account_id
Expand Down
2 changes: 1 addition & 1 deletion settings.tf
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ locals {
name = "SSLCertificateArns"
value = aws_acm_certificate.certificate.arn
},
var.production ? {
var.alb_logs ? {
namespace = "aws:elbv2:loadbalancer"
name = "AccessLogsS3Bucket"
value = aws_s3_bucket.alb_logs_bucket[0].id
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ variable "ha" {
default = false
}

variable "alb_logs" {
type = bool
default = false
description = "Enable the ALB Logs in S3, stored in a dedicated bucket"
}

variable "beanstalk_env_vars" {
type = list(object({
name: string,
Expand Down

0 comments on commit 5022e87

Please sign in to comment.