Building reusable Terraform modules
When working in an business environment or with teams, it's important to have a standard and secure way of creating resources. This is where modules come in handy. We can create a module that defines the standard way to create certain resources, and then share that module with other teams or projects. That way, infrastructure built will always meet your required standards. Learn how to create a module for an EC2 instance in this lab.
- 11 guided tasks
- Terminal in the browser

Scenario
This lab is part of the Terraform on AWS course. It is the Building reusable Terraform modules - Part 2 lesson, and it continues the Building reusable Terraform modules - Part 1 lesson, where you start the EC2 module. If you completed Part 1 in your own terminal, open its directory and continue to the next task.
Teams need a standard and secure way to create resources. In this lab, you'll finish a reusable Terraform module that only creates an encrypted t2.micro or t3.micro EC2 instance with a BusinessUnit tag and the latest Ubuntu Server or Amazon Linux AMI. Then you'll use the module to deploy the instance into a custom VPC, check it with the AWS CLI, and destroy it.
What you will learn
- Use the aws_ami data source to find the latest Ubuntu Server or Amazon Linux AMI.
- Let module users select an AMI with a validated variable, locals, and the coalesce function.
- Call a local module from the root configuration and add a custom VPC for the instance.
- Check module inputs with terraform validate, then deploy, verify, and destroy the resources.