Terraform: Creating Reusable Modules-Part 1

Steven Tisdol
4 min readJun 20, 2022

--

Courtesy of parkmycloud.com

Welcome back to another Terraform module. Today we are going to use best practices to create our Terraform Infrastructure while also creating reusable modules that can be used for most use cases. Modules are the Terraform “best practice” way to build your infrastructure on any cloud platform. To give you a better look, I’ve stolen an image from my teammate Duran. Click on his name to check out his projects.

Courtesy of jlouden.com

We are going to create an EC2 module for our “Dev” root folder. You can find the code on my GitHub.

Remember to use your AWS account with admin privileges (also not a best practice). Also, feel free to use the IDE of your choice. But my favorite is VS CODE. If you’re using the same IDE, it’ll be easier to follow along.

So let’s get started!

Step One: Creating Root and Modules

Change your directory to a folder you will remember. I’m in my project folder so I’ll start there. First, we need to create a folder to hold our project.

mkdir terraform-reusablecd terraform-reusable

Once inside our project folder, we will create a directory for our “dev” root folder and then change directory into it.

mkdir dev
cd dev

Here let’s create our ec2 directory.

mkdir ec2

Now that we have our Dev root folder as well as our EC2 module set up, we are ready to begin coding our Terraform files in each folder.

Step Two: Creating EC2 Module .tf Files

Let’s change over into our “ec2” directory (or rather module) and create the files we need in this folder.

code compute.tf variabled.tf

Remember, I’m using VS Code, so the “code” command may not work for you.

The files you’ll need will pop up at the top of your window. Let’s start with the “compute.tf” file and enter the code below:

Now we will navigate to our “variables.tf” file. We are going enter the variables below into the file:

We are done with this folder so let’s move on to step three.

Step Three: Creating Dev Root Module .tf Files

Let’s head over to the files in our “Dev” folder. Open the “main.tf” and the “variables.tf” files to see them both side by side. Let’s start with the “main.tf” file. Enter the code provided below:

Now we just need to enter the values in our “varibales.tf” file. Use the code below:

Now that we have all the information we need, it’s time to build our infrastructure.

Step Four: Provisioning Our Infrastructure

First make sure you change your directory to the “Dev” folder if you haven’t done so already.

cd ../

Now like first initialize our infrastructure.

terraform init

Hopefully, you’ve received the “Successfully Initialized!” message.

There isn’t much code to format but you can still run a “terraform fmt” to do so if you would like. Now let’s validate our syntax.

terraform validate

Once your receive the “valid” message, it’s time to build.

terraform apply --auto-approve

We can see our plan building.

Great! I’m hoping you received the same “successful” message. Head over to your EC2 console to verify your instance was created.

There you go! We have successfully created a reusable EC2 module. There are plenty of other services we can add to our module to build a bigger infrastructure, but I’m going to save that for our next project where we take a deeper dive into Terraform modules and how to make them reusable.

Stay tuned for more Terraform projects on our DevOps journey.

--

--