1- # TODO: optional based on flag: create_vpc
2- resource "aws_vpc" "main" {
1+ resource "aws_vpc" "main" {
2+ count = var . create_vpc == " true " ? 1 : 0
33 cidr_block = var. vpc_cidr
44 tags = {
55 Name = " ${ var . aws_resource_identifier } "
66 }
77}
8- # TODO: optional based on flag: create_vpc
98resource "aws_internet_gateway" "gw" {
10- vpc_id = aws_vpc. main . id
9+ count = var. create_vpc == " true" ? 1 : 0
10+ vpc_id = aws_vpc. main [0 ]. id
1111}
1212
13- # TODO: optional based on flag: create_vpc
1413# resource "aws_subnet" "private" {
15- # vpc_id = aws_vpc.main.id
16- # count = length(var.private_subnets)
14+ # vpc_id = aws_vpc.main[0] .id
15+ # count = var.create_vpc == "true" ? length(var.private_subnets) : 0
1716# cidr_block = element(var.private_subnets, count.index)
1817# availability_zone = element(var.availability_zones, count.index)
1918
@@ -23,12 +22,11 @@ resource "aws_internet_gateway" "gw" {
2322# }
2423# }
2524
26- # TODO: optional based on flag: create_vpc
2725resource "aws_subnet" "public" {
28- vpc_id = aws_vpc. main . id
26+ count = var. create_vpc == " true" ? length (var. public_subnets ) : 0
27+ vpc_id = aws_vpc. main [0 ]. id
2928 cidr_block = element (var. public_subnets , count. index )
3029 availability_zone = element (var. availability_zones , count. index )
31- count = length (var. public_subnets )
3230 map_public_ip_on_launch = true
3331
3432 tags = {
@@ -37,26 +35,27 @@ resource "aws_subnet" "public" {
3735 }
3836}
3937
40- # TODO: optional based on flag: create_vpc
4138resource "aws_route_table" "public" {
42- vpc_id = aws_vpc. main . id
39+ count = var. create_vpc == " true" ? 1 : 0
40+ vpc_id = aws_vpc. main [0 ]. id
4341
4442 tags = {
4543 Name = " ${ var . aws_resource_identifier } "
4644 }
4745
4846}
49- # TODO: optional based on flag: create_vpc
47+
5048resource "aws_route" "public" {
51- route_table_id = aws_route_table. public . id
49+ count = var. create_vpc == " true" ? 1 : 0
50+ route_table_id = aws_route_table. public [0 ]. id
5251 destination_cidr_block = " 0.0.0.0/0"
53- gateway_id = aws_internet_gateway. gw . id
52+ gateway_id = aws_internet_gateway. gw [ 0 ] . id
5453}
55- # TODO: optional based on flag: create_vpc
54+
5655resource "aws_route_table_association" "public" {
57- count = length (var. public_subnets )
56+ count = var . create_vpc == " true " ? length (var. public_subnets ) : 0
5857 subnet_id = element (aws_subnet. public . * . id , count. index )
59- route_table_id = aws_route_table. public . id
58+ route_table_id = aws_route_table. public [ 0 ] . id
6059}
6160
6261
@@ -66,7 +65,7 @@ resource "aws_route_table_association" "public" {
6665resource "aws_security_group" "allow_http" {
6766 name = " allow_http"
6867 description = " Allow HTTP traffic"
69- vpc_id = aws_vpc. main . id
68+ vpc_id = var . create_vpc == " true " ? aws_vpc. main [ 0 ] . id : null
7069 ingress {
7170 description = " HTTP"
7271 from_port = 80
@@ -85,7 +84,7 @@ resource "aws_security_group" "allow_http" {
8584resource "aws_security_group" "allow_https" {
8685 name = " allow_https"
8786 description = " Allow HTTPS traffic"
88- vpc_id = aws_vpc. main . id
87+ vpc_id = var . create_vpc == " true " ? aws_vpc. main [ 0 ] . id : null
8988 ingress {
9089 description = " HTTPS"
9190 from_port = 443
@@ -104,7 +103,7 @@ resource "aws_security_group" "allow_https" {
104103resource "aws_security_group" "allow_ssh" {
105104 name = " allow_ssh"
106105 description = " Allow SSH traffic"
107- vpc_id = aws_vpc. main . id
106+ vpc_id = var . create_vpc == " true " ? aws_vpc. main [ 0 ] . id : null
108107 ingress {
109108 description = " SSH"
110109 from_port = 22
0 commit comments