Solution Review: Create a VPC with a CIDR Range

In this lesson, we'll discuss the solution to the last lesson's project.

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:

Terraform init and apply commands used 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.

This code requires the following environment variables to execute:
access_key_id
Not Specified...
secret_access_key
Not Specified...
/
terraform.tfvars
variables.tf
main.tf
Terraform solution project implementation

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.

Project: Create a VPC with a CIDR Range
Project: Find the CIDR Range
Mark as Completed
Report an Issue