Terraform: Creating Reusable Infrastructure With Terraform module EC2.

Matthew Mendez
DevOps Engineer Documentation
5 min readJul 12, 2021

1. What are Terraform modules?

2. Prerequisites.

3. Scenario.

4. Let’s get started!

1. What are Terraform modules?

Terraform offers a great way to package and reuse common code in the form of modules. Terraform modules are comparable to functions or methods in scripting or programming languages. They present a standard interface for creating resources by providing inputs and returning outputs. Modules also simplify projects by increasing readability and allowing teams to organize infrastructure in logical blocks. Modules can be easily shared and sourced into any Terraform project. A Terraform module (usually the root module of a configuration) can call other modules to include their resources into the configuration. A module that has been called by another module is often referred to as a child module.

2. Prerequisites.

3. Scenerio.

My Level Up In Tech team has asked me to create a custom module for ec2 instance that you can use as repeatable infrastructure. Once we are finished customizing, we will push our custom module to our github repo.

4. Let’s get started!

We’ll begin by forking and cloning the Level up repo. Head to:

You should now have a terraformec2 directory. Change into the terraformec2 directory.

  • cd terraformec2 (I’ll be referring our terraformec2 directoy as the root module from here)

Let’s get started on setting up our custom module. Within the terraformec2 directory, create a new directory my_ec2. (I chose to name mine matts_ec2)

  • mkdir my_ec2

Let’s make a few files within my_ec2 directory

  • touch main.tf providers.tf outputs.tf variables.tf

The ec2.tf file within our root module has a provider section that we will need to cut and paste out and move it into our provider.tf file that we created in our my_ec2 directory.

Above, The provider section within our ec2.tf file. Cut and paste into our provider.tf file within our my_ec2 directory below.

Lets cut and paste the resource section from the ec2.tf and move it into our main.tf in our my_ec2 directory.

ec2.tf should now be empty.

Let’s add the following to the ec2.tf file in the root directory.

The code below allows our root module to reference our child module (my_ec2 directory).

While in the current terraformec2 directory, run terraform init to initalize a working directory containing terraform configuration files.

Variables.

Now that we have successfully initialized Terraform, we can start creating variables in our variables.tf file under our my_ec2 directory. Variables in terraform are a great way to define centrally controlled reusable values. The information in terraform variables is saved independently from the deployment plans, which makes the values easy to read and edit from a single file.

Add the following code to our variables.tf file.

Note: Your AMI ID might be different depending on the region you specify when you configure your AWS credentials.

Main.tf

Next in our main.tf file, lets add the following code.

As you can see in our main.tf file:

  • We are declaring our EC2 instance with resource “aws_instance”.
  • Declaring our ami id that we declared in our variables.tf file.
  • Declaring our instance type that we declared in our variables.tf file.
  • Declaring how many instances are launched.
  • Naming our EC2 instance.

Outputs.

Lets add some outputs in our my_ec2 directory. Outputs show the information needed after Terraform templates are deployed. They are also used within modules to export information. Add the following code to our outputs.tf file.

Here we want the ip address and the tag returned to us in the cli after running the terraform apply command.

Its now time to run our code!

  • First hit terraform fmt. The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style.
  • Next hit terraform plan. The terraform plan command creates an execution plan.
  • Next hit terraform validate. Terraform Validate runs checks that verify whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state.
  • Next hit terraform apply. The terraform apply command executes the actions proposed in a Terraform plan. Hit yes when prompted.

After successfully creating our resources, we can go to the AWS EC2 console to take a look at our resources that we have created. You also will have noticed our output values printed in the cli.

Go ahead and run a terraform destroy now that we know our code has worked and got our resources up and running.

Github.

Were now tasked to push our code to Github. The primary benefit of GitHub is its version control system, which allows for seamless collaboration without compromising the integrity of the original project.

  • We’ll need to add a .gitignore file in our terraformec2 directory. The purpose of gitignore files is to ensure that certain files not tracked by Git remain untracked. Add the following .gitignore file.
  • Next run git status. The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.
  • Next run git add ec2.tf my_ec2 .gitignore providers.tf variables.tf outputs.tf. This will add our files to the staging area.
  • Next run git commit -m “managing infrastructure as code”
  • Next run git push -u origin main. This will push our code to Github.

To view my Github code for this tuturiol. Click here.

--

--

Matthew Mendez
DevOps Engineer Documentation

Documenting my journey from bartender to a career as a devops engineer