|
| 1 | +# ------- VPC ------- |
| 2 | +# Create the VPC's |
| 3 | +resource "aws_vpc" "cdp_vpc" { |
| 4 | + cidr_block = var.vpc_cidr |
| 5 | + tags = merge(var.env_tags,{Name = var.vpc_name}) |
| 6 | + |
| 7 | + instance_tenancy = "default" |
| 8 | + enable_dns_support = true |
| 9 | + enable_dns_hostnames = true |
| 10 | +} |
| 11 | + |
| 12 | +# ------- AWS Public Network infrastructure ------- |
| 13 | +# Internet Gateway |
| 14 | +resource "aws_internet_gateway" "cdp_igw" { |
| 15 | + vpc_id = aws_vpc.cdp_vpc.id |
| 16 | + tags = merge(var.env_tags,{Name = var.igw_name}) |
| 17 | +} |
| 18 | + |
| 19 | +# AWS VPC Public Subnets |
| 20 | +resource "aws_subnet" "cdp_public_subnets" { |
| 21 | + for_each = {for idx, subnet in var.public_subnets: idx => subnet} |
| 22 | + |
| 23 | + vpc_id = aws_vpc.cdp_vpc.id |
| 24 | + cidr_block = each.value.cidr |
| 25 | + map_public_ip_on_launch = true |
| 26 | + availability_zone = each.value.az |
| 27 | + tags = merge(var.env_tags,each.value.tags) |
| 28 | +} |
| 29 | + |
| 30 | +# Public Route Table |
| 31 | +resource "aws_default_route_table" "cdp_public_route_table" { |
| 32 | + default_route_table_id = aws_vpc.cdp_vpc.default_route_table_id |
| 33 | + |
| 34 | + route { |
| 35 | + cidr_block = "0.0.0.0/0" |
| 36 | + gateway_id = aws_internet_gateway.cdp_igw.id |
| 37 | + } |
| 38 | + |
| 39 | + tags = merge(var.env_tags,{Name = var.public_route_table_name}) |
| 40 | + |
| 41 | +} |
| 42 | + |
| 43 | +# Associate the Public Route Table with the Public Subnets |
| 44 | +resource "aws_route_table_association" "cdp_public_subnets" { |
| 45 | + |
| 46 | + for_each = aws_subnet.cdp_public_subnets |
| 47 | + |
| 48 | + subnet_id = each.value.id |
| 49 | + route_table_id = aws_vpc.cdp_vpc.default_route_table_id |
| 50 | +} |
| 51 | + |
| 52 | +# ------- AWS Private Networking infrastructure ------- |
| 53 | + |
| 54 | +# AWS VPC Private Subnets |
| 55 | +resource "aws_subnet" "cdp_private_subnets" { |
| 56 | + for_each = {for idx, subnet in var.private_subnets: idx => subnet} |
| 57 | + |
| 58 | + vpc_id = aws_vpc.cdp_vpc.id |
| 59 | + cidr_block = each.value.cidr |
| 60 | + map_public_ip_on_launch = false |
| 61 | + availability_zone = each.value.az |
| 62 | + tags = merge(var.env_tags,each.value.tags) |
| 63 | +} |
| 64 | + |
| 65 | +# Private Route Table for the AWS VPC |
| 66 | +# - Not implemeted in Terraform because of "when: no" in Ansible |
| 67 | + |
| 68 | +# Elastic IP for each NAT gateway |
| 69 | +resource "aws_eip" "cdp_nat_gateway_eip" { |
| 70 | + |
| 71 | + for_each = {for idx, subnet in var.public_subnets: idx => subnet} |
| 72 | + |
| 73 | + vpc = true |
| 74 | + tags = merge(var.env_tags,{Name = format("%s-%s-%02d", var.nat_gateway_name, "eip", index(var.public_subnets, each.value)+1)}) |
| 75 | +} |
| 76 | + |
| 77 | +# Network Gateways (NAT) |
| 78 | +resource "aws_nat_gateway" "cdp_nat_gateway" { |
| 79 | + |
| 80 | + # for_each = { for s in aws_subnet.cdp_public_subnets : s.id => s } |
| 81 | + count = length(aws_subnet.cdp_public_subnets) |
| 82 | + |
| 83 | + subnet_id = aws_subnet.cdp_public_subnets[count.index].id |
| 84 | + allocation_id = aws_eip.cdp_nat_gateway_eip[count.index].id |
| 85 | + connectivity_type = "public" |
| 86 | + |
| 87 | + tags = merge(var.env_tags,{Name = format("%s-%02d", var.nat_gateway_name, count.index)}) |
| 88 | + # tags = merge(var.env_tags,{Name = format("%s-%s-%02d", var.nat_gateway_name, "eip", index(aws_subnet.cdp_public_subnets, each.value)+1)}) |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +# Private Route Tables |
| 93 | +resource "aws_route_table" "cdp_private_route_table" { |
| 94 | + for_each = {for idx, subnet in var.private_subnets: idx => subnet} |
| 95 | + |
| 96 | + vpc_id = aws_vpc.cdp_vpc.id |
| 97 | + |
| 98 | + tags = merge(var.env_tags,{Name = format("%s-%02d", var.private_route_table_name, index(var.private_subnets, each.value))}) |
| 99 | + |
| 100 | + route { |
| 101 | + cidr_block = "0.0.0.0/0" |
| 102 | + #nat_gateway_id = aws_nat_gateway.cdp_nat_gateway[0].id |
| 103 | + |
| 104 | + nat_gateway_id = aws_nat_gateway.cdp_nat_gateway[(index(var.private_subnets, each.value) % length(aws_nat_gateway.cdp_nat_gateway))].id |
| 105 | + |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +# Associate the Private Route Tables with the Private Subnets |
| 110 | +resource "aws_route_table_association" "cdp_private_subnets" { |
| 111 | + |
| 112 | + count = length(aws_subnet.cdp_private_subnets) |
| 113 | + |
| 114 | + subnet_id = aws_subnet.cdp_private_subnets[count.index].id |
| 115 | + route_table_id = aws_route_table.cdp_private_route_table[count.index].id |
| 116 | +} |
| 117 | + |
| 118 | +# ------- Security Groups ------- |
| 119 | +# Default SG |
| 120 | +resource "aws_security_group" "cdp_default_sg" { |
| 121 | + vpc_id = aws_vpc.cdp_vpc.id |
| 122 | + name = var.security_group_default_name |
| 123 | + description = var.security_group_default_name |
| 124 | + |
| 125 | + tags = merge(var.env_tags,{Name = var.security_group_default_name}) |
| 126 | + |
| 127 | + # Create self reference ingress rule to allow |
| 128 | + # communication among resources in the security group. |
| 129 | + ingress { |
| 130 | + from_port = 0 |
| 131 | + to_port = 0 |
| 132 | + protocol = "all" |
| 133 | + self = true |
| 134 | + } |
| 135 | + |
| 136 | + # Dynamic Block to create security group rule from var.sg_ingress |
| 137 | + dynamic "ingress" { |
| 138 | + for_each = var.security_group_rules_ingress |
| 139 | + |
| 140 | + content { |
| 141 | + cidr_blocks = ingress.value.cidr |
| 142 | + from_port = ingress.value.from_port |
| 143 | + to_port = ingress.value.to_port |
| 144 | + protocol = ingress.value.protocol |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + # Terraform removes the default ALLOW ALL egress. Let's recreate this |
| 150 | + egress { |
| 151 | + cidr_blocks = ["0.0.0.0/0"] |
| 152 | + from_port = 0 |
| 153 | + to_port = 0 |
| 154 | + protocol = "all" |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +# Knox SG |
| 159 | +resource "aws_security_group" "cdp_knox_sg" { |
| 160 | + vpc_id = aws_vpc.cdp_vpc.id |
| 161 | + name = var.security_group_knox_name |
| 162 | + description = var.security_group_knox_name |
| 163 | + |
| 164 | + tags = merge(var.env_tags,{Name = var.security_group_knox_name}) |
| 165 | + |
| 166 | + # Create self reference ingress rule to allow |
| 167 | + # communication among resources in the security group. |
| 168 | + ingress { |
| 169 | + from_port = 0 |
| 170 | + to_port = 0 |
| 171 | + protocol = "all" |
| 172 | + self = true |
| 173 | + } |
| 174 | + |
| 175 | + # Dynamic Block to create security group rule from var.sg_ingress |
| 176 | + dynamic "ingress" { |
| 177 | + for_each = var.security_group_rules_ingress |
| 178 | + |
| 179 | + content { |
| 180 | + cidr_blocks = ingress.value.cidr |
| 181 | + from_port = ingress.value.from_port |
| 182 | + to_port = ingress.value.to_port |
| 183 | + protocol = ingress.value.protocol |
| 184 | + } |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | + # Terraform removes the default ALLOW ALL egress. Let's recreate this |
| 189 | + egress { |
| 190 | + cidr_blocks = ["0.0.0.0/0"] |
| 191 | + from_port = 0 |
| 192 | + to_port = 0 |
| 193 | + protocol = "all" |
| 194 | + } |
| 195 | +} |
0 commit comments