-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
170 lines (138 loc) · 3.52 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
# Provider and Access details
provider "aws" {
region = "${var.aws_region}"
}
resource "aws_vpc" "default" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
tags {
Name = "tf_app_prov"
}
}
resource "aws_subnet" "tf_app_subnet" {
vpc_id = "${aws_vpc.default.id}"
cidr_block = "10.0.0.0/24"
map_public_ip_on_launch = true
tags {
Name = "tf_app_subnet"
}
}
resource "aws_internet_gateway" "gw" {
vpc_id = "${aws_vpc.default.id}"
tags {
Name = "tf_app_ig"
}
}
resource "aws_route_table" "r" {
vpc_id = "${aws_vpc.default.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gw.id}"
}
tags {
Name = "aws_route_table"
}
}
resource "aws_route_table_association" "a" {
subnet_id = "${aws_subnet.tf_app_subnet.id}"
route_table_id = "${aws_route_table.r.id}"
}
# Default Security Group to access
# the Instances over SSH and HTTP
resource "aws_security_group" "default" {
name = "instance_tk"
description = "Used in the terraform"
vpc_id = "${aws_vpc.default.id}"
# SSH access <-- anywhere
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# HTTP access <-- anywhere
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# outbound Internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# ELB security group to access
# the ELB over HTTP
resource "aws_security_group" "elb" {
name = "elb_tk"
description = "Used in the terraform"
vpc_id = "${aws_vpc.default.id}"
# HTTP access from anywhere
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# outbound internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
# VPC has an Internet gateway or fail
depends_on = ["aws_internet_gateway.gw"]
}
resource "aws_elb" "web" {
name = "tk-elb"
# Availability zone as our instance
subnets = ["${aws_subnet.tf_app_subnet.id}"]
security_groups = ["${aws_security_group.elb.id}"]
listener {
instance_port = 80
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 3
target = "HTTP:80/"
interval = 30
}
# Instance --> registered automatically
instances = ["${aws_instance.web.id}"]
cross_zone_load_balancing = true
idle_timeout = 400
connection_draining = true
connection_draining_timeout = 400
}
resource "aws_lb_cookie_stickiness_policy" "default" {
name = "lbpolicy"
load_balancer = "${aws_elb.web.id}"
lb_port = 80
cookie_expiration_period = 600
}
resource "aws_instance" "web" {
instance_type = "t2.micro"
# Lookup the correct AMI based on the region
# we specified
ami = "${lookup(var.aws_amis, var.aws_region)}"
# SSH keypair created/downloaded
# from the AWS console.
key_name = "${var.key_name}"
# Security group to allow HTTP and SSH access
vpc_security_group_ids = ["${aws_security_group.default.id}"]
subnet_id = "${aws_subnet.tf_app_subnet.id}"
user_data = "${file("userdata.sh")}"
#Instance tags
tags {
Name = "elb-tk"
}
}