-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
executable file
·122 lines (99 loc) · 2.7 KB
/
main.tf
File metadata and controls
executable file
·122 lines (99 loc) · 2.7 KB
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
#Availability Zones - Change it
variable "azs" {
type = "list"
description = "List of availability zones that instance can be provisioned to"
default = [ "us-west-2a", "us-west-2b" ]
}
# AWS KeyName - Change it
variable "KeyPair_Name" {
description = "Desired name of the AWS key pair"
default = ""
}
variable "Public_SSHKey" {
description = "Public SSH Key"
default = ""
}
#Subnets - Change it
variable "aws_subnet" {
description = "Subnet for the ELB"
default = [ "subnet-de32bbba", "subnet-de32bbba" ]
}
variable "ElasticLoadBalancer_Name" {
description = "Elastic Load Balancer Name"
default = ""
}
provider "aws" {
region = "us-west-2"
}
resource "aws_key_pair" "deployer" {
key_name = "${var.KeyPair_Name}"
public_key = "${var.Public_SSHKey}"
}
resource "aws_elb" "web" {
name = "${var.ElasticLoadBalancer_Name}"
# The same availability zone as our instance
subnets = ["${var.aws_subnet}"]
security_groups = ["sg-428f4024"]
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/index.html"
interval = 5
}
# The instance is 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"
count = "4"
availability_zone = "${element(var.azs,count.index)}"
# Lookup the correct AMI based on the region we specified
ami = "ami-6b8cef13"
# The name of our SSH keypair you've created and downloaded
# from the AWS console.
# https://console.aws.amazon.com/ec2/v2/home?region=us-west-2#KeyPairs:
key_name = "${aws_key_pair.deployer.key_name}"
vpc_security_group_ids = ["sg-428f4024"]
user_data = <<EOF
#!/bin/bash
yum update -y
yum install httpd -y
service httpd start
chkconfig httpd on
myIP=`curl http://169.254.169.254/latest/meta-data/public-ipv4`
echo 'My IP ==>'$myIP
cat << EOFF > /var/www/html/index.html
<html>
<body>
<h1>Welcome to my Web Page</h1>
<hr/>
<p>MY IP:$myIP </p>
<hr/>
</body>
</html>
EOFF
EOF
tags {
Name = "instance-elb-${count.index}"
}
}
output "loadbalancer" {
value = "http://${aws_elb.web.dns_name}"
}