-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
150 lines (120 loc) · 4.29 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
#-----------------------------------------------------------
# Provision Highly Available Web in any Region Default VPC
# Green/Blue Deployment
# Creat:
# - Security Group for web Server
# - Launch Configuration with Auto AMI Lookup
# - Auto Scaling Group using 2 Availability Zones
# - Classic Load Balancer in 2 Availability Zones
#
# Смотреть документацию на основном сайте
#------------------------------------------------------------
provider "aws" {
region = "ca-central-1"
}
data "aws_availability_zones" "available"{}
data "aws_ami" "latest_ubuntu" {
owners = [ "099720109477" ]
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-20.04-amd64-server-*"]
}
}
#------------------------ Security Group for Web Server------------------------------------------------------
resource "aws_security_group" "web" {
name = "Dynamic Security Group"
dynamic "ingress" {
for_each = ["80","443"]
content {
from_port = ingress.value
to_port = ingress.value
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" ]
}
tags = {
Name = "Dynamic Security Group"
Owner = "Me"
}
}
#------------------------Launch Configuration with Auto AMI Lookup-----------------------------------------------
resource "aws_launch_configuration" "web" {
//name = "WebServer-Highly-Available-LC"
name_prefix = "WebServer-Highly-Available-LC-" // создадим префикс. остальная часть имени будет подставляться тераформом автоматически при
// изменении launch configuration
image_id = data.aws_ami.latest_ubuntu
instance_type = "t3.micro"
security_groups = [aws_security_group.web.id]
user_data = file("user_data.sh")
lifecycle {
create_before_destroy = true
}
}
#-------------------------Auto Scaling Group using 2 Availability Zones-------------------------------------------
resource "aws_autoscaling_group" "web" {
//name = "WebServer-Highly-Available-ASG"
name = "ASG-${aws_launch_configuration.web.name}" // появилась зависимость от имени launch configuration
// если имя будет меняться, то будет создаваться новая ASG
launch_configuration = aws_launch_configuration.web.name
max_size = 2
min_size = 2
min_elb_capacity = 2
vpc_zone_identifier = [aws_default_subnet.default_az1.id,aws_default_subnet.default_az2.id ]
health_check_type = "ELB"
load_balancers = [aws_elb.web.name]
dynamic "tag" {
for_each = {
Name = "WebServer in ASG"
Owner = "Me"
TAGKEY = "TAGVALUE"
}
content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
lifecycle {
create_before_destroy = true
}
}
#-----------------Classic Load Balancer in 2 Availability Zones------------------------------------------------------
resource "aws_elb" "web" {
name = "WebSerber-HA-ELB"
availability_zones = [data.aws_availability_zones.available.names[0],data.aws_availability_zones.available.names[1]]
security_groups = [aws_security_group.web.id]
listener {
lb_port = 80
lb_protocol = "http"
instance_port = 80
instance_protocol = "http"
}
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 3
target = "HTTP:80/"
interval = 10
}
tags = {
Name = "WebServer-Highly-Available-ELB"
}
}
#-------------------------------------------------------------------------------------------------------------------------
resource "aws_default_subnet" "default_az1" {
availability_zone = data.aws_availability_zones.available.names[0]
}
resource "aws_default_subnet" "default_az2" {
availability_zone = data.aws_availability_zones.available.names[1]
}
#------------------------------------------------------------------------------------------------------------------------
output "web_loadbalancer_url" {
value = aws_elb.web.dns_name
}