├── .gitignore ├── data.tf ├── internet_gw.tf ├── main.tf ├── nat.tf ├── routetable-private.tf ├── routetable.tf ├── subnets.tf ├── variables.tf └── vpc.tf /.gitignore: -------------------------------------------------------------------------------- 1 | .terraform 2 | .env 3 | plan -------------------------------------------------------------------------------- /data.tf: -------------------------------------------------------------------------------- 1 | data "aws_availability_zones" "available" {} -------------------------------------------------------------------------------- /internet_gw.tf: -------------------------------------------------------------------------------- 1 | resource "aws_internet_gateway" "gw" { 2 | vpc_id = aws_vpc.main.id 3 | 4 | tags = { 5 | Name = "iaasweek" 6 | } 7 | } -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "us-east-1" 3 | version = "~> 2.0" 4 | } 5 | 6 | terraform { 7 | backend "s3" { 8 | bucket = "iaasweek-tfstates-terraform" 9 | key = "terraformt.tfstate" 10 | region = "us-east-1" 11 | } 12 | } -------------------------------------------------------------------------------- /nat.tf: -------------------------------------------------------------------------------- 1 | resource "aws_eip" "gw" { 2 | count = var.az_count 3 | vpc = true 4 | 5 | tags = { 6 | Environment = "iaasweek" 7 | } 8 | } 9 | 10 | resource "aws_nat_gateway" "gw" { 11 | count = var.az_count 12 | subnet_id = element(aws_subnet.public.*.id, count.index) 13 | allocation_id = element(aws_eip.gw.*.id, count.index) 14 | 15 | tags = { 16 | Environment = "iaasweek" 17 | } 18 | } -------------------------------------------------------------------------------- /routetable-private.tf: -------------------------------------------------------------------------------- 1 | resource "aws_route_table" "private" { 2 | count = var.az_count 3 | vpc_id = aws_vpc.main.id 4 | 5 | route { 6 | cidr_block = "0.0.0.0/0" 7 | nat_gateway_id = element(aws_nat_gateway.gw.*.id, count.index) 8 | } 9 | 10 | tags = { 11 | Environment = "iaasweek" 12 | } 13 | } 14 | 15 | resource "aws_route_table_association" "private" { 16 | count = var.az_count 17 | subnet_id = element(aws_subnet.private.*.id, count.index) 18 | route_table_id = element(aws_route_table.private.*.id, count.index) 19 | } -------------------------------------------------------------------------------- /routetable.tf: -------------------------------------------------------------------------------- 1 | resource "aws_route" "internet_access" { 2 | route_table_id = aws_vpc.main.main_route_table_id 3 | destination_cidr_block = "0.0.0.0/0" 4 | gateway_id = aws_internet_gateway.gw.id 5 | } 6 | 7 | -------------------------------------------------------------------------------- /subnets.tf: -------------------------------------------------------------------------------- 1 | # Criar subnet public para cada AZ 2 | resource "aws_subnet" "private" { 3 | count = var.az_count 4 | cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index) 5 | availability_zone = data.aws_availability_zones.available.names[count.index] 6 | vpc_id = aws_vpc.main.id 7 | 8 | tags = { 9 | Name = "Private" 10 | } 11 | } 12 | 13 | # Criar subnet public para cada AZ 14 | resource "aws_subnet" "public" { 15 | count = var.az_count 16 | # var.az_count é usado para não conflitar com o private 17 | cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, var.az_count + count.index) 18 | availability_zone = data.aws_availability_zones.available.names[count.index] 19 | vpc_id = aws_vpc.main.id 20 | map_public_ip_on_launch = true 21 | 22 | tags = { 23 | Name = "Public" 24 | } 25 | } -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | variable "vpc_cidr_block" { 2 | description = "Range of IPv4 address for the VPC." 3 | default = "172.17.0.0/16" 4 | } 5 | 6 | variable "az_count" { 7 | default = "2" 8 | } -------------------------------------------------------------------------------- /vpc.tf: -------------------------------------------------------------------------------- 1 | resource "aws_vpc" "main" { 2 | cidr_block = var.vpc_cidr_block 3 | enable_dns_hostnames = true 4 | 5 | tags = { 6 | Name = "iaasweek" 7 | } 8 | } 9 | --------------------------------------------------------------------------------