Creating an custom module for ec2 instance with a amazon linux 2 ami ID

Eric Wainwright
3 min readJun 18, 2022
Terraform and EC2

A module is a container for multiple resources that are used together. Every Terraform configuration has at least one module, known as its root module, which consists of the resources defined in the . tf files in the main working directory.

Is this article I will be creating a module to start an ec2 instance. The very first thing we will need to do is clone a repo assigned by my coaches at Level Up In Tech AWS DevOps 24 week boot camp. After cloning we will need to make changes to the ec2.tf file located in the ROOT/ PARENT directory called, terraformec2 in order to create the Module.

What is needed to complete the objectives of this article:

First, I will need to fork and then clone the repo above. Below we have our ec2.tf file. This file is going to be broken down in order to create the ec2 instance module.

Providers section from the ec2.tf file in the root directory cut and pasted into providers.tf

Above we have cut the providers from the ec2.tf file in the root directory and have pasted it into the a directory called; my-ec2 which is a child of the root directory. Next, we cut and past the resources section from ec2.tf and place it in the main.tf file in the child directory as seen below. Now the ec2.tf file should be emptied.

This resource was cut and pasted from the original ec2.tf file in the root directory. The EC2 resource here is given an ami-id, instance type and server name. Also, be sure to have the ami in the same AZ as the one your working in or it will not operate.

The ec2.tf file in the Parent Module terraformec2 is now empty. I will be turning the ec2.tf file into a module below.

module “my-ec2” created above

I will now run a the following terraform commands to sure up the configuration.

  • Terraform init.
  • Terraform fmt
  • Terraform Validate.
  • Terraform Plan.
  • Terraform apply.

After a success apply, the next step is to open the AWS Console navigate to EC2 and view instances. I should see a server called, “Edubs-Server”.

As shown above the server was started successfully.

Next, I am running a terraform destroy to clean up the IaC. Below, I will now add my variables; which will create a linkage to my root module. Creating communication between the Parent and Child Modules.

Be sure the ami is from the same AZ your currently working from.

We will now continue to define our variables, next is the main.tf file for the instance.

child variable for ec2 instance

Now the I am adding the .gitignore file to make sure that no unnecessary files are sent upstream when pushing to github. This is huge because any file size 100MB or larger will not push to github.

.gitignore file above

It is now time to commit changes and push my code.

code successfully pushed to github and gist have been created as well
Code pushed to github and you can see the Directory folder my-ec2

This was very interesting terraform is a great tool and this project gave a base to work from now on when setting up a module(s).

--

--

Eric Wainwright

DevOps engineer in search of Cloud Engineer role. Looking for Cloud Operations or Cloud Infrastructure opportunities.