Skip to content

Commit dac0cfb

Browse files
authored
Merge pull request #66 from Lemoncode/feat/iac
Feat/iac
2 parents 2c8ca8c + 22feb7b commit dac0cfb

File tree

38 files changed

+4170
-0
lines changed

38 files changed

+4170
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ app/
2525

2626
# CDK8s demos
2727
*/08-ckd8s/00-hello-world-code/
28+
29+
# Private view
30+
.aws/
31+
lab/
32+
private.readme.md

04-cloud/01-eks/06-autoscalling-our-applications/00-install-kube-ops-view.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ kubectl get svc kube-ops-view | tail -n 1 | awk '{ print "Kube-ops-view URL = ht
3939

4040
This will display a line similar to Kube-ops-view URL = http://<URL_PREFIX_ELB>.amazonaws.com Opening the URL in your browser will provide the current state of our cluster.
4141

42+
> Reference: https://kubernetes-operational-view.readthedocs.io/en/latest/
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 = "AWS_ACCESS_KEY"
4+
secret_key = "AWS_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>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
82+
EOF
83+
84+
}
41.4 KB
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html>
2+
<head>
3+
<title>LemonLand Test Site</title>
4+
</head>
5+
<body style="background-color:#FFFFFF">
6+
<p style="text-align: center;">
7+
<img src="fruits.png" alt="YEAH!" style="margin-left:auto;margin-right:auto">
8+
</p>
9+
10+
</body>
11+
</html>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## Instalando Terraform
2+
3+
La instalación de Terraform es un simple ejecutable que añadiremos a nuestro path. Podemos usar los gestores de paquetes de nuestro sistema operativo para instalarlo.
4+
5+
## Instalación Manual
6+
7+
Este paso es común a todos los sistemas operativos.
8+
9+
Descargar el paquete adecuado para nuestro sistema operativo en [este enlace](https://www.terraform.io/downloads).
10+
11+
### Instalación Manual Mac o Linux
12+
13+
Una vez descargado lo descomprimimos, y lo movemos al directorio de ejectutables.
14+
15+
```bash
16+
mv ~/Downloads/terraform /usr/local/bin/
17+
```
18+
19+
Para confirmar la instalación, simplemente ejecutar:
20+
21+
```bash
22+
terraform -help
23+
```
24+
25+
### Instalación Manual Windows
26+
27+
Una vez descargado lo descomprimimos y lo ubicamos en una carpeta fácilmente reconocible por ejemplo `C:\terraform`.
28+
29+
Para editar el `PATH`, seguiremos los siguientes pasos:
30+
31+
1. Ir a `Control panel` --> `System` --> `System settings` --> `Environment Variables`
32+
2. Buscar `PATH`
33+
3. Pulsar `edit` y modificarlo de forma adecuada
34+
4. Asegurar que hemos incluido `;` --> `c:\path;c:\path2`
35+
36+
## Referencias
37+
38+
[Terraform Downloads](https://www.terraform.io/downloads)
39+
[Where can I set up path xxxx.exe on Windows?](https://stackoverflow.com/questions/1618280/where-can-i-set-path-to-make-exe-on-windows)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Introducción
2+
3+
## Infraestructura como código
4+
5+
> Proveer infraestructura a través del software para conseguir despliegues consistentes y predecibles.
6+
7+
## Conceptos fundamentales
8+
9+
* Definido en Código
10+
* Almacenamiento en control de versiones
11+
* Declarativo o imperativo
12+
* Idempotente y consistente
13+
* Push o Pull
14+
15+
## Declarativo vs Imperativo
16+
17+
```ini
18+
# Make me a campero
19+
get mollete
20+
get ham
21+
get cheese
22+
get tomato
23+
get lettuce
24+
get mayonnaise
25+
26+
put ham in mollete
27+
put cheese on ham
28+
put lettuce on cheese
29+
put tomato on lettuce
30+
```
31+
32+
```ini
33+
# Make me a campero
34+
get mollete
35+
get ham
36+
get cheese
37+
get tomato
38+
get lettuce
39+
get mayonnaise
40+
41+
put ham in mollete
42+
put cheese on ham
43+
put lettuce on cheese
44+
put tomato on lettuce
45+
put mayonaise on tomato
46+
```
47+
48+
```ini
49+
# Make me a campero
50+
food campero "classic-campero" {
51+
ingridients = [
52+
"ham", "cheese", "tomato", "lettuce", "mayonnaise"
53+
]
54+
}
55+
```
56+
put mayonaise on tomato
57+
```
58+
59+
```ini
60+
# Make me a campero
61+
food campero "classic-campero" {
62+
ingridients = [
63+
"ham", "cheese", "tomato", "lettuce", "mayonnaise"
64+
]
65+
}
66+
```
67+
68+
> Terraform es un ejemplo de aproximación declarativa para desarrollar Infraestructure as Code.
69+
70+
## Idempotente y Consistente
71+
72+
* Mi amigo: Hazme un Campero
73+
* Mi respuesta Toma tu Campero
74+
75+
En un mundo **idempotente**, si **mi amigo pregunta de nuevo** por el `campero`
76+
77+
* Mi amigo: Hazme un Campero
78+
* Mi respuesta Toma tu Campero
79+
80+
No te voy a hacer otro porque ya tienes uno.
81+
82+
> Terrafom intenta ser idempotente en el sentido de que si n has cambiado nada en tu código y lo aplicas de nuevo al mismo entorno, nada cambiará en el entorno porque tu código coincide con la realidad de la infraestructura que existe.
83+
84+
## Push or Pull
85+
86+
* ¿Pushing o Pulling al entorno?
87+
* Terraform es de tipo PUSH
88+
89+
## Beneficios de la Infraestructura como Código
90+
91+
* Despliegues automatizados
92+
* Proceso repetible
93+
* Entornos consistentes
94+
* Componentes reusables
95+
* Arquitectura documentada
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+
```

0 commit comments

Comments
 (0)