Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

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.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 0m
Published
Clock icon Mar 29, 2021

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Create the Directory Structure for the Terraform Project

    1. Check that Terraform is installed and functioning properly using the terraform version command.
    2. Create a new directory to house your Terraform code called terraform_project.
    3. In the main project directory, create a custom directory called modules and a directory inside it called vpc.
  2. Challenge

    Write Your Terraform VPC Module Code

    1. In the vpc directory, create a new file called main.tf and add the provided code.
    2. Create a new file called variables.tf and add the provided code.
    3. Create a new file called outputs.tf and add the provided code.
  3. Challenge

    Write Your Main Terraform Project Code

    1. In the terraform_project directory, create a new file called main.tf and add the provided code, which invokes the VPC module created earlier.
    2. Create a new file called outputs.tf and add the provided code.
  4. Challenge

    Deploy Your Code and Test Out Your Module

    1. Format the code in all of your files using the terraform fmt -recursive command.
    2. Initialize the Terraform configuration to fetch any required providers and get the code being referenced in the module block with the terraform init command.
    3. Validate the code using the terraform validate command.
    4. Review the actions that will be performed when you deploy the code using the terraform plan command.
    5. Deploy the code with the terraform apply --auto-approve command.
    6. View the resources that were created using the terraform state command.
    7. Tear down the infrastructure using the terraform destroy command.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

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.

Start learning by doing today

View Plans