-
Notifications
You must be signed in to change notification settings - Fork 1
/
web_servers.cluster.tf.good
141 lines (114 loc) · 3.21 KB
/
web_servers.cluster.tf.good
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
provider "aws" {
region = "us-east-1"
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
}
resource "aws_launch_configuration" "example" {
image_id = "ami-33e4bc49"
instance_type = "t2.micro"
security_groups = ["${aws_security_group.instance.id}"]
key_name = "${var.key_name}"
user_data = <<-EOF
#!/bin/bash
#sudo apt-get update
#sudo apt-get install -y nginx
#sudo service nginx start
cd /home/ubuntu
echo "Hello, world<br/>" > index.html
echo "<br/>" >> index.html
date >> index.html
echo "<br/>" >> index.html
uname -n >> index.html
python -m SimpleHTTPServer "${var.server_port}"
EOF
lifecycle {
create_before_destroy = true
}
connection {
user = "ubuntu"
private_key = "${file(var.private_key_path)}"
}
}
resource "aws_security_group" "instance" {
name = "terraform-example-instance"
description = "Allow HTTP traffic from anywhere"
ingress {
from_port = "${var.server_port}"
to_port = "${var.server_port}"
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# Opening SSH port
ingress {
from_port = "${var.ssh_port}"
to_port = "${var.ssh_port}"
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
lifecycle {
create_before_destroy = true
}
}
data "aws_availability_zones" "all" {}
resource "aws_autoscaling_group" "example" {
launch_configuration = "${aws_launch_configuration.example.id}"
availability_zones = ["${data.aws_availability_zones.all.names}"]
min_size = 3
max_size = 10
load_balancers = ["${aws_elb.example.name}"]
health_check_type = "ELB"
tag {
key = "Name"
value = "terraform-asg-example"
propagate_at_launch = true
}
}
resource "aws_elb" "example" {
name = "terraform-asg-example"
security_groups = ["${aws_security_group.elb.id}"]
availability_zones = ["${data.aws_availability_zones.all.names}"]
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 5
interval = 300
target = "HTTP:${var.server_port}/"
}
listener {
lb_port = "${var.http_port}"
lb_protocol = "http"
instance_port = "${var.server_port}"
instance_protocol = "http"
}
connection {
user = "ubuntu"
private_key = "${file(var.private_key_path)}"
}
tags {
myTag = "aws-elb-instance"
}
}
resource "aws_security_group" "elb" {
name = "terraform-example-elb"
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = "${var.http_port}"
to_port = "${var.http_port}"
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
output "elbs_dns_name" {
value = "${aws_elb.example.dns_name}"
}
# output "addresses" {
# value = "${aws_launch_configuration.examples.public_ip}"
# }
output "public_port" {
value = "${var.server_port}"
}