Solution Review: Create a VPC with a CIDR Range
In this lesson, we'll discuss the solution to the last lesson's project.
We'll cover the following
Problem statement#
Write a Terraform project that creates a VPC with a CIDR range set from a variable in a region set by another variable. Then apply the project and set the CIDR variable using a file and the region using an environment variable.
Running the project#
You can use following commands to run the project:
📝Note: Clicking the RUN button will run the
terraform init
command.
Solution#
Let’s look into the implementation of this project.
/
- main.tf
To configure the region, the AWS infrastructure is created in which you set the region on the provider. We are taking the region from a variable "region"
and passing that to the provider block to configure the region. The "aws_vpc"
resource is used to create a VPC and the cidr_block
property is used to configure the CIDR block for the VPC. To allow the CIDR block to be configured by a variable, we have declared a variable "cidr"
and set the value of this variable to the "cidr_block"
property on the "aws_vpc"
resource.