This repository has been archived by the owner on Aug 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
97 lines (81 loc) · 2.24 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
terraform {
backend "s3" {
bucket = "cleverage-terraform-states"
key = "terraform-training"
region = "us-east-1"
}
}
provider "aws" {
region = "${var.aws_region}"
version = "~> 1.50"
}
resource "aws_default_vpc" "default" {}
resource "aws_key_pair" "authorized_key" {
key_name = "terraform-demo"
public_key = "${var.authorized_key}"
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"]
}
resource "aws_instance" "web" {
count = "${var.instances_count}"
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "${var.instances_type}"
key_name = "${aws_key_pair.authorized_key.key_name}"
tags = "${var.tags}"
}
resource "aws_security_group_rule" "allow_ssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_default_vpc.default.default_security_group_id}"
}
resource "aws_security_group" "allow_http" {
name = "allow_http"
vpc_id = "${aws_default_vpc.default.id}"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_elb" "lb" {
name = "terraform-demo-elb"
instances = ["${aws_instance.web.*.id}"]
availability_zones = ["${aws_instance.web.*.availability_zone}"]
security_groups = ["${aws_default_vpc.default.default_security_group_id}", "${aws_security_group.allow_http.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
}
tags = "${var.tags}"
}
resource "ansible_host" "default" {
count = "${aws_instance.web.count}"
inventory_hostname = "${aws_instance.web.*.id[count.index]}"
vars {
ansible_user = "ubuntu"
ansible_host = "${aws_instance.web.*.public_ip[count.index]}"
}
}