Creating a reusable (repeatable) Infrastructure with a custom ec2 module

Morgan Clark
5 min readJul 10, 2022

--

Hello everyone, I come to you with another project to add to my ongoing learning and tooling experience. Tooling, in that I am developing and honing new skills towards becoming a more knowledgeable DevOps associate and future engineer.

This project consists of the following…

Task:

Your team needs you to create a custom module for ec2 instance with an 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: GitHub — LevelUpInTech/terraformec2
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!

Tools of the trade:

1. AWS account (personal or business)
2. A repository account to store your completed project, GitHub (https://github.com/), GitLab (https://gitlab.com/help)
3. Select an IDE (Integrated development environment) AWS Cloud9
4. Terraform account (If you’ve never used Terraform cloud, you will need to setup your account in AWS cloud9 with the credentials from Terraform Cloud) For additional info {see https://learn.hashicorp.com/collections/terraform/cloud-get-started}
5. AWS CLI to execute amazon commands

Here we go…
I.
GitHub
a. Create a new repository in GitHub:

b. Fork a copy of the ec2.tf template file contained in a repository that belongs to LevelUpInTech. {A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project.}

Screen shot taken from GitHub Fork option

II. AWS Cloud9
a. mkdir directory name {ec2_multi_use}
b. cd directory {cd ec2_multi_use}

c. Run the below command to clone a copy of the newly forked GitHub repository:
git clone https://github.com/mclark0814/ec2_multi_use.git

e. Create 4x files that are needed in a Terraform “folder”: by running touch file name:

i. touch main.tf outputs.tf variables.tf providers.tf {you can enter multiple file names in a single command:

Screenshot of the environment in AWS Cloud9

ii. main.tf: contains the below info which utilizes variables or calls to variables {var.ami_id}

iii. The providers.tf file contains required providers, provider and Terraform cloud info.

iv. The variables.tf file contains data pertaining to the variables that are called within the code by the prefix: var.”item” {var.ami_id}

v. The outputs.tf file is used to return to the user selected data:

vi. The original ec2.tf file that was “forked” or copied from the LevelUpInTech repository, contained the below info but was revised to allow for modular functioning:

ec2.tf file forked from LUIT repository

Modified version of ec2.tf file

vii. Now we will execute the code and hope that we have fulfilled our task.

1. We will run terraform init (Initialize) {see https://learn.hashicorp.com/tutorials/terraform/init}

You will notice that there are three items initialized: Modules, Terraform Cloud and Provider plugins.

2. We will run terraform plan {see https://learn.hashicorp.com/tutorials/terraform/plan}
The terraform plan command creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure.

Shows units how many will be added

3. Next run terraform apply {Enter — auto-approve to bypass the prompts to approve this command}

4. While the new AWS EC2 instance is being provisioned, we can go over to Terraform by access the above link which was generated during the terraform apply command, on the landing page, we find the same info that is displayed in the AWS Cloud9.

Screenshot taken from the Terraform Cloud workspace
Results of the Terraform apply –auto-approve command

5. Now we upload the new code to GitHub:

I utilized the Source Control option contained with in AWS Cloud9 to push the project folder/repository to GitHub.

6. Now we terminate the previously provisioned AWS EC2 instance

7. Enter Terraform destroy, below you will see that I was prompted to approve the destruction of the resources.

Thank you for your patience as I provided some suggestions on how to utilize an ec2.tf Terraform file (template) to provision an AWS EC2 Instance which can be utilized multiple times.

--

--

Morgan Clark
Morgan Clark

Written by Morgan Clark

Sr. Telecom Engineer, pursuing new skills and career opportunities in DevOps.

No responses yet