-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
executable file
·233 lines (206 loc) · 5.17 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
228
229
230
231
232
233
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.10.0"
}
github = {
source = "integrations/github"
version = "4.23.0"
}
}
}
provider "aws" {
region = var.region
}
provider "github" {
token = file("${var.github_token_path}/${var.github_token_filename}")
}
##########
# GitHub #
##########
resource "github_repository_file" "dbendpoint" {
content = aws_db_instance.db_server.address
file = "dbserver.endpoint"
repository = var.github_repo_name
branch = var.github_repo_branch
overwrite_on_create = true
depends_on = [
aws_db_instance.db_server
]
}
################
# VPC & Subnet #
################
data "aws_vpc" "default_vpc" {
default = true
}
data "aws_subnets" "subnets" {
filter {
name = "vpc-id"
values = [data.aws_vpc.default_vpc.id]
}
}
#############
# DB Server #
#############
resource "aws_security_group" "db_sg" {
name = "DBSecurityGroup"
vpc_id = data.aws_vpc.default_vpc.id
tags = {
"Name" = "DBSecurityGroup"
}
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
security_groups = [aws_security_group.lt_sg.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_db_instance" "db_server" {
instance_class = "db.t2.micro"
allocated_storage = 20 # GB
vpc_security_group_ids = [aws_security_group.db_sg.id]
allow_major_version_upgrade = false
auto_minor_version_upgrade = true
backup_retention_period = 0
identifier = "${var.prefix}-db"
db_name = var.db_name
engine = "mysql"
engine_version = "8.0.28"
username = var.db_username
password = var.db_password
monitoring_interval = 0
multi_az = false
port = 3306
publicly_accessible = false
skip_final_snapshot = true
}
###################
# Launch Template #
###################
data "aws_ami" "amazon_linux_2" {
owners = ["amazon"]
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-kernel-5.10*"]
}
}
resource "aws_security_group" "lt_sg" {
name = "LTSecurityGroup"
vpc_id = data.aws_vpc.default_vpc.id
tags = {
Name = "LTSecurityGroup"
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
security_groups = [aws_security_group.alb_sg.id]
}
ingress {
from_port = 22
to_port = 22
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_launch_template" "lt" {
name = "${var.prefix}-lt"
image_id = data.aws_ami.amazon_linux_2.id
instance_type = "t2.micro"
key_name = var.ssh_key_name
vpc_security_group_ids = [aws_security_group.lt_sg.id]
user_data = base64encode(templatefile("${path.module}/user-data.sh", {
repo_name = var.github_repo_name
} ))
depends_on = [
github_repository_file.dbendpoint
]
tag_specifications {
resource_type = "instance"
tags = {
Name = var.prefix
}
}
}
#############################
# Application Load Balancer #
#############################
resource "aws_alb_target_group" "alb_tg" {
name = "${var.prefix}-alb-tg"
port = 80
protocol = "HTTP"
vpc_id = data.aws_vpc.default_vpc.id
target_type = "instance"
health_check {
healthy_threshold = 2
unhealthy_threshold = 3
}
}
resource "aws_security_group" "alb_sg" {
name = "ALBSecurityGroup"
vpc_id = data.aws_vpc.default_vpc.id
tags = {
Name = "ALBSecurityGroup"
}
ingress {
from_port = 80
to_port = 80
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_alb_listener" "alb_listener" {
load_balancer_arn = aws_alb.alb.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_alb_target_group.alb_tg.arn
}
}
resource "aws_alb" "alb" {
name = "${var.prefix}-alb"
ip_address_type = "ipv4"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.alb_sg.id]
subnets = data.aws_subnets.subnets.ids
}
######################
# Auto Scaling Group #
######################
resource "aws_autoscaling_group" "asg" {
max_size = 3
min_size = 1
desired_capacity = 2
name = "${var.prefix}-asg"
health_check_grace_period = 300
health_check_type = "ELB"
target_group_arns = [aws_alb_target_group.alb_tg.arn]
vpc_zone_identifier = aws_alb.alb.subnets
launch_template {
id = aws_launch_template.lt.id
version = aws_launch_template.lt.latest_version
}
}