Skip to content

Commit 600f9fc

Browse files
Added flexibility around logging
1 parent 21f7328 commit 600f9fc

File tree

7 files changed

+55
-33
lines changed

7 files changed

+55
-33
lines changed

CHANGELOG.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [1.0.4] - 2017-11-06
8+
#### Added
9+
* added `create_log_bucket` and `enable_logging` to help control logging more granularly.
10+
11+
#### Changed
12+
* existing related variables made more descriptive
13+
* S3 policy related test made more explicit (⭐ @antonbabenko)
14+
715
## [1.0.3] - 2017-10-19
8-
## Added
16+
#### Added
917
* TravisCI configuration added and now passing.
1018
* badge added to docs.
1119
* permissions section now in the example readme.
1220
* placeholder shell script added for CI deployment. Eventually this should conditionally release to the registry when those APIs become available.
1321

14-
## Changed
22+
#### Changed
1523
* altered tf variable `aws_region` to `region`.
1624
* replaced hardcoding the region to instead use a random region as retrieved by an awscli docker container within CI.
1725
* example cert is now a regionally-specific resource enabling tests to run in various regions at once and not collide.

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,18 @@ For an example of using ALB with ECS look no further than the [hashicorp example
3535
A full example leveraging other community modules is contained in the [examples/test_fixtures directory](examples/test_fixtures). Here's the gist of using it via the Terraform registry:
3636
```
3737
module "alb" {
38-
source = "terraform-aws-modules/alb/aws"
39-
alb_name = "my-alb"
40-
region = "us-east-2"
41-
alb_security_groups = ["sg-edcd9784", "sg-edcd9785"]
42-
vpc_id = "vpc-abcde012"
43-
subnets = ["subnet-abcde012", "subnet-bcde012a"]
44-
certificate_arn = "arn:aws:iam::123456789012:server-certificate/test_cert-123456789012"
45-
log_bucket = "logs-us-east-2-123456789012"
46-
log_prefix = "my-alb-logs"
47-
health_check_path = "/"
38+
source = "terraform-aws-modules/alb/aws"
39+
alb_name = "my-alb"
40+
region = "us-east-2"
41+
alb_security_groups = ["sg-edcd9784", "sg-edcd9785"]
42+
vpc_id = "vpc-abcde012"
43+
subnets = ["subnet-abcde012", "subnet-bcde012a"]
44+
certificate_arn = "arn:aws:iam::123456789012:server-certificate/test_cert-123456789012"
45+
create_log_bucket = true
46+
enable_logging = true
47+
log_bucket_name = "logs-us-east-2-123456789012"
48+
log_location_prefix = "my-alb-logs"
49+
health_check_path = "/"
4850
4951
tags {
5052
"Terraform" = "true"

examples/test_fixtures/main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ module "alb" {
4848
subnets = "${module.vpc.public_subnets}"
4949
certificate_arn = "${aws_iam_server_certificate.fixture_cert.arn}"
5050
health_check_path = "/"
51-
log_bucket = "logs-${var.region}-${data.aws_caller_identity.fixtures.account_id}"
52-
log_prefix = "${var.log_prefix}"
51+
create_log_bucket = true
52+
enable_logging = true
53+
log_bucket_name = "logs-${var.region}-${data.aws_caller_identity.fixtures.account_id}"
54+
log_location_prefix = "${var.log_location_prefix}"
5355
force_destroy_log_bucket = true
5456

5557
tags {

examples/test_fixtures/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
variable "log_prefix" {
1+
variable "log_location_prefix" {
22
default = "my-alb-logs"
33
}
44

main.tf

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ resource "aws_alb" "main" {
1313
tags = "${merge(var.tags, map("Name", format("%s", var.alb_name)))}"
1414

1515
access_logs {
16-
bucket = "${var.log_bucket}"
17-
prefix = "${var.log_prefix}"
18-
enabled = "${var.log_bucket != ""}"
16+
bucket = "${var.log_bucket_name}"
17+
prefix = "${var.log_location_prefix}"
18+
enabled = "${var.enable_logging}"
1919
}
2020
}
2121

@@ -28,7 +28,7 @@ data "aws_iam_policy_document" "bucket_policy" {
2828
]
2929

3030
resources = [
31-
"arn:aws:s3:::${var.log_bucket}/${var.log_prefix}/AWSLogs/${data.aws_caller_identity.current.account_id}/*",
31+
"arn:aws:s3:::${var.log_bucket_name}/${var.log_location_prefix}/AWSLogs/${data.aws_caller_identity.current.account_id}/*",
3232
]
3333

3434
principals {
@@ -39,11 +39,11 @@ data "aws_iam_policy_document" "bucket_policy" {
3939
}
4040

4141
resource "aws_s3_bucket" "log_bucket" {
42-
bucket = "${var.log_bucket}"
42+
bucket = "${var.log_bucket_name}"
4343
policy = "${var.bucket_policy == "" ? data.aws_iam_policy_document.bucket_policy.json : var.bucket_policy}"
4444
force_destroy = "${var.force_destroy_log_bucket}"
45-
count = "${var.log_bucket != "" ? 1 : 0}"
46-
tags = "${merge(var.tags, map("Name", format("%s", var.log_bucket)))}"
45+
count = "${var.create_log_bucket ? 1 : 0}"
46+
tags = "${merge(var.tags, map("Name", format("%s", var.log_bucket_name)))}"
4747
}
4848

4949
resource "aws_alb_target_group" "target_group" {
@@ -71,7 +71,7 @@ resource "aws_alb_target_group" "target_group" {
7171
tags = "${merge(var.tags, map("Name", format("%s-tg", var.alb_name)))}"
7272
}
7373

74-
resource "aws_alb_listener" "front_end_http" {
74+
resource "aws_alb_listener" "frontend_http" {
7575
load_balancer_arn = "${aws_alb.main.arn}"
7676
port = "80"
7777
protocol = "HTTP"
@@ -83,7 +83,7 @@ resource "aws_alb_listener" "front_end_http" {
8383
}
8484
}
8585

86-
resource "aws_alb_listener" "front_end_https" {
86+
resource "aws_alb_listener" "frontend_https" {
8787
load_balancer_arn = "${aws_alb.main.arn}"
8888
port = "443"
8989
protocol = "HTTPS"

test/integration/default/local_alb.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
require 'rhcl'
33

44
module_vars = Rhcl.parse(File.open('examples/test_fixtures/variables.tf'))
5-
log_prefix = module_vars['variable']['log_prefix']['default']
5+
log_location_prefix = module_vars['variable']['log_location_prefix']['default']
66
tf_state = JSON.parse(File.open('.kitchen/kitchen-terraform/default-aws/terraform.tfstate').read)
77
principal_account_id = tf_state['modules'][0]['outputs']['principal_account_id']['value']
88
account_id = tf_state['modules'][0]['outputs']['account_id']['value']
99
vpc_id = tf_state['modules'][0]['outputs']['vpc_id']['value']
1010
security_group_id = tf_state['modules'][0]['outputs']['sg_id']['value']
1111
account_id = tf_state['modules'][0]['outputs']['account_id']['value']
1212
# this must match the format in examples/test_fixtures/locals.tf
13-
log_bucket = 'logs-' + ENV['AWS_REGION'] + '-' + account_id
13+
log_bucket_name = 'logs-' + ENV['AWS_REGION'] + '-' + account_id
1414
# subnet_ids = tf_state['modules'][0]['outputs']['subnet_ids']['value']
1515

1616
describe alb('my-alb') do
@@ -34,9 +34,9 @@
3434
it { should belong_to_vpc('my-vpc') }
3535
end
3636

37-
describe s3_bucket(log_bucket) do
37+
describe s3_bucket(log_bucket_name) do
3838
it { should exist }
39-
it { should have_object("#{log_prefix}/AWSLogs/#{account_id}/ELBAccessLogTestFile") }
39+
it { should have_object("#{log_location_prefix}/AWSLogs/#{account_id}/ELBAccessLogTestFile") }
4040
it do
4141
should have_policy <<-POLICY
4242
{
@@ -49,7 +49,7 @@
4949
"AWS": "arn:aws:iam::#{principal_account_id}:root"
5050
},
5151
"Action": "s3:PutObject",
52-
"Resource": "arn:aws:s3:::#{log_bucket}/#{log_prefix}/AWSLogs/#{account_id}/*"
52+
"Resource": "arn:aws:s3:::#{log_bucket_name}/#{log_location_prefix}/AWSLogs/#{account_id}/*"
5353
}
5454
]
5555
}

variables.tf

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ variable "backend_protocol" {
3333
}
3434

3535
variable "bucket_policy" {
36-
description = "A custom S3 bucket policy to apply to the log bucket. If not provided, a minimal policy will be generated from other variables."
36+
description = "An S3 bucket policy to apply to the log bucket. If not provided, a minimal policy will be generated from other variables."
3737
default = ""
3838
}
3939

@@ -80,13 +80,23 @@ variable "health_check_unhealthy_threshold" {
8080
default = 3
8181
}
8282

83-
variable "log_bucket" {
83+
variable "create_log_bucket" {
84+
default = false
85+
description = "Create the S3 bucket (named with the log_bucket_name var) and attach a policy to allow ALB logging."
86+
}
87+
88+
variable "enable_logging" {
89+
default = false
90+
description = "Enable the ALB to write log entries to S3."
91+
}
92+
93+
variable "log_bucket_name" {
8494
description = "S3 bucket for storing ALB access logs. Setting this means the module will try to create the bucket."
8595
default = ""
8696
}
8797

88-
variable "log_prefix" {
89-
description = "S3 prefix within the log_bucket under which logs are stored."
98+
variable "log_location_prefix" {
99+
description = "S3 prefix within the log_bucket_name under which logs are stored."
90100
default = ""
91101
}
92102

0 commit comments

Comments
 (0)