AWS, Terraform
AWS con Terraform (I)
Terraform es una herramienta para crear infraestructuras mediante código. Su potencia radica no solo en la sencillez de su código sino también el amplio soporte de diferentes arquitecturas. Vamos a empezar con una infraestructura fácil . Esta infraestructura no tiene coste es el “Free Tier” de amazon.
- 1 VPC
- 1 Internet Gateway
- 2 Subnets Publicas
- 2 Instancias
VPC
Elementos | configuración |
---|---|
Nombre | vpc_ejemplo |
Alcance de la red | 10.0.0.0/16 |
INTERNET GATEWAY
Elementos | configuración |
---|---|
Nombre | igw |
vpc | vpc_ejempo |
SUBNET 1
Elementos | configuración |
---|---|
Nombre | subnet1 |
vpc | vpc_ejemplo |
red | 10.0.1.0/24 |
Zona de disponibilidad | eu-west-1a |
SUBNET 2
Elementos | configuración |
---|---|
Nombre | subnet2 |
vpc | vpc_ejemplo |
red | 10.0.2.0/24 |
Zona de disponibilidad | eu-west-1b |
TABLA DE RUTAS
Elementos | configuración |
---|---|
Nombre | rutas |
vpc | vpc_ejemplo |
Alcance de la red | 0.0.0.0/0 |
Objetivo | Internet gw(igw) |
Subnet adjunta | subnet1,subnet2 |
La forma mas fácil de empezar a trabajar con terraform es creando todos los ficheros de configuración (*.tf) en el mismo directorio. En otra entrega veremos como organizar mejor el proyecto y el uso de modulos. Creamos dos ficheros “vars.tf” donde vamos a poner todas las variables y “main.tf” donde se configuraran los recursos
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 |
provider "aws" { access_key = "AKIAIAABBCCFL44UA" secret_key = "wpVWuoiwerABCDFmEBDPy4fgvXOEf9n6NKqFiZe" region = "eu-west-1" } variable "cidr" { default = "10.0.0.0/16" } variable "subnetsprivadas" { type = "list" description = "subnets privadas" default = ["10.0.1.0/24","10.0.2.0/24"] } variable "apacheport" { type = "string" description = "Puerto Apache" default = "80" } variable "sshport" { type = "string" description = "Puerto SSH" default = "22" } variable "amis" { type = "string" description = "Amis usadas" default = "ami-58d7e821" } |
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 |
resource "aws_vpc" "vpc_ejemplo" { cidr_block = "${var.cidr}" tags { Name = "vpc_ejemplo" } } resource "aws_subnet" "subnetsprivadas" { count = 2 vpc_id = "${aws_vpc.vpc_ejemplo.id}" cidr_block = "${element(var.subnetsprivadas,count.index)}" tags { Name = "subnet${count.index+1}" } } resource "aws_internet_gateway" "igw" { vpc_id = "${aws_vpc.vpc_ejemplo.id}" tags { Name = "igw" } } resource "aws_route_table" "internet" { vpc_id = "${aws_vpc.vpc_ejemplo.id}" route { cidr_block = "0.0.0.0/0" gateway_id = "${aws_internet_gateway.igw.id}" } tags { Name = "igw" } } resource "aws_route_table_association" "ruta" { count = 2 subnet_id = "${element(aws_subnet.subnetsprivadas.*.id,count.index)}" route_table_id = "${aws_route_table.internet.id}" } resource "aws_security_group" "apacheSG" { name = "apacheSG" description = "permite 80 y 22" vpc_id = "${aws_vpc.vpc_ejemplo.id}" ingress { from_port = "${var.apacheport}" to_port = "${var.apacheport}" protocol = "tcp" cidr_blocks = ["${var.cidr}"] } ingress { from_port = "${var.sshport}" to_port = "${var.sshport}" protocol = "tcp" cidr_blocks = ["${var.cidr}"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags { "Name" = "ApacheSG" } } resource "aws_instance" "apache1" { ami = "${var.amis}" instance_type = "t2.micro" key_name = "ireland" vpc_security_group_ids = ["${aws_security_group.apacheSG.id}"] subnet_id = "${element(aws_subnet.subnetsprivadas.*.id,0)}" associate_public_ip_address = true source_dest_check = false tags { Name = "apache1" } } resource "aws_instance" "apache2" { ami = "${var.amis}" instance_type = "t2.micro" key_name = "ireland" vpc_security_group_ids = ["${aws_security_group.apacheSG.id}"] subnet_id = "${element(aws_subnet.subnetsprivadas.*.id,1)}" associate_public_ip_address = true source_dest_check = false tags { Name = "apache2" } } |
1 |
terraform init |
1 |
terraform plan |
1 |
terraform apply |
1 |
terraform destroy |