Skip to content

Commit ec9f5b3

Browse files
authored
Merge pull request #4 from kunduso/add-network
Add network
2 parents af36236 + 236b864 commit ec9f5b3

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

network.tf

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

security_group.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resource "aws_default_security_group" "default" {
2+
vpc_id = aws_vpc.this.id
3+
}

variable.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,18 @@ variable "secret_key" {
1717
type = string
1818
sensitive = true
1919
default = ""
20+
}
21+
variable "vpc_cidr" {
22+
description = "the vpc cidr"
23+
default = "10.20.32.0/25"
24+
}
25+
variable "subnet_cidr_private" {
26+
description = "cidr blocks for the private subnets"
27+
default = ["10.20.30.0/27", "10.20.30.32/27", "10.20.30.64/27"]
28+
type = list(any)
29+
}
30+
variable "subnet_cidr_public" {
31+
description = "cidr blocks for the public subnets"
32+
default = ["10.20.30.96/27"]
33+
type = list(any)
2034
}

0 commit comments

Comments
 (0)