-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelb.tf
220 lines (193 loc) · 5.38 KB
/
elb.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# The provider here is aws but it can be other provider
provider "aws" {
shared_credentials_file = "/home/svarkey/.aws/credentials"
profile = "default"
}
# Create a VPC to launch our instances into
resource "aws_vpc" "terraform_vpc" {
cidr_block = "172.31.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "Terraform_Vpc"
}
}
# Create a way out to the internet
resource "aws_internet_gateway" "gw" {
vpc_id = "${aws_vpc.terraform_vpc.id}"
tags {
Name = "Terraform_Internet_Gateway"
}
}
# Public route as way out to the internet
resource "aws_route" "internet_access" {
route_table_id = "${aws_vpc.terraform_vpc.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gw.id}"
}
# Create a subnet in the AZ eu-east-1
resource "aws_subnet" "subnet_us_east_1a" {
vpc_id = "${aws_vpc.terraform_vpc.id}"
cidr_block = "172.31.1.0/24"
map_public_ip_on_launch = true
availability_zone = "us-east-1a"
// default_for_az = true
tags = {
Name = "Terraform_Subnet"
}
}
# Associate subnet subnet_eu_west_1a to public route table
resource "aws_route_table_association" "subnet_eu_west_1a_association" {
subnet_id = "${aws_subnet.subnet_us_east_1a.id}"
route_table_id = "${aws_vpc.terraform_vpc.main_route_table_id}"
}
resource "aws_default_network_acl" "default" {
default_network_acl_id = "${aws_vpc.terraform_vpc.default_network_acl_id}"
ingress {
protocol = -1
rule_no = 100
action = "allow"
cidr_block = "0.0.0.0/0"
from_port = 0
to_port = 0
}
egress {
protocol = -1
rule_no = 100
action = "allow"
cidr_block = "0.0.0.0/0"
from_port = 0
to_port = 0
}
}
resource "aws_default_security_group" "default" {
vpc_id = "${aws_vpc.terraform_vpc.id}"
ingress {
protocol = -1
self = true
from_port = 0
to_port = 0
cidr_blocks = [
"0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [
"0.0.0.0/0"]
}
depends_on = [
"aws_internet_gateway.gw"]
}
resource "aws_instance" "TerraformDemo" {
ami = "ami-14c5486b"
instance_type = "t2.micro"
key_name = "svarkey_private"
subnet_id = "${aws_subnet.subnet_us_east_1a.id}"
count = "${var.num_of_instances}"
# Creating three EC2 instances
vpc_security_group_ids = [
"${aws_default_security_group.default.id}"]
lifecycle {
# Due to several known issues in Terraform AWS provider related to arguments of aws_instance:
# (eg, https://github.com/terraform-providers/terraform-provider-aws/issues/2036)
# we have to ignore changes in the following arguments
ignore_changes = [
"private_ip",
"root_block_device"]
}
tags {
Name = "TerraformDemo_Instance_${count.index}"
}
}
resource "aws_elb" "web" {
name = "terraform-demo"
# The same availability zone as our instance
subnets = [
"${aws_subnet.subnet_us_east_1a.id}"]
security_groups = [
"${aws_default_security_group.default.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
}
# The instance is registered automatically
instances = [
"${aws_instance.TerraformDemo.*.id}"]
cross_zone_load_balancing = true
idle_timeout = 400
connection_draining = true
connection_draining_timeout = 400
depends_on = [
"aws_instance.TerraformDemo"]
provisioner "local-exec" {
command = "cp -f /home/svarkey/dev/code/demo/terraform/wait_for_elb.sh /tmp/wait_for_elb.sh"
on_failure = "continue"
}
provisioner "local-exec" {
command = "sed -Ei 's/LOAD_BAL_DNS/${aws_elb.web.dns_name}/g' /tmp/wait_for_elb.sh"
on_failure = "continue"
}
provisioner "local-exec" {
command = "chmod a+x /tmp/wait_for_elb.sh"
on_failure = "continue"
}
}
resource "null_resource" "nginx_install" {
depends_on = [
"aws_instance.TerraformDemo"]
# Bootstrap script can run on any instance of the cluster
# So we just choose the first in this case
count = "${var.num_of_instances}"
triggers {
current_ec2_instance_id = "${element(aws_instance.TerraformDemo.*.id, count.index)}"
instance_number = "${count.index + 1}"
}
connection {
host = "${element(aws_instance.TerraformDemo.*.public_ip, count.index)}"
}
provisioner "remote-exec" {
inline = [
"sudo yum -y update",
"sudo yum -y install nginx",
"sudo service nginx start",
"sudo sed -Ei 's/Amazon Linux AMI/Amazon Linux AMI-${element(aws_instance.TerraformDemo.*.public_ip, count.index)}/g' /usr/share/nginx/html/index.html",
]
connection {
user = "ec2-user"
type = "ssh"
private_key = "${file("/home/svarkey/dev/data/vpn/aws/svarkey_private.pem")}"
timeout = "5m"
agent = false
}
}
provisioner "local-exec" {
command = "/tmp/wait_for_elb.sh"
interpreter = [
"/bin/bash",
"-c"]
on_failure = "continue"
}
}
/*
# THIS ONE WORKS
resource "aws_elb_attachment" "search-svc-elb-attach-0" {
elb = "${aws_security_group.elb.id}"
instance = "${element(aws_instance.SanchuTest.*.id, 0)}"
}
## THE FOLLOWING TWO THROW AN ERROR
resource "aws_elb_attachment" "search-svc-elb-attach-1" {
elb = "${aws_security_group.elb.id}"
instance = "${element(aws_instance.SanchuTest.*.id, 1)}"
}
*/