Skip to content

Commit 60f378e

Browse files
authored
Improve module versions + add alb access logs example (#3)
1 parent 0c22da0 commit 60f378e

File tree

5 files changed

+164
-9
lines changed

5 files changed

+164
-9
lines changed

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v2.5.0
3+
rev: v3.2.0
44
hooks:
55
- id: check-added-large-files
66
args: ['--maxkb=500']
@@ -18,7 +18,7 @@ repos:
1818
args: ['--allow-missing-credentials']
1919
- id: trailing-whitespace
2020
- repo: git://github.com/antonbabenko/pre-commit-terraform
21-
rev: v1.29.0
21+
rev: v1.31.0
2222
hooks:
2323
- id: terraform_fmt
2424
- id: terraform_docs

README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Terraform 0.12. Pin module version to `~> v1.0`. Submit pull-requests to `master
1818
```hcl
1919
module "alb" {
2020
source = "umotif-public/alb/aws"
21-
version = "~> 1.2.0"
21+
version = "~> 1.2.1"
2222
2323
name_prefix = "complete-alb"
2424
@@ -43,7 +43,7 @@ module "alb" {
4343
```hcl
4444
module "nlb" {
4545
source = "umotif-public/alb/aws"
46-
version = "~> 1.2.0"
46+
version = "~> 1.2.1"
4747
4848
name = "complete-nlb"
4949
@@ -70,6 +70,8 @@ Module is to be used with Terraform > 0.12.
7070
## Examples
7171

7272
* [Application Load Balancer ALB](https://github.com/umotif-public/terraform-aws-alb/tree/master/examples/alb)
73+
* [Application Load Balancer ALB with S3 access logs](https://github.com/umotif-public/terraform-aws-alb/tree/master/examples/alb-with-s3-access-logs)
74+
* [Application Load Balancer NLB](https://github.com/umotif-public/terraform-aws-alb/tree/master/examples/nlb)
7375

7476
## Authors
7577

@@ -80,14 +82,14 @@ Module managed by [Marcin Cuber](https://github.com/marcincuber) [linkedin](http
8082

8183
| Name | Version |
8284
|------|---------|
83-
| terraform | ~> 0.12.6 |
84-
| aws | ~> 2.45 |
85+
| terraform | >= 0.12.6, < 0.14 |
86+
| aws | >= 2.45, < 4.0 |
8587

8688
## Providers
8789

8890
| Name | Version |
8991
|------|---------|
90-
| aws | ~> 2.45 |
92+
| aws | >= 2.45, < 4.0 |
9193

9294
## Inputs
9395

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "aws_availability_zones" "available" {}
2+
data "aws_region" "current" {}
3+
data "aws_caller_identity" "current" {}
+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
}
4+
5+
#####
6+
# VPC and subnets
7+
#####
8+
module "vpc" {
9+
source = "terraform-aws-modules/vpc/aws"
10+
version = "2.44.0"
11+
12+
name = "simple-vpc"
13+
14+
cidr = "10.0.0.0/16"
15+
16+
azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
17+
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
18+
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
19+
20+
enable_nat_gateway = false
21+
22+
tags = {
23+
Environment = "test"
24+
}
25+
}
26+
27+
#####
28+
# Application Load Balancer
29+
#####
30+
module "alb" {
31+
source = "../../"
32+
33+
name_prefix = "example-with-access-logs"
34+
35+
load_balancer_type = "application"
36+
37+
internal = false
38+
vpc_id = module.vpc.vpc_id
39+
subnets = flatten([module.vpc.public_subnets])
40+
41+
enable_http_to_https_redirect = true
42+
cidr_blocks_redirect = ["10.10.0.0/16"]
43+
44+
access_logs = {
45+
bucket = aws_s3_bucket.alb_access_logs.bucket
46+
prefix = "example-with-access-logs-alb"
47+
enabled = true
48+
}
49+
50+
tags = {
51+
Project = "Test"
52+
}
53+
}
54+
55+
#####
56+
# ALB listener
57+
#####
58+
resource "aws_lb_listener" "alb_80_redirect_to_443" {
59+
load_balancer_arn = module.alb.arn
60+
port = "80"
61+
protocol = "HTTP"
62+
63+
default_action {
64+
type = "redirect"
65+
66+
redirect {
67+
port = "443"
68+
protocol = "HTTPS"
69+
status_code = "HTTP_301"
70+
}
71+
}
72+
}
73+
74+
#####
75+
# SGs
76+
#####
77+
resource "aws_security_group_rule" "alb_ingress_443" {
78+
security_group_id = module.alb.security_group_id
79+
type = "ingress"
80+
protocol = "tcp"
81+
from_port = 443
82+
to_port = 443
83+
cidr_blocks = ["0.0.0.0/0"]
84+
ipv6_cidr_blocks = ["::/0"]
85+
}
86+
87+
#####
88+
# S3 bucket storing ALB access logs
89+
#####
90+
locals {
91+
alb_root_account_id = "156460612806" # valid account id for Ireland Region. Full list -> https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html
92+
}
93+
94+
resource "aws_s3_bucket" "alb_access_logs" {
95+
bucket = "example-alb-access-logs-bucket"
96+
acl = "private"
97+
region = data.aws_region.current.name
98+
99+
server_side_encryption_configuration {
100+
rule {
101+
apply_server_side_encryption_by_default {
102+
sse_algorithm = "AES256"
103+
}
104+
}
105+
}
106+
107+
policy = <<POLICY
108+
{
109+
"Version": "2012-10-17",
110+
"Statement": [
111+
{
112+
"Sid": "AllowELBRootAccount",
113+
"Effect": "Allow",
114+
"Principal": {
115+
"AWS": "arn:aws:iam::${local.alb_root_account_id}:root"
116+
},
117+
"Action": "s3:PutObject",
118+
"Resource": "arn:aws:s3:::example-alb-access-logs-bucket/*"
119+
},
120+
{
121+
"Sid": "AWSLogDeliveryWrite",
122+
"Effect": "Allow",
123+
"Principal": {
124+
"Service": "delivery.logs.amazonaws.com"
125+
},
126+
"Action": "s3:PutObject",
127+
"Resource": "arn:aws:s3:::example-alb-access-logs-bucket/*",
128+
"Condition": {
129+
"StringEquals": {
130+
"s3:x-amz-acl": "bucket-owner-full-control"
131+
}
132+
}
133+
},
134+
{
135+
"Sid": "AWSLogDeliveryAclCheck",
136+
"Effect": "Allow",
137+
"Principal": {
138+
"Service": "delivery.logs.amazonaws.com"
139+
},
140+
"Action": "s3:GetBucketAcl",
141+
"Resource": "arn:aws:s3:::example-alb-access-logs-bucket"
142+
}
143+
]
144+
}
145+
POLICY
146+
147+
tags = {
148+
Environment = "test"
149+
}
150+
}

versions.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
2-
required_version = "~> 0.12.6"
2+
required_version = ">= 0.12.6, < 0.14"
33

44
required_providers {
5-
aws = "~> 2.45"
5+
aws = ">= 2.45, < 4.0"
66
}
77
}

0 commit comments

Comments
 (0)