-
Notifications
You must be signed in to change notification settings - Fork 73
/
main.tf
167 lines (145 loc) · 4.76 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
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
# -------------------------
# Define el provider de AWS
# -------------------------
provider "aws" {
region = "eu-west-1"
}
# -----------------------------------------------
# Data source que obtiene el id del AZ eu-west-1a
# -----------------------------------------------
data "aws_subnet" "az_a" {
availability_zone = "eu-west-1a"
}
# -----------------------------------------------
# Data source que obtiene el id del AZ eu-west-1a
# -----------------------------------------------
data "aws_subnet" "az_b" {
availability_zone = "eu-west-1b"
}
# ---------------------------------------
# Define una instancia EC2 con AMI Ubuntu
# ---------------------------------------
resource "aws_instance" "servidor_1" {
ami = "ami-0aef57767f5404a3c"
instance_type = var.tipo_instancia
subnet_id = data.aws_subnet.az_a.id
vpc_security_group_ids = [aws_security_group.mi_grupo_de_seguridad.id]
// Escribimos un "here document" que es
// usado durante la inicialización
user_data = <<-EOF
#!/bin/bash
echo "Hola Terraformers! Soy servidor 1" > index.html
nohup busybox httpd -f -p ${var.puerto_servidor} &
EOF
tags = {
Name = "servidor-1"
}
}
# ----------------------------------------------
# Define la segunda instancia EC2 con AMI Ubuntu
# ----------------------------------------------
resource "aws_instance" "servidor_2" {
ami = "ami-0aef57767f5404a3c"
instance_type = var.tipo_instancia
subnet_id = data.aws_subnet.az_b.id
vpc_security_group_ids = [aws_security_group.mi_grupo_de_seguridad.id]
user_data = <<-EOF
#!/bin/bash
echo "Hola Terraformers! Soy servidor 2" > index.html
nohup busybox httpd -f -p ${var.puerto_servidor} &
EOF
tags = {
Name = "servidor-2"
}
}
# ------------------------------------------------------
# Define un grupo de seguridad con acceso al puerto 8080
# ------------------------------------------------------
resource "aws_security_group" "mi_grupo_de_seguridad" {
name = "primer-servidor-sg"
ingress {
security_groups = [aws_security_group.alb.id]
description = "Acceso al puerto 8080 desde el exterior"
from_port = var.puerto_servidor
to_port = var.puerto_servidor
protocol = "TCP"
}
}
# ----------------------------------------
# Load Balancer público con dos instancias
# ----------------------------------------
resource "aws_lb" "alb" {
load_balancer_type = "application"
name = "terraformers-alb"
security_groups = [aws_security_group.alb.id]
subnets = [data.aws_subnet.az_a.id, data.aws_subnet.az_b.id]
}
# ------------------------------------
# Security group para el Load Balancer
# ------------------------------------
resource "aws_security_group" "alb" {
name = "alb-sg"
ingress {
cidr_blocks = ["0.0.0.0/0"]
description = "Acceso al puerto 80 desde el exterior"
from_port = var.puerto_lb
to_port = var.puerto_lb
protocol = "TCP"
}
egress {
cidr_blocks = ["0.0.0.0/0"]
description = "Acceso al puerto 8080 de nuestros servidores"
from_port = var.puerto_servidor
to_port = var.puerto_servidor
protocol = "TCP"
}
}
# ----------------------------------------------------
# Data Source para obtener el ID de la VPC por defecto
# ----------------------------------------------------
data "aws_vpc" "default" {
default = true
}
# ----------------------------------
# Target Group para el Load Balancer
# ----------------------------------
resource "aws_lb_target_group" "this" {
name = "terraformers-alb-target-group"
port = var.puerto_lb
vpc_id = data.aws_vpc.default.id
protocol = "HTTP"
health_check {
enabled = true
matcher = "200"
path = "/"
port = var.puerto_servidor
protocol = "HTTP"
}
}
# -----------------------------
# Attachment para el servidor 1
# -----------------------------
resource "aws_lb_target_group_attachment" "servidor_1" {
target_group_arn = aws_lb_target_group.this.arn
target_id = aws_instance.servidor_1.id
port = var.puerto_servidor
}
# -----------------------------
# Attachment para el servidor 2
# -----------------------------
resource "aws_lb_target_group_attachment" "servidor_2" {
target_group_arn = aws_lb_target_group.this.arn
target_id = aws_instance.servidor_2.id
port = var.puerto_servidor
}
# ------------------------
# Listener para nuestro LB
# ------------------------
resource "aws_lb_listener" "this" {
load_balancer_arn = aws_lb.alb.arn
port = var.puerto_lb
default_action {
target_group_arn = aws_lb_target_group.this.arn
type = "forward"
}
}