Terraform: Creating A Custom Module For EC2

Troy Ingram
6 min readJun 14, 2021

Use Case

A simple Terraform file has been made by team member in your organization to launch an EC2 instance. Your company would like to use this template as the standard for launching EC2 instances, however it does not currently follow Terraform best practices. You have been directed to modify the file so that it is a module, allowing easy re-use by team members within the company.

Steps

  1. Fork and clone the original repo locally with the ec2.tf template as a starting point to create the EC2 : https://github.com/LevelUpInTech/terraformec2.git
  2. Create a custom module for EC2 out of the resource block that can be used as repeatable infrastructure.
  3. Push the newly created module and the EC2.tf file our repo.
  4. To view my full code please visit my GitHub.

Modules

As your infrastructure grows, it can get increasingly complex and difficult to manage in a single file. Understanding and navigating the file can become difficult due to the size and number of resources. Sharing parts of the configuration with team members is error prone and hard to maintain for the same reason. Modules can be used to organize, encapsulate, and re-use your configuration. Modules can also provide consistency and…

--

--

Troy Ingram

Cloud DevOps Engineer | 5x AWS Certified | Terraform Associate | Python | Linux | DevOps