Skip to content

Commit 107320e

Browse files
committed
funciones y looping added
1 parent 9791cbf commit 107320e

9 files changed

Lines changed: 1064 additions & 84 deletions

File tree

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# Actualizando la VPC y las instancias
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 d4.tfplan
10+
terraform apply "d4.tfplan"
11+
```
12+
13+
## Pasos
14+
15+
### Paso 1.
16+
17+
Creamos un nueva entrada en el fichero de variables, para controlar el número de instancias que queremos.
18+
19+
Actualizamos `variables.tf`
20+
21+
```diff
22+
# NETWORKING
23+
variable "vpc_cidr_block" {
24+
type = string
25+
description = "VPC cidr block"
26+
default = "10.0.0.0/16"
27+
}
28+
+
29+
+variable "vpc_subnet_count" {
30+
+ type = number
31+
+ description = "Number of subnets to create"
32+
+ default = 2
33+
+}
34+
+
35+
variable "vpc_enable_dns_hostnames" {
36+
type = bool
37+
description = "Enable / Disable DNS hostnames on VPC"
38+
default = true
39+
}
40+
```
41+
42+
### Paso 2.
43+
44+
Ahora podemos actualizar el recurso de las subnets.
45+
46+
Actualizamos `network.tf`
47+
48+
```diff
49+
+resource "aws_subnet" "subnets" {
50+
+ count = var.vpc_subnet_count
51+
+ cidr_block = var.subnets_cidr_block[count.index]
52+
+ vpc_id = aws_vpc.vpc.id
53+
+ map_public_ip_on_launch = var.subnet_map_public_ip_on_launch
54+
+ availability_zone = data.aws_availability_zones.available.names[count.index]
55+
+}
56+
+
57+
- resource "aws_subnet" "subnet1" {
58+
- cidr_block = var.subnets_cidr_block[0]
59+
- vpc_id = aws_vpc.vpc.id
60+
- map_public_ip_on_launch = var.subnet_map_public_ip_on_launch
61+
- availability_zone = data.aws_availability_zones.available.names[0]
62+
-
63+
- tags = local.common_tags
64+
- }
65+
-
66+
- resource "aws_subnet" "subnet2" {
67+
- cidr_block = var.subnets_cidr_block[1]
68+
- vpc_id = aws_vpc.vpc.id
69+
- map_public_ip_on_launch = var.subnet_map_public_ip_on_launch
70+
- availability_zone = data.aws_availability_zones.available.names[1]
71+
-
72+
- tags = local.common_tags
73+
- }
74+
```
75+
76+
En consecuencia debemos actualizar la asociación de rutas paras las subnets.
77+
78+
```diff
79+
-resource "aws_route_table_association" "rta-subnet1" {
80+
+resource "aws_route_table_association" "rta-subnets" {
81+
subnet_id = aws_subnet.subnet1.id
82+
route_table_id = aws_route_table.rtb.id
83+
}
84+
```
85+
86+
```diff
87+
resource "aws_route_table_association" "rta-subnets" {
88+
+ count = var.vpc_subnet_count
89+
- subnet_id = aws_subnet.subnet1.id
90+
+ subnet_id = aws_subnet.subnets[count.index].id
91+
route_table_id = aws_route_table.rtb.id
92+
}
93+
```
94+
95+
Ahora podemos eliminar `rta-subnet2`
96+
97+
```diff
98+
-resource "aws_route_table_association" "rta-subnet2" {
99+
- subnet_id = aws_subnet.subnet2.id
100+
- route_table_id = aws_route_table.rtb.id
101+
-}
102+
```
103+
104+
### Paso 3.
105+
106+
Ahora que hemos hecho esto vamos a actulizar las instancias.
107+
108+
Actualizamos `variables.tf`
109+
110+
```diff
111+
# INSTANCES
112+
variable "aws_instance_type" {
113+
type = string
114+
description = "The EC2 instance to be used"
115+
default = "t2.micro"
116+
}
117+
+
118+
+variable "instance_count" {
119+
+ type = number
120+
+ description = "Number of instances to create in VPC"
121+
+ default = 2
122+
+}
123+
+
124+
```
125+
126+
Actualizamos `instances.tf`
127+
128+
```diff
129+
# INSTANCES #
130+
-resource "aws_instance" "nginx1" {
131+
+resource "aws_instance" "nginx" {
132+
+ count = var.instance_count
133+
ami = nonsensitive(data.aws_ssm_parameter.ami.value)
134+
instance_type = var.aws_instance_type
135+
- subnet_id = aws_subnet.subnet1.id
136+
+ subnet_id = aws_subnet.subnets[count.index].id
137+
vpc_security_group_ids = [aws_security_group.nginx-sg.id]
138+
iam_instance_profile = aws_iam_instance_profile.nginx_profile.name
139+
depends_on = [aws_iam_role_policy.allow_s3_all]
140+
141+
user_data = <<EOF
142+
#! /bin/bash
143+
sudo amazon-linux-extras install -y nginx1
144+
sudo service nginx start
145+
aws s3 cp s3://${aws_s3_bucket.web_bucket.id}/website/index.html /home/ec2-user/index.html
146+
aws s3 cp s3://${aws_s3_bucket.web_bucket.id}/website/fruits.png /home/ec2-user/fruits.png
147+
sudo rm /usr/share/nginx/html/index.html
148+
sudo cp /home/ec2-user/index.html /usr/share/nginx/html/index.html
149+
sudo cp /home/ec2-user/fruits.png /usr/share/nginx/html/fruits.png
150+
EOF
151+
152+
tags = local.common_tags
153+
154+
}
155+
156+
-resource "aws_instance" "nginx2" {
157+
- ami = nonsensitive(data.aws_ssm_parameter.ami.value)
158+
- instance_type = var.aws_instance_type
159+
- subnet_id = aws_subnet.subnet2.id
160+
- vpc_security_group_ids = [aws_security_group.nginx-sg.id]
161+
- iam_instance_profile = aws_iam_instance_profile.nginx_profile.name
162+
- depends_on = [aws_iam_role_policy.allow_s3_all]
163+
164+
- user_data = <<EOF
165+
- #! /bin/bash
166+
- sudo amazon-linux-extras install -y nginx1
167+
- sudo service nginx start
168+
- aws s3 cp s3://${aws_s3_bucket.web_bucket.id}/website/index.html /home/ec2-user/index.html
169+
- aws s3 cp s3://${aws_s3_bucket.web_bucket.id}/website/fruits.png /home/ec2-user/fruits.png
170+
- sudo rm /usr/share/nginx/html/index.html
171+
- sudo cp /home/ec2-user/index.html /usr/share/nginx/html/index.html
172+
- sudo cp /home/ec2-user/fruits.png /usr/share/nginx/html/fruits.png
173+
- EOF
174+
-
175+
- tags = local.common_tags
176+
-
177+
- }
178+
-
179+
```
180+
181+
### Paso 4.
182+
183+
Por último actualizamos `loadbalancer.tf`
184+
185+
```diff
186+
-resource "aws_lb_target_group_attachment" "nginx1" {
187+
+resource "aws_lb_target_group_attachment" "nginx" {
188+
+ count = var.instance_count
189+
target_group_arn = aws_lb_target_group.nginx.arn
190+
- target_id = aws_instance.nginx1.id
191+
+ target_id = aws_instance.nginx[count.index].id
192+
port = 80
193+
}
194+
-
195+
-resource "aws_lb_target_group_attachment" "nginx2" {
196+
- target_group_arn = aws_lb_target_group.nginx.arn
197+
- target_id = aws_instance.nginx2.id
198+
- port = 80
199+
-}
200+
```
201+
202+
## Clean Up
203+
204+
```bash
205+
terraform destroy
206+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Actualizando S3 Bucket Objects
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 d4.tfplan
10+
terraform apply "d4.tfplan"
11+
```
12+
13+
## Pasos
14+
15+
Actualizamos `s3.tf`
16+
17+
```diff
18+
## aws_s3_bucket_object
19+
+resource "aws_s3_bucket_object" "website_content" {
20+
+ for_each = {
21+
+ website = "/website/index.html"
22+
+ logo = "/website/fruits.png"
23+
+ }
24+
+ bucket = aws_s3_bucket.web_bucket.bucket
25+
+ key = each.value
26+
+ source = ".${each.value}"
27+
+
28+
+ tags = local.common_tags
29+
+}
30+
- resource "aws_s3_bucket_object" "website" {
31+
- bucket = aws_s3_bucket.web_bucket.bucket
32+
- key = "/website/index.html"
33+
- source = "./website/index.html"
34+
-
35+
- tags = local.common_tags
36+
- }
37+
-
38+
- resource "aws_s3_bucket_object" "graphic" {
39+
- bucket = aws_s3_bucket.web_bucket.bucket
40+
- key = "/website/fruits.png"
41+
- source = "./website/fruits.png"
42+
-
43+
- tags = local.common_tags
44+
- }
45+
46+
```
47+
48+
49+
## Clean Up
50+
51+
```bash
52+
terraform destroy
53+
```
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Testeando las Funciones con la Consola de Terraform
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 d4.tfplan
10+
terraform apply "d4.tfplan"
11+
```
12+
13+
## Pasos
14+
15+
### Paso 1. Console setup
16+
17+
Necesitamos inicializar la configuración para que la consola de Terraform funcione. En nuestro caso ya esta inicializada así que no deberíamos tener que preocuparnos por esto.
18+
19+
```bash
20+
terraform init
21+
```
22+
23+
```bash
24+
Initializing the backend...
25+
26+
Initializing provider plugins...
27+
- Reusing previous version of hashicorp/random from the dependency lock file
28+
- Reusing previous version of hashicorp/aws from the dependency lock file
29+
- Using previously-installed hashicorp/random v3.1.0
30+
- Using previously-installed hashicorp/aws v3.70.0
31+
32+
Terraform has been successfully initialized!
33+
34+
You may now begin working with Terraform. Try running "terraform plan" to see
35+
any changes that are required for your infrastructure. All Terraform commands
36+
should now work.
37+
38+
If you ever set or change modules or backend configuration for Terraform,
39+
rerun this command to reinitialize your working directory. If you forget, other
40+
commands will detect it and remind you to do so if necessary.
41+
```
42+
43+
### Paso 2. Arrancar la consola.
44+
45+
```bash
46+
terraform console
47+
```
48+
49+
Ahora podemos probar diferentes funciones
50+
51+
```bash
52+
min(89, 90, 4)
53+
lower("TOMATO")
54+
```
55+
56+
Vamos a testear `cidrsubnet` alimentando el valor `vpc_cidr_block`. Podemos comprobar su valor por defecto de la siguiente forma:
57+
58+
[cidrsunet docs](https://www.terraform.io/language/functions/cidrsubnet)
59+
60+
```
61+
> var.vpc_cidr_block
62+
"10.0.0.0/16
63+
```
64+
65+
Y la podemos usar de la siguiente manera
66+
67+
```
68+
> cidrsubnet(var.vpc_cidr_block, 8, 0)
69+
"10.0.0.0/24"
70+
> cidrsubnet(var.vpc_cidr_block, 8, 4)
71+
"10.0.4.0/24"
72+
```
73+
74+
Otra funcion interesante es `lookup`, que nos ayuda a encontrar un valor en un `map`
75+
76+
```
77+
> lookup(local.common_tags, "company", "Unknown")
78+
"Lemoncode"
79+
> lookup(local.common_tags, "missing", "Unknown")
80+
"Unknown"
81+
```
82+
83+
Por supuesto, podemos seguir simplemente pidiendo un valor como hemos hecho previamente:
84+
85+
```
86+
> local.common_tags
87+
{
88+
"billing_code" = "FOO9999"
89+
"company" = "Lemoncode"
90+
"project" = "Lemoncode-web-app"
91+
}
92+
```
93+
94+
## Clean Up
95+
96+
```bash
97+
terraform destroy
98+
```

0 commit comments

Comments
 (0)