-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebservers.tf
112 lines (98 loc) · 2.66 KB
/
webservers.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
# configured aws provider with proper credentials
provider "aws" {
region = var.region
#profile = "default"
}
# create a vpc
resource "aws_vpc" "this" {
cidr_block = "10.20.20.0/26"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
"Name" = "Application-lb"
}
}
# create a subnet
resource "aws_subnet" "private" {
count = length(var.subnet_cidr_private)
vpc_id = aws_vpc.this.id
cidr_block = var.subnet_cidr_private[count.index]
availability_zone = var.availability_zone[count.index]
tags = {
"Name" = "Application-lb-private"
}
}
# create a route table
resource "aws_route_table" "this-rt" {
vpc_id = aws_vpc.this.id
tags = {
"Name" = "Application-lb-route-table"
}
}
# create a route table association
resource "aws_route_table_association" "private" {
count = length(var.subnet_cidr_private)
subnet_id = element(aws_subnet.private.*.id, count.index)
route_table_id = aws_route_table.this-rt.id
}
# create an internet gateway
resource "aws_internet_gateway" "this-igw" {
vpc_id = aws_vpc.this.id
tags = {
"Name" = "Application-lb-gateway"
}
}
# create an internet route
resource "aws_route" "internet-route" {
destination_cidr_block = "0.0.0.0/0"
route_table_id = aws_route_table.this-rt.id
gateway_id = aws_internet_gateway.this-igw.id
}
# Create a security group
resource "aws_security_group" "web-server" {
name = "allow_http_access"
description = "allow http traffic from alb"
vpc_id = aws_vpc.this.id
ingress {
description = "traffic from alb"
from_port = "80"
to_port = "80"
protocol = "tcp"
security_groups = [aws_security_group.alb_sg.id]
}
egress {
cidr_blocks = ["0.0.0.0/0"]
from_port = "0"
protocol = "-1"
to_port = "0"
}
tags = {
"Name" = "web-server-sg"
}
}
# use data source to get a registered amazon linux 2 ami
data "aws_ami" "amazon_linux_2" {
most_recent = true
owners = ["amazon"]
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn2-ami-hvm*"]
}
}
# launch 2 EC2 instances and install apache
resource "aws_instance" "web-server" {
count = length(var.subnet_cidr_private)
instance_type = "t2.micro"
ami = data.aws_ami.amazon_linux_2.id
vpc_security_group_ids = [aws_security_group.web-server.id]
subnet_id = element(aws_subnet.private.*.id, count.index)
user_data = file("install_httpd.sh")
associate_public_ip_address = true
tags = {
Name = "web-server-${count.index + 1}"
}
}