Solution Review: Find the CIDR Range

In this lesson, we'll discuss the solution of the Terraform project you made in the last lesson.

Problem statement#

Make a Terraform project that creates a VPC and sets the tag Name to your name. Create another Terraform project that looks up that VPC using a data block filtering on the Nametag and outputs the CIDR range of the VPC to the terminal.

Running the project#

You can use the following commands to run the project.

📝Note: Please use the cd command to move to the specific folder and run the commands.

part1#

Inside part1 folder run commands:

terraform init
terraform apply

part2#

Inside part2 folder run commands:

terraform init
terraform apply

📝Note: Clicking the RUN button will move you to the part1 folder and run terraform init commands.

Solution#

Let’s look into the implementation of this project.

📝Note: Please do change the Name in the tags block with your name, before running the project.

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

Subfolder part1 creates the VPC with the Name tag set to Kevin. Subfolder part2 shows how to look this up by data block and output it.

In the folder part1, we set up an AWS provider pointing to the region "eu-west-2". We then declare a "aws_vpc" resource and set the cidr_block. We also set the Name tag to our name, which is "Kevin" in this case. We then run terraform init to initialise Terraform and terraform apply to create the infrastructure.

Then, for part2, we configure the provider and use the same region as part1 (eu-west-2). We then configure the aws_vpc using a "data" block to look up the existing VPC that we created in part1. We set the Name tag to "Kevin" so that the data resource finds the VPC that we created in part1. To complete the exercise, we output the CIDR block of the VPC using an output.

Project: Find the CIDR Range
Project: Create a VPC and Two Subnets
Mark as Completed
Report an Issue