Creating a Terraform module to configure AWS ec2 instance and launch docker

What is Terraform?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

Configuration files describe to Terraform the components needed to run a single application or your entire data center. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied.

The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.

What is Terraform Module?

A module is a container for multiple resources that are used together. Modules can be used to create lightweight abstractions, so that you can describe your infrastructure in terms of its architecture, rather than directly in terms of physical objects.

The .tf files in your working directory when you run terraform plan or terraform apply together form the root module. That module may call other modules and connect them together by passing output values from one to input values of another.

To learn how to use modules, see the Modules configuration section. This section is about creating re-usable modules that other configurations can include using module blocks.

Why Terraform is used?

Terraform manages external resources (such as public cloud infrastructure, private cloud infrastructure, network appliances, software as a service, and platform as a service) with “providers”. HashiCorp maintains an extensive list of official providers, and can also integrate with community-developed providers. Users can interact with Terraform providers by declaring resources or by calling data sources.

Terraform supports several cloud infrastructure providers such as Amazon Web Services, Microsoft Azure, IBM Cloud, Google Cloud Platform, DigitalOcean, Oracle Cloud Infrastructure, Yandex. Cloud, VMware vSphere, and OpenStack.

A declarative coding tool, Terraform enables developers to use a high-level configuration language called HCL (HashiCorp Configuration Language) to describe the desired “end-state” cloud or on-premises infrastructure for running an application

Terraform has four major commands:

$ terraform init
$ terraform plan
$ terraform apply
$ terraform destroy

Below are some of the benefits of using Terraform.

  • Does orchestration, not just configuration management
  • Supports multiple providers such as AWS, Azure, GCP, DigitalOcean, and many more
  • Provide immutable infrastructure where configuration changes smoothly
  • Uses easy to understand language, HCL (HashiCorp configuration language)
  • Easily portable to any other provider
  • Supports Client only architecture, so no need for additional configuration management on a server

Where Terraform is used?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. Configuration files describe to Terraform the components needed to run a single application or your entire data center.

Terraform can be used to manage a practical infrastructure for deploying development and production versions of a two-tier application in Google’s Cloud using Cloud SQL, Google Kubernetes Engine (GKE), and Kubernetes. In AWS and any other Cloud Providers.

Terraform Lifecycle

Terraform lifecycle consists of — init, plan, apply, and destroy.

Terraform lifecycle, Terraform module

  • Terraform init initializes the working directory which consists of all the configuration files
  • Terraform plan is used to create an execution plan to reach a desired state of the infrastructure. Changes in the configuration files are done to achieve the desired state.
  • Terraform apply then makes the changes in the infrastructure as defined in the plan, and the infrastructure comes to the desired state.
  • Terraform destroy is used to delete all the old infrastructure resources, which are marked tainted after the application phase.

My Terraform Module

I have created a Terraform Module to create AWS instances and launch docker services on them. For testing, I have launched a container with HTTPD service and launched a demo site on port 8080 over public IP.

There are a total of 5 Terraform files:


Terraform Module Code file is used to launch an ec2 instance on Amazon Linux and launch docker application on it

There are 3 variables and you need to give values to them at the time of executing terraform files. I have set a default value of these variables according to my configuration u can edit them.

provider. tf file is used to configuring the setup region and authentication part. contains all the variables necessary in this module.

Practical Demo

I have finally created a terraform registry by creating a GitHub repository with version as a tag and repository name in format terraform-<provider>-<name>.

Now I created a Terraform registry and provided this repository link and a registry is created.

Now we will run this module.

terraform init

It will download the necessary files for the purpose

The last step is to execute the following command:

terraform apply --auto-approve

The setup is finally complete.

Checking our setup

We can see that an instance has been launched on AWS.

We can check the docker configuration by hitting the public IP with 5000 port to check if our container has been launched.

We can see that our module has successfully done its job.

Here is the link to the GitHub repo:

Link for Terraform Registry:

I hope it helps you. Feel free to ask your doubts.




I automate things 😉

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Threads in Slack, a long design journey

Getting started with WSL2

The world of IoT computing — Cloud vs Edge vs Fog

Weekly Technical Progress Update #6, Sunday July 25th

A .NET Manager’s Perspective on F#

[Volatility] ImportError: No module named Crypto.Hash

How to Write Beautiful Python Code: Configuring Black and Isort in VSCode

How to make an app?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shivansh Srivastava

Shivansh Srivastava

I automate things 😉

More from Medium

How To Create a Self-Signed SSL Certificate for Apache in Ubuntu Virtual Cloud Server

Automating Deploy WordPress with MySQL on Kubernetes cluster on AWS.

Let’s Encrypt Certificates -A Highly recommended solution to Encrypt Traffic in Hybrid

Jewelled Diaper or Snuffbox? AWS image recognition at its quirkiest