forked from vamperst/Hackaton-project-terraform-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
101 lines (79 loc) · 2.11 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
# Specify the provider and access details
provider "aws" {
region = "${var.aws_region}"
}
data "template_file" "script" {
template = "${file("${path.module}/script.sh.tpl")}"
vars = {
ECR_REGISTRY = "${var.ECR_REGISTRY}"
}
}
variable "project" {
default = "fiap-lab"
}
data "aws_vpc" "vpc" {
tags = {
Name = "${var.project}"
}
}
data "aws_subnet_ids" "all" {
vpc_id = "${data.aws_vpc.vpc.id}"
tags = {
Tier = "Public"
}
}
data "aws_subnet" "public" {
for_each = data.aws_subnet_ids.all.ids
id = "${each.value}"
}
resource "random_shuffle" "random_subnet" {
input = [for s in data.aws_subnet.public : s.id]
result_count = 1
}
resource "aws_elb" "web" {
name = "hackton-elb-${terraform.workspace}"
subnets = data.aws_subnet_ids.all.ids
security_groups = ["${aws_security_group.allow-ssh.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 = 6
}
# The instances are registered automatically
instances = aws_instance.web.*.id
}
resource "aws_instance" "web" {
instance_type = "t2.micro"
ami = "${lookup(var.aws_amis, var.aws_region)}"
count = 1
subnet_id = "${random_shuffle.random_subnet.result[0]}"
vpc_security_group_ids = ["${aws_security_group.allow-ssh.id}"]
key_name = "${var.KEY_NAME}"
iam_instance_profile = "${aws_iam_instance_profile.ecr_readOnly_profile.name}"
provisioner "file" {
content = "${data.template_file.script.rendered}"
destination = "$(pwd)/script.sh"
}
provisioner "remote-exec" {
inline = [
"sudo chmod +x $(pwd)/script.sh",
"sudo bash $(pwd)/script.sh"
]
}
connection {
user = "${var.INSTANCE_USERNAME}"
private_key = "${file("${var.PATH_TO_KEY}")}"
host = "${self.public_dns}"
}
tags = {
Name = "${format("nginx-hackaton-%03d-${terraform.workspace}", count.index + 1)}"
}
}