Building And Testing a Basic Terraform Module

1 hour
  • 4 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

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.
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.
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.
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.

Additional Resources

In the files you create in the modules/vpc directory, you will need to insert the following provided code.

In the main.tf file:

provider "aws" {
  region = var.region
}

resource "aws_vpc" "this" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "this" {
  vpc_id     = aws_vpc.this.id
  cidr_block = "10.0.1.0/24"
}

data "aws_ssm_parameter" "this" {
  name = "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
}

In the variables.tf file:

variable "region" {
  type    = string
  default = "us-east-1"
}

In the outputs.tf file:

output "subnet_id" {
  value = aws_subnet.this.id
}

output "ami_id" {
  value = data.aws_ssm_parameter.this.value
}

In the files you create in the terraform_project directory, you will need to insert the following provided code.

In the main.tf file:

variable "main_region" {
  type    = string
  default = "us-east-1"
}

provider "aws" {
  region = var.main_region
}

module "vpc" {
  source = "./modules/vpc"
  region = var.main_region
}

resource "aws_instance" "my-instance" {
  ami           = module.vpc.ami_id
  subnet_id     = module.vpc.subnet_id
  instance_type = "t2.micro"
}

In the outputs.tf file:

output "PrivateIP" {
  description = "Private IP of EC2 instance"
  value       = aws_instance.my-instance.private_ip
}

To get started, log in to the lab server using the credentials provided:

ssh cloud_user@<Terraform-Controller>

What are Hands-on Labs

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.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?