-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
227 lines (185 loc) · 5.81 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Use terraform to create the following
# - EC2 hosting a plain vanilla apache website
# - Autoscaling Group under which the EC2 will run
# - Load balancer public facing
# https://twitter.com/fesshole/status/1451117312673714185
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0" # TODO: tighten version?
}
}
# semantic versioning must count for something, right?
required_version = ">= 1.0"
}
provider "aws" {
profile = "default"
region = "ap-southeast-2"
}
#################
resource "aws_vpc" "ex_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true # stuff not in DNS is the worst.
tags = {
Name = "example VPC"
}
}
# subnets for backends
resource "aws_subnet" "ex_public_apse2a" {
vpc_id = aws_vpc.ex_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "ap-southeast-2a"
tags = {
Name = "example subnet (a)"
}
}
resource "aws_subnet" "ex_public_apse2b" {
vpc_id = aws_vpc.ex_vpc.id
cidr_block = "10.0.2.0/24"
availability_zone = "ap-southeast-2b"
tags = {
Name = "example subnet (b)"
}
}
resource "aws_internet_gateway" "ex_igw" {
vpc_id = aws_vpc.ex_vpc.id
tags = {
Name = "example GW"
}
}
resource "aws_route_table" "ex_routes" {
vpc_id = aws_vpc.ex_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.ex_igw.id
}
tags = {
Name = "example route table"
}
}
resource "aws_route_table_association" "ex_route_assoc_apse2a" {
subnet_id = aws_subnet.ex_public_apse2a.id
route_table_id = aws_route_table.ex_routes.id
}
resource "aws_route_table_association" "ex_route_assoc_apse2b" {
subnet_id = aws_subnet.ex_public_apse2b.id
route_table_id = aws_route_table.ex_routes.id
}
resource "aws_security_group" "ex_webserver" {
name = "ex-webserver"
description = "network security policy for webserver backends (allow inbound SSH & HTTP)"
vpc_id = aws_vpc.ex_vpc.id
# let SSH in (we would definitely not do this in the real world!)
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# and HTTP too (probably wouldn't do this in the real world either)
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# let it all out
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "example webserver SG"
}
}
resource "aws_launch_configuration" "ex_webserver" {
name_prefix = "ex-web-"
image_id = "ami-0d2f34c92aa48cd95" # Debian 10 @sydney
instance_type = "t2.micro"
key_name = "miiichael"
security_groups = [aws_security_group.ex_webserver.id]
associate_public_ip_address = true
# should there be a 'set -e' in here somewhere? 🤔
user_data = <<-EOF
#! /bin/bash
sudo apt-get update
sudo apt-get install -y apache2
sudo systemctl start apache2
sudo systemctl enable apache2
echo "<h1>Deployed via Terraform</h1>" | sudo tee /var/www/html/index.html
EOF
lifecycle {
create_before_destroy = true
}
}
# I'm gonna pretend ALB doesn't exist yet (my brain can only take in so much...) so ELB it is.
resource "aws_security_group" "ex_elb_http" {
name = "ex-elb"
description = "network security policy for ELB frontend (allow inbound HTTP)"
vpc_id = aws_vpc.ex_vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# I guess this is to allow ELB to get to the backends?
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "example ELB frontend"
}
}
resource "aws_elb" "ex_frontend" {
name = "web-elb"
security_groups = [aws_security_group.ex_elb_http.id]
subnets = [aws_subnet.ex_public_apse2a.id, aws_subnet.ex_public_apse2a.id]
listener {
# SSL? What's that? <_< >_>
lb_port = 80
lb_protocol = "http"
instance_port = "80"
instance_protocol = "http"
}
}
resource "aws_autoscaling_group" "ex_frontent_asg" {
# this strikes me as a little bit sneaky. yet sensible.
name = "${aws_launch_configuration.ex_webserver.name}-asg"
min_size = 1
desired_capacity = 2
max_size = 4
health_check_type = "ELB"
load_balancers = [aws_elb.ex_frontend.id]
launch_configuration = aws_launch_configuration.ex_webserver.name
enabled_metrics = [
"GroupMinSize",
"GroupMaxSize",
"GroupDesiredCapacity",
"GroupInServiceInstances",
"GroupTotalInstances"
]
metrics_granularity = "1Minute"
vpc_zone_identifier = [
aws_subnet.ex_public_apse2a.id,
aws_subnet.ex_public_apse2b.id
]
lifecycle {
create_before_destroy = true
}
tag {
key = "Name"
value = "example webserver"
propagate_at_launch = true
}
}
########
output "elb_dns_name" {
value = aws_elb.ex_frontend.dns_name
}