Module Concept in Terraform — Launching an ec2 instance

Chamakenjefi
5 min readJan 8, 2023

--

Introduction

Before we dive into the project at hand, let’s get a basic understanding of what modules are when we talk about Terraform and why it is such an important concept to apply when using Terraform.

What is a module: A Terraform module is a set of Terraform configuration files in a single directory. In other words, a module allows you to group resources together and reuse this group later, possibly many times. Modules are organized as root or child modules and we use the root module to reference the child module.

Why are modules important: Modules are important because they;

  • Organize your configuration
  • Encapsulate you configuration
  • Make your configuration reusable
  • Provide consistency and ensure best practices are used
  • Offer self-service as any other person or member of your team can reuse your code without needing your intervention

Please click here to visit the Terraform official documentation for more information on modules.

Prerequisite

  • AWS Account with admin privilege
  • IDE (Will be using Cloud9) with Terraform & AWS CLI installed
  • GitHub Account

Project 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. DO NOT USE THE TERRAFORM REGISTRY. YOU SHOULD BE CREATING YOUR OWN MODULES.
  3. Be sure to create a variables.tf file for your ec2 module.
  4. Push the new created module and the ec2.tf file to your repo!

So leeeettttts Goooooooooo….

Step 1: Fork & Clone repo locally

We will click on the link provided and then hit on “Fork” on the top right side of the page and as highlighted in yellow.

This will take you to your repo, where you have the choice of saving the repo with the same name (terraformec2) or customizing the name as per your project need, which is what I did for this project as per the new repo name below.

Steps 2 & 3: Create a custom module for ec2 out of the resource block

Here below is the code for our modules:

  • First, we can see that we have 2 ec2.tf files but they are both different as the first one is the root module while the second one is the child module. We then call the child module from the root module using the word “module”.
  • Second, we have the variable.tf file in which we declare all our variables so that they are only referenced in the ec2.tf file (child module). The word “var” is used to reference a variable.
  • Third, as a best practice, it is advisable to always comment your files so that you or any of your team members can easily identify which module they are working on. This can become increasingly challenging especially when your codes start increasing or when you have several codes to use.
  • Last but not the least, and as suggested in my last article, always build your code incrementally and test it as you go so as to ensure that every bit is working as you progress. This is very important as it makes troubleshooting easier if you face any issue as you can reduce your error search scope and save time.

We will now run our terraform commands to ensure our codes work properly.

  • terraform init
Terraform has been successfully initialized!
  • terraform validate
  • terraform plan
  • terraform apply

We can verify in our AWS console if the ec2 instance was successfully created and running.

  • terraform destroy

Finally, we can now destroy the resource we just created.

Step 4: Push codes to GitHub repo

Now that we have confirmed that our codes work as our ec2 instance has been successfully created, we can now run the following git commands to add, commit and push the modules (root & child) to my GitHub repo;

We can check our GitHub repo just make sure the codes have been successfully pushed:

root (ec2.tf) & child module (ec2-instance)

Here is also a link to this repo for your reference https://github.com/ChamakeNjefi/terraform-ec2-wk21

Hooray!!!! We have accomplished all the project tasks.

Conclusion

This was quite a pretty sweet, short, and straightforward project to execute which was fun to do. Hope you enjoyed it too and feel free to leave any constructive feedback on how I can improve my code.

--

--