-
Notifications
You must be signed in to change notification settings - Fork 15
/
alb.tf
145 lines (121 loc) · 3.22 KB
/
alb.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
resource "aws_lb" "this" {
name_prefix = "mb-"
security_groups = ["${aws_security_group.alb.id}"]
subnets = tolist(var.public_subnet_ids)
tags = var.tags
access_logs {
bucket = aws_s3_bucket.this.bucket
enabled = true
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_lb_listener" "http" {
load_balancer_arn = aws_lb.this.arn
depends_on = [aws_lb.this] # https://github.com/terraform-providers/terraform-provider-aws/issues/9976
port = "80"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_lb_listener" "https" {
load_balancer_arn = aws_lb.this.arn
depends_on = [aws_lb.this] # https://github.com/terraform-providers/terraform-provider-aws/issues/9976
port = "443"
protocol = "HTTPS"
ssl_policy = var.ssl_policy
certificate_arn = var.certificate_arn
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "No valid routing rule"
status_code = "400"
}
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_s3_bucket" "this" {
bucket_prefix = "mb-"
acl = "private"
force_destroy = ! var.protection
tags = var.tags
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
lifecycle_rule {
enabled = true
expiration {
days = var.log_retention
}
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_s3_bucket_policy" "this" {
bucket = aws_s3_bucket.this.id
policy = data.aws_iam_policy_document.s3.json
}
data "aws_elb_service_account" "this" {}
data "aws_iam_policy_document" "s3" {
statement {
actions = ["s3:PutObject"]
resources = ["${aws_s3_bucket.this.arn}/*"]
principals {
type = "AWS"
identifiers = [data.aws_elb_service_account.this.arn]
}
}
}
resource "aws_security_group" "alb" {
name_prefix = "${var.id}-alb-"
vpc_id = var.vpc_id
tags = var.tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "alb_egress_ecs" {
description = "ECS"
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
security_group_id = aws_security_group.alb.id
source_security_group_id = aws_security_group.ecs.id
}
resource "aws_security_group_rule" "alb_ingress_http" {
description = "Internet"
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
security_group_id = aws_security_group.alb.id
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_security_group_rule" "alb_ingress_https" {
description = "Internet"
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
security_group_id = aws_security_group.alb.id
cidr_blocks = ["0.0.0.0/0"]
}