|
| 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 | +``` |
0 commit comments