-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublic_vpc.tf
More file actions
66 lines (56 loc) · 1.9 KB
/
Copy pathpublic_vpc.tf
File metadata and controls
66 lines (56 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# The resources are created conditionally based on the value of the create_vpc variable.
# Justification: This is for development purposes, Flow Logs and other features are not required for a red instance.
# trivy:ignore:AVD-AWS-0178
resource "aws_vpc" "main" {
count = var.create_vpc ? 1 : 0
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = merge(
local.tags,
{ Name = "${lower(var.instance_name)}-red-instance-vpc" },
)
}
# Create a public subnet with smart AZ selection
# Justification: This is a public subnet for the red instance
# trivy:ignore:AVD-AWS-0164
resource "aws_subnet" "public" {
count = var.create_vpc ? 1 : 0
vpc_id = aws_vpc.main[0].id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
# Only specify AZ if explicitly provided
availability_zone = var.availability_zone != "" ? var.availability_zone : null
tags = merge(
local.tags,
{ Name = "${lower(var.instance_name)}-red-instance-public-subnet" },
)
}
# Create an internet gateway
resource "aws_internet_gateway" "igw" {
count = var.create_vpc ? 1 : 0
vpc_id = aws_vpc.main[0].id
tags = merge(
local.tags,
{ Name = "${lower(var.instance_name)}-red-instance-igw" },
)
}
# Create a route table and associate it with the public subnet
resource "aws_route_table" "public" {
count = var.create_vpc ? 1 : 0
vpc_id = aws_vpc.main[0].id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw[0].id
}
tags = merge(
local.tags,
{ Name = "${lower(var.instance_name)}-red-instance-public-route-table" },
)
}
# Associate the route table with the public subnet
resource "aws_route_table_association" "public" {
count = var.create_vpc ? 1 : 0
subnet_id = aws_subnet.public[0].id
route_table_id = aws_route_table.public[0].id
}