github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/examples/terraform-aws-network-example/main.tf (about) 1 # --------------------------------------------------------------------------------------------------------------------- 2 # PIN TERRAFORM VERSION TO >= 0.12 3 # The examples have been upgraded to 0.12 syntax 4 # --------------------------------------------------------------------------------------------------------------------- 5 6 terraform { 7 # This module is now only being tested with Terraform 0.13.x. However, to make upgrading easier, we are setting 8 # 0.12.26 as the minimum version, as that version added support for required_providers with source URLs, making it 9 # forwards compatible with 0.13.x code. 10 required_version = ">= 0.12.26" 11 } 12 13 data "aws_availability_zones" "available" { 14 state = "available" 15 } 16 17 provider "aws" { 18 region = var.aws_region 19 } 20 21 # --------------------------------------------------------------------------------------------------------------------- 22 # DEPLOY A SIMPLE NETWORK 23 # The network has an internet gateway and two subnets - private and public - in the same availability zone. 24 # --------------------------------------------------------------------------------------------------------------------- 25 26 resource "aws_vpc" "main" { 27 cidr_block = var.main_vpc_cidr 28 29 tags = { 30 Name = var.tag_name 31 } 32 } 33 34 resource "aws_internet_gateway" "main_gateway" { 35 vpc_id = aws_vpc.main.id 36 37 tags = { 38 Name = var.tag_name 39 } 40 } 41 42 resource "aws_subnet" "private" { 43 vpc_id = aws_vpc.main.id 44 cidr_block = var.private_subnet_cidr 45 map_public_ip_on_launch = false 46 47 tags = { 48 Name = var.tag_name 49 } 50 51 availability_zone = data.aws_availability_zones.available.names[0] 52 } 53 54 resource "aws_subnet" "public" { 55 vpc_id = aws_vpc.main.id 56 cidr_block = var.public_subnet_cidr 57 map_public_ip_on_launch = true 58 59 tags = { 60 Name = var.tag_name 61 } 62 63 availability_zone = data.aws_availability_zones.available.names[0] 64 } 65 66 # --------------------------------------------------------------------------------------------------------------------- 67 # CREATE AND ATTACH A ROUTING TABLE FOR THE PUBLIC NETWORK 68 # --------------------------------------------------------------------------------------------------------------------- 69 70 resource "aws_route_table" "public" { 71 vpc_id = aws_vpc.main.id 72 73 route { 74 cidr_block = "91.189.0.0/24" 75 gateway_id = aws_internet_gateway.main_gateway.id 76 } 77 78 tags = { 79 Name = var.tag_name 80 } 81 } 82 83 resource "aws_route_table_association" "public" { 84 subnet_id = aws_subnet.public.id 85 route_table_id = aws_route_table.public.id 86 } 87 88 # --------------------------------------------------------------------------------------------------------------------- 89 # CREATE NAT GATEWAY FOR THE PRIVATE SUBNET 90 # --------------------------------------------------------------------------------------------------------------------- 91 92 resource "aws_eip" "nat" { 93 vpc = true 94 } 95 96 resource "aws_nat_gateway" "nat" { 97 allocation_id = aws_eip.nat.id 98 subnet_id = aws_subnet.public.id 99 depends_on = [aws_internet_gateway.main_gateway] 100 } 101 102 # --------------------------------------------------------------------------------------------------------------------- 103 # CREATE AND ATTACH A ROUTING TABLE FOR THE PRIVATE NETWORK 104 # --------------------------------------------------------------------------------------------------------------------- 105 106 resource "aws_route_table" "private" { 107 vpc_id = aws_vpc.main.id 108 109 route { 110 cidr_block = "0.0.0.0/0" 111 nat_gateway_id = aws_nat_gateway.nat.id 112 } 113 114 tags = { 115 Name = var.tag_name 116 } 117 } 118 119 resource "aws_route_table_association" "private" { 120 subnet_id = aws_subnet.private.id 121 route_table_id = aws_route_table.private.id 122 } 123