-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
209 lines (175 loc) · 4.7 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
locals {
subnet_cidr_blocks = [for cidr_block in cidrsubnets("${var.cidr_base}/16", 4,4) : cidrsubnets(cidr_block, 4, 4, 4 ,4)]
}
#
# Data
#
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-ebs"]
}
}
#
# VPC
#
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "testing-alb-cognito-auth"
cidr = "${var.cidr_base}/16"
azs = ["${var.aws_region}a", "${var.aws_region}b", "${var.aws_region}c", "${var.aws_region}d"]
public_subnets = local.subnet_cidr_blocks[0]
#private_subnets = local.subnet_cidr_blocks[1]
#enable_nat_gateway = true
#single_nat_gateway = true
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Terraform = "true"
Environment = "dev"
}
}
#
# Security Groups
#
resource "aws_security_group" "allow_http_from_public_subnet" {
name = "allow_http_from_public_subnet"
description = "Allow HTTP traffic from all public subnets"
vpc_id = module.vpc.vpc_id
ingress {
description = "HTTP from public Subnets"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = module.vpc.public_subnets_cidr_blocks
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "allow_http_https_from_world" {
name = "allow_http_https_from_world"
description = "Allow HTTP(s) Traffic from the world"
vpc_id = module.vpc.vpc_id
ingress {
description = "HTTP from public Subnets"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTP from public Subnets"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "allow_ssh_from_world" {
name = "allow_ssh_from_world"
description = "Allow SSH Traffic from the world"
vpc_id = module.vpc.vpc_id
ingress {
description = "SSH from world"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
#
#Launch configuration and autoscaling group
#
module "asg" {
source = "terraform-aws-modules/autoscaling/aws"
name = "asg-elb-demo"
# Launch configuration
lc_name = "asg-elb-demo-lc"
image_id = data.aws_ami.amazon_linux.id
instance_type = "t2.micro"
security_groups = [aws_security_group.allow_http_from_public_subnet.id, aws_security_group.allow_ssh_from_world.id]
key_name = "rob-lk"
# Auto scaling group
asg_name = "asg-elb-demo"
vpc_zone_identifier = module.vpc.public_subnets
health_check_type = "EC2"
min_size = 0
max_size = 1
desired_capacity = 1
wait_for_capacity_timeout = 0
tags = [
{
key = "Environment"
value = "dev"
propagate_at_launch = true
},
{
key = "Project"
value = "megasecret"
propagate_at_launch = true
},
]
# Run commands at launch
user_data = <<-EOF
#!/bin/bash
sudo su
yum -y install httpd
mkdir -p /tmp/asg_demo/
wget https://asg-demo-20210318.s3.amazonaws.com/asg_demo.tar.gz -P /tmp/asg_demo/
tar -zxvf /tmp/asg_demo/asg_demo.tar.gz -C /tmp/asg_demo/
rm -f /tmp/asg_demo/asg_demo.tar.gz
mv /tmp/asg_demo/* /var/www/html/
systemctl enable httpd
systemctl start httpd
EOF
}
######
# ALB
######
module "alb" {
source = "terraform-aws-modules/alb/aws"
name = "my-alb"
load_balancer_type = "application"
vpc_id = module.vpc.vpc_id
subnets = module.vpc.public_subnets
security_groups = [aws_security_group.allow_http_https_from_world.id]
target_groups = [
{
name_prefix = "pref-"
backend_protocol = "HTTP"
backend_port = 80
target_type = "instance"
}
]
http_tcp_listeners = [
{
port = 80
protocol = "HTTP"
target_group_index = 0
}
]
tags = {
Environment = "Test"
}
}
# Create a new ALB Target Group attachment
resource "aws_autoscaling_attachment" "asg_attachment" {
autoscaling_group_name = module.asg.this_autoscaling_group_name
alb_target_group_arn = module.alb.target_group_arns[0]
}