Skip to content

Commit 67d24be

Browse files
committed
adding locals
1 parent 464a931 commit 67d24be

3 files changed

Lines changed: 185 additions & 6 deletions

File tree

05-iac/00-terraform/03-usando-inputs-outputs/02-demo.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ variable "sg_ingress_port" {
200200
}
201201
202202
variable "sg_egress_cidr_blocks" {
203-
type = list(string)
203+
type map = list(string)
204204
description = "cidr blocks allow for egress"
205205
default = ["0.0.0.0/0"]
206206
}
@@ -245,10 +245,7 @@ resource "aws_security_group" "nginx-sg" {
245245
}
246246
# ....
247247
```
248-
249-
### Paso 6. Actualizamos las instancias
250-
251-
Creamos nuevas entradas en `variables.tf` para las instancias
248+
mapas entradas en `variables.tf` para las instancias
252249

253250
```diff
254251

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# Añadiendo Locals a la Configuración
2+
3+
## Pre requisitos
4+
5+
> Si has destruidfo el entorno recrealo
6+
7+
```bash
8+
cd lab/lc_web_app/
9+
terraform plan -out d1.tfplan
10+
terraform apply "d1.tfplan"
11+
```
12+
13+
## Pasos
14+
15+
### Paso 1. Crear un nuevo fichero para Locals
16+
17+
Crear `./lab/lc_web_app/locals.tf`.
18+
19+
```tf
20+
locals {
21+
common_tags = {
22+
23+
}
24+
}
25+
26+
```
27+
28+
### Paso 2. Añadiendo valores
29+
30+
Queremos añadir 3 valores, `company`, `project` y `billing_code`. Vamos a sacar esta información de las `variables`
31+
32+
Actualizamos `variables.tf`
33+
34+
```tf
35+
# ....
36+
# COMMON
37+
variable "company" {
38+
type = string
39+
description = "Company name for resource tagging"
40+
default = "Lemoncode"
41+
}
42+
43+
variable "project" {
44+
type = string
45+
description = "Project name for resource tagging"
46+
}
47+
48+
variable "billing_code" {
49+
type = string
50+
description = "Billing code for resource tagging"
51+
}
52+
53+
```
54+
55+
Ahora podemos actualizar `locals.tf`
56+
57+
```diff
58+
locals {
59+
common_tags = {
60+
+ company = var.company
61+
+ project = "${var.company}-${var.project}"
62+
+ billing_code = var.billing_code
63+
}
64+
}
65+
```
66+
67+
### Paso 4. Actualizando nuestra configuración
68+
69+
Ahora podemos actualizar nuestra configuración con las etiquetas.
70+
71+
Actualizamos `main.tf`
72+
73+
```diff
74+
# PROVIDERS
75+
provider "aws" {
76+
access_key = var.aws_access_key
77+
secret_key = var.aws_secret_key
78+
region = var.aws_region
79+
}
80+
81+
# DATA
82+
data "aws_ssm_parameter" "ami" {
83+
name = "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
84+
}
85+
86+
# RESOURCES
87+
88+
# NETWORKING #
89+
resource "aws_vpc" "vpc" {
90+
cidr_block = var.vpc_cidr_block
91+
enable_dns_hostnames = var.vpc_enable_dns_hostnames
92+
93+
+ tags = local.common_tags
94+
}
95+
96+
resource "aws_internet_gateway" "igw" {
97+
vpc_id = aws_vpc.vpc.id
98+
99+
+ tags = local.common_tags
100+
}
101+
102+
resource "aws_subnet" "subnet1" {
103+
cidr_block = var.subnet_cidr_block
104+
vpc_id = aws_vpc.vpc.id
105+
map_public_ip_on_launch = var.subnet_map_public_ip_on_launch
106+
107+
+ tags = local.common_tags
108+
}
109+
110+
# ROUTING #
111+
resource "aws_route_table" "rtb" {
112+
vpc_id = aws_vpc.vpc.id
113+
114+
route {
115+
cidr_block = var.route_table_cidr_block
116+
gateway_id = aws_internet_gateway.igw.id
117+
}
118+
119+
+ tags = local.common_tags
120+
}
121+
122+
resource "aws_route_table_association" "rta-subnet1" {
123+
subnet_id = aws_subnet.subnet1.id
124+
route_table_id = aws_route_table.rtb.id
125+
}
126+
127+
# SECURITY GROUPS #
128+
resource "aws_security_group" "nginx-sg" {
129+
name = "nginx_sg"
130+
vpc_id = aws_vpc.vpc.id
131+
132+
# HTTP access from anywhere
133+
ingress {
134+
from_port = var.sg_ingress_port
135+
to_port = var.sg_ingress_port
136+
protocol = "tcp"
137+
cidr_blocks = var.sg_ingress_cidr_blocks
138+
}
139+
140+
# outbound internet access
141+
egress {
142+
from_port = var.sg_egress_port
143+
to_port = var.sg_egress_port
144+
protocol = "-1"
145+
cidr_blocks = var.sg_egress_cidr_blocks
146+
}
147+
148+
+ tags = local.common_tags
149+
}
150+
151+
# INSTANCES #
152+
resource "aws_instance" "nginx1" {
153+
ami = nonsensitive(data.aws_ssm_parameter.ami.value)
154+
instance_type = var.aws_instance_type
155+
subnet_id = aws_subnet.subnet1.id
156+
vpc_security_group_ids = [aws_security_group.nginx-sg.id]
157+
158+
user_data = <<EOF
159+
#! /bin/bash
160+
sudo amazon-linux-extras install -y nginx1
161+
sudo service nginx start
162+
sudo rm /usr/share/nginx/html/index.html
163+
echo '<html><head><title>Lemon Land Server</title></head><body style=\"background-color:#1F778D\"><p style=\"text-align: center;\"><span style=\"color:#FFFFFF;\"><span style=\"font-size:28px;\">Welcome to &#127819; land</span></span></p></body></html>' | sudo tee /usr/share/nginx/html/index.html
164+
EOF
165+
166+
+ tags = local.common_tags
167+
168+
}
169+
170+
```
171+
172+
El `map` de locals `common_tags` será evaluado contra la etiqueta `tags`, la cual espera un tipo de dato `map`.
173+
174+
## Clean Up
175+
176+
```bash
177+
terraform destroy
178+
```

05-iac/00-terraform/03-usando-inputs-outputs/readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ Vamos a ir haciendo mejoras de manera progresiava sobre nuestro despliegue, apro
1313

1414
## Locals
1515

16-
> Valores evaluados dentro de la configuración
16+
> Valores evaluados dentro de la configuración, no puedes modificar su valor, como las variables.
17+
18+
Puedes tener múltiples entradas `locals`, pero sus pares `key/value` han de ser únicos.
19+
20+
[Añadiendo Locals a la configuración - Demo 03](03-demo.md)

0 commit comments

Comments
 (0)