Creating a custom EC2 module using Terraform

Imaze Enabulele
AWS in Plain English
4 min readOct 22, 2022

--

Scenario

Your team needs you to create a custom module for ec2 instance with a amazon linux 2 ami ID

  1. Fork and clone this 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 you can use as repeatable infrastructure
  3. Push the new created module and the ec2.tf file to your repo!

Definitions

Fork: A fork is a copy of a repository. Forking a repository allows you to experiment freely with changes without affecting the original project.

Terraform module: A module is a container for multiple resources that are used together.

Root Module: The .tf files in your working directory when you run terraform plan or terraform apply together form the root module. That module may call other modules and connect them together by passing output values from one to input values of another. Documentation

Child Module: This is the module that’s called by a root module to have its contents included in the configuration Documentation

Step 1: Fork and clone the repo locally with the ec2.tf template

ec2.tf template

To fork, a repository, navigate to the top right corner of your GitHub page and hit the Fork tab. This will pull the repo to your GitHub account

To clone it locally, click on the Code tab, copy the URL, navigate to your cloud environment and run the command git clone <repo URL>

Step 2: Create a custom module for ec2

We will create a directory called ec2-module to store the modules and change into the directory. This is termed the “child module”. Refer to the definition above

mkdir ec2-modulecd ec2-module

Within this directory, we’ll use the touch command to create the configuration files

main.tf
variables.tf
output.tf
provider.tf

Now let's get the configuration files all set up

The providers.tf file will contain the AWS provider and the region

ec2-module/providers.tf

The main.tf file will contain the ec2 instance resource where the AMI ID and instance type will be specified. The AMI ID can be gotten from the Amazon Linux selection when launching an instance in the AWS console. Best practice demands the arguments(ami, instance_type etc) are not hard coded but their values are referenced from variables created

ec2-module/main.tf

The variables.tf file will contain declared variables with values assigned which are referenced in other configuration files

ec2-module/variables.tf

The last is the Output.tf file which will print to the CLI, the public IP and tag

ec2.module/outputs.tf

Now we’ll go over to the ec2 file we cloned from the repository. This file is in the root (parent) module. We’ll use a module block to call the child module (ec2-module) to include its resources in the configuration. This is simply done by specifying the source of the files

root/ec2.tf

We will also create an outputs.tf file that would be used to call the outputs.tf file in the child module

root/outputs.tf

Now to the good stuff

Initialize the current directory to get the modules and plugins prepared

Let’s format our code (Terraform fmt) to get that great format and style for readability and consistency. Also, we’ll validate the code

Terraform plan to get a full layout of what’s going to be built

And lastly, Terraform apply to fire up the code that’s itching to be put to use

We’ll take a walk over to the AWS Console to confirm our instance was launched.

Don’t forget to destroy your infrastructure before calling it a day to avoid raking up some bills

Thank you for reading!

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Check out Circuit.

--

--