-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
169 lines (148 loc) · 4.35 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
# Configure the AWS Provider
provider "aws" {
version = "~> 3.0"
region = var.aws_region
access_key = var.aws_access_key
secret_key = var.aws_secret_key
}
resource "aws_vpc" "vpc_example"{
cidr_block = var.vpc_cidr_block
enable_dns_hostnames = "true"
tags = {
Name = var.vpc_name
}
}
resource "aws_subnet" "subnet-us-east-1-1a" {
vpc_id = aws_vpc.vpc_example.id
cidr_block = var.subnet_1a_cidr_block
availability_zone = var.subnet_1a_az
tags = {
Name = var.subnet_1a_name
}
}
resource "aws_subnet" "subnet-us-east-1-1b" {
vpc_id = aws_vpc.vpc_example.id
cidr_block = var.subnet_1b_cidr_block
availability_zone = var.subnet_1b_az
tags = {
Name = var.subnet_1b_name
}
}
resource "aws_internet_gateway" "igw-example" {
vpc_id = aws_vpc.vpc_example.id
}
resource "aws_route_table" "route-table-example"{
vpc_id = aws_vpc.vpc_example.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw-example.id
}
route {
ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.igw-example.id
}
tags = {
Name = var.route_table_name
}
}
resource "aws_main_route_table_association" "route-table-with-vpc" {
vpc_id = aws_vpc.vpc_example.id
route_table_id = aws_route_table.route-table-example.id
}
resource "aws_security_group" "sg-example" {
name = var.security_group_name
description = var.security_group_description
vpc_id = aws_vpc.vpc_example.id
ingress {
description = "SSH from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Http from VPC"
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"]
}
tags = {
Name = var.security_group_name
}
}
resource "aws_instance" "web1" {
ami = var.instance_ami
instance_type = var.instance_type
associate_public_ip_address = "true"
subnet_id = aws_subnet.subnet-us-east-1-1a.id
vpc_security_group_ids = [aws_security_group.sg-example.id]
key_name = var.instance_key_name
tags = {
type = var.instance_tag_type
}
provisioner "local-exec" {
command = "sleep 120; ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ec2-user --private-key '${var.private_key_path}' -i ansible/aws_ec2.yaml ansible/main.yml"
}
}
resource "aws_instance" "web2" {
ami = var.instance_ami
instance_type = var.instance_type
associate_public_ip_address = "true"
subnet_id = aws_subnet.subnet-us-east-1-1b.id
vpc_security_group_ids = [aws_security_group.sg-example.id]
key_name = var.instance_key_name
tags = {
type = var.instance_tag_type
}
provisioner "local-exec" {
command = "sleep 120; ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u ec2-user --private-key '${var.private_key_path}' -i ansible/aws_ec2.yaml ansible/main.yml"
}
}
resource "aws_lb_target_group" "target-group-example" {
name = var.target_group_name
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.vpc_example.id
}
resource "aws_lb_target_group_attachment" "lb-tg-web1" {
target_group_arn = aws_lb_target_group.target-group-example.arn
target_id = aws_instance.web1.id
port = 80
}
resource "aws_lb_target_group_attachment" "lb-tg-web2" {
target_group_arn = aws_lb_target_group.target-group-example.arn
target_id = aws_instance.web2.id
port = 80
}
resource "aws_lb" "lb-example" {
name = var.lb_name
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.sg-example.id]
subnets = [
aws_subnet.subnet-us-east-1-1a.id,
aws_subnet.subnet-us-east-1-1b.id
]
tags = {
Name = var.lb_name
}
}
output "elb_public_ip" {
value = aws_lb.lb-example.dns_name
}
resource "aws_alb_listener" "lb-example-listener" {
load_balancer_arn = aws_lb.lb-example.arn
port = "80"
protocol = "HTTP"
default_action {
target_group_arn = aws_lb_target_group.target-group-example.arn
type = "forward"
}
}