1
+ # https://docs.aws.amazon.com/glue/latest/dg/set-up-vpc-dns.html
2
+ resource "aws_vpc" "this" {
3
+ cidr_block = var. vpc_cidr
4
+ # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc#enable_dns_support
5
+ enable_dns_support = true
6
+ # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc#enable_dns_hostnames
7
+ enable_dns_hostnames = true
8
+ # checkov:skip=CKV2_AWS_11: Not creating a flow log for this VPC
9
+ tags = {
10
+ " Name" = " app-4"
11
+ }
12
+ }
13
+ data "aws_availability_zones" "available" {
14
+ state = " available"
15
+ }
16
+ resource "aws_subnet" "private" {
17
+ count = length (var. subnet_cidr_private )
18
+ vpc_id = aws_vpc. this . id
19
+ cidr_block = var. subnet_cidr_private [count . index ]
20
+ availability_zone = data. aws_availability_zones . available . names [(count. index ) % length (data. aws_availability_zones . available . names )]
21
+ tags = {
22
+ " Name" = " app-4-private-${ count . index + 1 } "
23
+ }
24
+ }
25
+ resource "aws_subnet" "public" {
26
+ count = length (var. subnet_cidr_public )
27
+ vpc_id = aws_vpc. this . id
28
+ cidr_block = var. subnet_cidr_public [count . index ]
29
+ availability_zone = data. aws_availability_zones . available . names [(count. index ) % length (data. aws_availability_zones . available . names )]
30
+ tags = {
31
+ " Name" = " app-4-public-${ count . index + 1 } "
32
+ }
33
+ }
34
+ resource "aws_route_table" "private" {
35
+ count = length (var. subnet_cidr_private )
36
+ vpc_id = aws_vpc. this . id
37
+ tags = {
38
+ " Name" = " app-4-private-route-table-${ count . index + 1 } "
39
+ }
40
+ }
41
+ resource "aws_route_table" "public" {
42
+ vpc_id = aws_vpc. this . id
43
+ tags = {
44
+ " Name" = " app-4-public"
45
+ }
46
+ }
47
+ resource "aws_route_table_association" "private" {
48
+ count = length (var. subnet_cidr_private )
49
+ subnet_id = element (aws_subnet. private . * . id , count. index )
50
+ route_table_id = aws_route_table. private [count . index ]. id
51
+ }
52
+ resource "aws_route_table_association" "public" {
53
+ count = length (var. subnet_cidr_public )
54
+ subnet_id = element (aws_subnet. public . * . id , count. index )
55
+ route_table_id = aws_route_table. public . id
56
+ }
57
+ resource "aws_internet_gateway" "this-igw" {
58
+ vpc_id = aws_vpc. this . id
59
+ tags = {
60
+ " Name" = " app-4-gateway"
61
+ }
62
+ }
63
+ resource "aws_route" "internet-route" {
64
+ destination_cidr_block = " 0.0.0.0/0"
65
+ route_table_id = aws_route_table. public . id
66
+ gateway_id = aws_internet_gateway. this-igw . id
67
+ }
68
+ resource "aws_eip" "nat_gateway" {
69
+ count = length (var. subnet_cidr_public )
70
+ domain = " vpc"
71
+ # checkov:skip=CKV2_AWS_19: The IP is attached to the NAT gateway
72
+ }
73
+ resource "aws_nat_gateway" "public" {
74
+ count = length (var. subnet_cidr_public )
75
+ subnet_id = element (aws_subnet. public . * . id , count. index )
76
+ allocation_id = aws_eip. nat_gateway [count . index ]. id
77
+ depends_on = [aws_internet_gateway . this-igw ]
78
+ tags = {
79
+ " Name" = " app-4-NAT-${ count . index + 1 } "
80
+ }
81
+ }
82
+ resource "aws_route" "private-route" {
83
+ count = length (var. subnet_cidr_private )
84
+ destination_cidr_block = " 0.0.0.0/0"
85
+ route_table_id = aws_route_table. private [count . index ]. id
86
+ nat_gateway_id = aws_nat_gateway. public [count . index ]. id
87
+ }
0 commit comments