- Lab
- A Cloud Guru
Building and Testing a Basic Terraform Module
Terraform modules are a good way to abstract out repeated chunks of code, making it reusable across other Terraform projects and configurations. In this hands-on lab, we'll be writing a basic Terraform module from scratch and then testing it out.
Path Info
Table of Contents
-
Challenge
Create the Directory Structure for the Terraform Project
- Check that Terraform is installed and functioning properly using the
terraform version
command. - Create a new directory to house your Terraform code called
terraform_project
. - In the main project directory, create a custom directory called
modules
and a directory inside it calledvpc
.
- Check that Terraform is installed and functioning properly using the
-
Challenge
Write Your Terraform VPC Module Code
- In the
vpc
directory, create a new file calledmain.tf
and add the provided code. - Create a new file called
variables.tf
and add the provided code. - Create a new file called
outputs.tf
and add the provided code.
- In the
-
Challenge
Write Your Main Terraform Project Code
- In the
terraform_project
directory, create a new file calledmain.tf
and add the provided code, which invokes the VPC module created earlier. - Create a new file called
outputs.tf
and add the provided code.
- In the
-
Challenge
Deploy Your Code and Test Out Your Module
- Format the code in all of your files using the
terraform fmt -recursive
command. - Initialize the Terraform configuration to fetch any required providers and get the code being referenced in the module block with the
terraform init
command. - Validate the code using the
terraform validate
command. - Review the actions that will be performed when you deploy the code using the
terraform plan
command. - Deploy the code with the
terraform apply --auto-approve
command. - View the resources that were created using the
terraform state
command. - Tear down the infrastructure using the
terraform destroy
command.
- Format the code in all of your files using the
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.