-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.tf
179 lines (149 loc) · 3.65 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "3.42.0"
}
}
}
provider "aws" {
region = "us-east-1"
#Using credentials file
shared_credentials_file = "credentials"
profile = "default"
}
#-----VPC-----
resource "aws_vpc" "dev-vpc" {
cidr_block = "10.132.120.0/21"
tags = {
Name = "prod-vpc"
}
}
#-----Subnet 1-----
resource "aws_subnet" "public-subnet-1" {
vpc_id = "aws_vpc.dev-vpc"
cidr_block = "10.132.121.0/24"
availability_zone = "us-east-1a"
tags = {
Name = "Public"
}
}
#-----Subnet 2-----
resource "aws_subnet" "public-subnet-2" {
vpc_id = "aws_vpc.dev-vpc"
cidr_block = "10.132.122.0/24"
availability_zone = "us-east-1b"
tags = {
Name = "Public"
}
}
#-----Subnet 3-----
resource "aws_subnet" "private-subnet-1" {
vpc_id = "aws_vpc.dev-vpc"
cidr_block = "10.132.123.0/24"
availability_zone = "us-east-1a"
tags = {
Name = "Private"
}
}
#-----Subnet 4-----
resource "aws_subnet" "private-subnet-2" {
vpc_id = "aws_vpc.dev-vpc"
cidr_block = "10.132.124.0/24"
availability_zone = "us-east-1b"
tags = {
Name = "Private"
}
}
#-----Loadbalancer Security Group-----
resource "aws_security_group" "Loadbalancer" {
name = "Loadbalancer"
description = "Load balancer SG"
vpc_id = "aws_vpc.dev-vpc"
ingress = [
{
description = "Allow http traffic"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0" ]
ipv6_cidr_blocks = [ "::/0" ]
prefix_list_ids = null
security_groups = null
self = null
}
]
egress = [
{
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
prefix_list_ids = null
security_groups = null
self = null
description = null
}
]
}
#-----Webserver Security Group-----
resource "aws_security_group" "Webserver" {
name = "Webserver"
description = "Web Server SG"
vpc_id = "aws_vpc.dev-vpc"
ingress = [
{
description = "Allow traffic from Loadbalancer SG"
from_port = 80
to_port = 80
security_groups = [ "LoadBalancer" ]
protocol = "TCP"
cidr_blocks = [ "0.0.0.0/0" ]
ipv6_cidr_blocks = [ "::/0" ]
prefix_list_ids = null
security_groups = null
self = null
}
]
egress = [
{
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
prefix_list_ids = null
security_groups = null
self = null
description = null
}
]
}
#-----Load balancer-----
#-----Lauch Template-----
resource "aws_launch_template" "TOP-Template-1" {
name = "TOP-Template-1"
image_id = "ami-0c2b8ca1dad447f8a"
instance_type = "t2.micro"
vpc_security_group_ids = [ "WebServer" ]
key_name = "TOP-keys"
user_data = <<-EOF
#!/bin/sh
yum update -y
yum install -y httpd
systemctl start httpd
echo "hello world from $(hostname)" > /var/www/html/index.html
EOF
}
#-----Autoscaling Group-----
resource "aws_autoscaling_group" "asg-1" {
availability_zones = [ "us-east-1a, us-east-1b" ]
desired_capacity = 2
min_size = 2
max_size = 2
launch_template {
id = aws_launch_template.TOP-Template-1.id
version = "$Latest"
}
}