Skip to content

Commit 01c12aa

Browse files
committed
desplegando config base
1 parent b039cec commit 01c12aa

4 files changed

Lines changed: 324 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# PROVIDERS
2+
provider "aws" {
3+
access_key = "ACCESS_KEY"
4+
secret_key = "SECRET_KEY"
5+
region = "eu-west-3"
6+
}
7+
8+
# DATA
9+
data "aws_ssm_parameter" "ami" {
10+
name = "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
11+
}
12+
13+
# RESOURCES
14+
15+
# NETWORKING #
16+
resource "aws_vpc" "vpc" {
17+
cidr_block = "10.0.0.0/16"
18+
enable_dns_hostnames = "true"
19+
}
20+
21+
resource "aws_internet_gateway" "igw" {
22+
vpc_id = aws_vpc.vpc.id
23+
}
24+
25+
resource "aws_subnet" "subnet1" {
26+
cidr_block = "10.0.0.0/24"
27+
vpc_id = aws_vpc.vpc.id
28+
map_public_ip_on_launch = "true"
29+
}
30+
31+
# ROUTING #
32+
resource "aws_route_table" "rtb" {
33+
vpc_id = aws_vpc.vpc.id
34+
35+
route {
36+
cidr_block = "0.0.0.0/0"
37+
gateway_id = aws_internet_gateway.igw.id
38+
}
39+
}
40+
41+
resource "aws_route_table_association" "rta-subnet1" {
42+
subnet_id = aws_subnet.subnet1.id
43+
route_table_id = aws_route_table.rtb.id
44+
}
45+
46+
# SECURITY GROUPS #
47+
48+
resource "aws_security_group" "nginx-sg" {
49+
name = "nginx_sg"
50+
vpc_id = aws_vpc.vpc.id
51+
52+
# HTTP access from anywhere
53+
ingress {
54+
from_port = 80
55+
to_port = 80
56+
protocol = "tcp"
57+
cidr_blocks = ["0.0.0.0/0"]
58+
}
59+
60+
# outbound internet access
61+
egress {
62+
from_port = 0
63+
to_port = 0
64+
protocol = "-1"
65+
cidr_blocks = ["0.0.0.0/0"]
66+
}
67+
}
68+
69+
# INSTANCES #
70+
resource "aws_instance" "nginx1" {
71+
ami = nonsensitive(data.aws_ssm_parameter.ami.value)
72+
instance_type = "t2.micro"
73+
subnet_id = aws_subnet.subnet1.id
74+
vpc_security_group_ids = [aws_security_group.nginx-sg.id]
75+
76+
user_data = <<EOF
77+
#! /bin/bash
78+
sudo amazon-linux-extras install -y nginx1
79+
sudo service nginx start
80+
sudo rm /usr/share/nginx/html/index.html
81+
echo '<html><head><title>Taco Team Server</title></head><body style=\"background-color:#1F778D\"><p style=\"text-align: center;\"><span style=\"color:#FFFFFF;\"><span style=\"font-size:28px;\">You did it! Have a &#127790;</span></span></p></body></html>' | sudo tee /usr/share/nginx/html/index.html
82+
EOF
83+
84+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Desplegando la configuración base
2+
3+
## AWS account
4+
5+
Crear una nueva cuenta AWS
6+
7+
## Pasos
8+
9+
### Paso 1. Crear directory de trabajo
10+
11+
Creamos los nuevos drirectorios `lab/lc_web_app` y dentro añadimo el fichero `main.tf`
12+
13+
```bash
14+
mkdir -p lab/lc_web_app
15+
cp ./.start-app/main.tf ./lab/lc_web_app
16+
```
17+
18+
Cambiamos el directorio de trabajo a `lab/lc_web_app`
19+
20+
```bash
21+
cd ./lab/lc_web_app
22+
```
23+
24+
### Paso 2. Actualizamos las crednciales
25+
26+
Actualizar `access_key` y `secret_key`
27+
28+
```diff
29+
provider "aws" {
30+
+ access_key = "ACCESS_KEY"
31+
+ secret_key = "SECRET_KEY"
32+
region = "eu-west-3"
33+
}
34+
```
35+
36+
### Paso 3. Comenzar la inicialización
37+
38+
```bash
39+
terraform init
40+
```
41+
42+
Obtendremos un output similar a este:
43+
44+
```
45+
Initializing the backend...
46+
47+
Initializing provider plugins...
48+
- Finding latest version of hashicorp/aws...
49+
- Installing hashicorp/aws v3.70.0...
50+
- Installed hashicorp/aws v3.70.0 (signed by HashiCorp)
51+
52+
Terraform has created a lock file .terraform.lock.hcl to record the provider
53+
selections it made above. Include this file in your version control repository
54+
so that Terraform can guarantee to make the same selections by default when
55+
you run "terraform init" in the future.
56+
57+
Terraform has been successfully initialized!
58+
59+
You may now begin working with Terraform. Try running "terraform plan" to see
60+
any changes that are required for your infrastructure. All Terraform commands
61+
should now work.
62+
63+
If you ever set or change modules or backend configuration for Terraform,
64+
rerun this command to reinitialize your working directory. If you forget, other
65+
commands will detect it and remind you to do so if necessary.
66+
```
67+
68+
### Paso 4. Obtener un Plan
69+
70+
Ahora que nuestra configuración ha sido inicializada, podemos obtener un plan:
71+
72+
```bash
73+
terraform plan -out d1.tfplan
74+
```
75+
76+
> Con este comando, estamos indicando que queremos el `plan` volcado sobre el fichero `d1.tfpplan`
77+
78+
Terraform nos indica las acciones que va a relizar.
79+
80+
```
81+
Plan: 7 to add, 0 to change, 0 to destroy.
82+
83+
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
84+
85+
Saved the plan to: d1.tfplan
86+
87+
To perform exactly these actions, run the following command to apply:
88+
terraform apply "d1.tfplan"
89+
90+
```
91+
92+
### Paso 5. Aplica el Plan
93+
94+
```bash
95+
terraform apply "d1.tfplan"
96+
```
97+
98+
Si aplicamos este comando, sin alimentar un plan, nos mostrarán un diálogo de confirmación de plan previo.
99+
100+
Después de unos minutos el despliegue finaliza, podemos acceder a la consola de AWS
101+
102+
## Clean Up
103+
104+
```bash
105+
terraform destroy
106+
```
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Desplegando la Configuración Base
2+
3+
## Revisando la configuración base
4+
5+
Abrimos `./.start-app/main.tf`
6+
7+
```tf
8+
provider "aws" {
9+
access_key = "ACCESS_KEY"
10+
secret_key = "SECRET_KEY"
11+
region = "us-east-1"
12+
}
13+
```
14+
15+
Este bloque le indica a Terraform que usaremos `AWS` como **provider**.
16+
17+
```tf
18+
data "aws_ssm_parameter" "ami" {
19+
name = "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
20+
}
21+
```
22+
23+
[Blog reference](https://aws.amazon.com/blogs/compute/query-for-the-latest-amazon-linux-ami-ids-using-aws-systems-manager-parameter-store/)
24+
25+
Este es un **service manager parameter**, al cual le damos como nombre de etiqueta `ami`. Dentro de este bloque, estamos alimentando el `path` a un parámetro en particular. En este caso, el parametro devuelve la última Amazon Linux 2 AMI ID.
26+
27+
En la sección de `NETWORKING` creamos la `VPC`
28+
29+
```tf
30+
resource "aws_vpc" "vpc" {
31+
cidr_block = "10.0.0.0/16"
32+
enable_dns_hostnames = "true"
33+
34+
}
35+
```
36+
37+
Después creamos la `internet gateway`, y lo asociamos con la VPC que creamos previamente. Para tal fin usamos `vpc_id = aws_vpc.vpc.id`
38+
39+
```tf
40+
resource "aws_internet_gateway" "igw" {
41+
vpc_id = aws_vpc.vpc.id
42+
43+
}
44+
```
45+
46+
> ¿Cómo sabemos que argumentos y atributos están disponibles para un recurso? Tenemos que leer la documentación `;)`: https://registry.terraform.io/providers/hashicorp/aws/latest/docs
47+
48+
Creamos una `subnet` asociada a la `VPC`. Gracias a esta entrada `map_public_ip_on_launch = "true"`, obtenemos una IP pública
49+
50+
```tf
51+
resource "aws_subnet" "subnet1" {
52+
cidr_block = "10.0.0.0/24"
53+
vpc_id = aws_vpc.vpc.id
54+
map_public_ip_on_launch = "true"
55+
}
56+
```
57+
58+
Creamos una `route table`, y la asociamos a nuestra `VPC`. Para ver la documentación oficial de este recurso, seguir el siguiente enlace [Route tables official Docs](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Route_Tables.html)
59+
60+
```tf
61+
resource "aws_route_table" "rtb" {
62+
vpc_id = aws_vpc.vpc.id
63+
64+
route {
65+
cidr_block = "0.0.0.0/0"
66+
gateway_id = aws_internet_gateway.igw.id
67+
}
68+
}
69+
```
70+
71+
En el bloque anidado, podemos especificar un `route` para añadir a la `route table`. En este caso estamos creando una `default route` y la apuntamos a la `internet gateway`. De esta manera el tráfico puede salir fuera de la `VPC` a través de la `internet gateway`.
72+
73+
Por último asociamos nuestra `route table` con una única `subnet`
74+
75+
```tf
76+
resource "aws_route_table_association" "rta-subnet1" {
77+
subnet_id = aws_subnet.subnet1.id
78+
route_table_id = aws_route_table.rtb.id
79+
}
80+
```
81+
82+
Creamos un `security group` que permita al puerto 80 de cualquier dirección hablar nuestra instancia `EC2`
83+
84+
```tf
85+
# Nginx security group
86+
resource "aws_security_group" "nginx-sg" {
87+
name = "nginx_sg"
88+
vpc_id = aws_vpc.vpc.id
89+
90+
# HTTP access from anywhere
91+
ingress {
92+
from_port = 80
93+
to_port = 80
94+
protocol = "tcp"
95+
cidr_blocks = ["0.0.0.0/0"]
96+
}
97+
98+
# outbound internet access
99+
egress {
100+
from_port = 0
101+
to_port = 0
102+
protocol = "-1"
103+
cidr_blocks = ["0.0.0.0/0"]
104+
}
105+
}
106+
```
107+
108+
Estamos asocaindo este `security group` con nuestra `VPC`, y estamos creando un único `ingress group` usando un bloque anidado, y dentro de este, establecemos la entrada `from_port` y `to_port` al puerto 80, el protocolo TCP y el `cidr_block` que refiere a todas las direcciones.
109+
110+
Por último tenemos la instancia EC2.
111+
112+
```tf
113+
resource "aws_instance" "nginx1" {
114+
ami = nonsensitive(data.aws_ssm_parameter.ami.value)
115+
instance_type = "t2.micro"
116+
subnet_id = aws_subnet.subnet1.id
117+
vpc_security_group_ids = [aws_security_group.nginx-sg.id]
118+
119+
user_data = <<EOF
120+
#! /bin/bash
121+
sudo amazon-linux-extras install -y nginx1
122+
sudo service nginx start
123+
sudo rm /usr/share/nginx/html/index.html
124+
echo '<html><head><title>Taco Team 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
125+
EOF
126+
127+
}
128+
```
129+
130+
> NOTA: Explicar el Workflow de Terraform
131+
132+
Ahora estamos listos para aplicar el contenido de `main.tf`
133+
134+
[Desplegando la configuración base - Demo 01](01-demo)

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

Whitespace-only changes.

0 commit comments

Comments
 (0)