Skip links

Automated Infrastructure Provisioning On AWS With Terraform

“Terraform” provides a common configuration of set of files to launch infrastructure either it is On-Premises or Cloud Providers. After launching infrastructure, terraform safely and quickly changes Infrastructure. Terraform acts just like an “Infrastructure as Code.” Infrastructure is built as code of `.tf` extension file, and terraform generates whole stack after applying “terraform apply” command, and destroys the whole stack by applying “terraform destroy”.

How Terraform works

  • One just need to create a common configuration file/s; after that apply ‘terraform apply’ command.
  • Terraform detects all terraform configuration files from the working directory, and starts building infrastructure. It may take some time to build the whole infrastructure.
  • When one wants to destroy the infrastructure, which is commonly known as ‘terraform destroy’, terraform synchronously destroys all terraform extension’s files from the working

Features of Terraform

  • Terraform works with most of cloud providers such as AWS, Google Cloud, Microsoft Azure, OpenStack, IBM Bluemix etc.
  • Exist for any environment.For ex. Windows, RHEL, Centos, Ubuntu, Mac etc.
  • Allows to create Terraform configuration files in multiple choices, For ex. Json or .tf (terraform) file can be used to configure Infrastructure.
  • Automation change which allows change in the infrastructure once it’s build. There are no complexity involved while changing the Infra management.
  • Terraform provides Execution Plan of the whole infrastructure prior to infrastructure building by using ‘terraform plan’ command.

Getting Started with Terraform

  • First download and extract package of Terraform from https://terraform.io/downloads.htmland then set the environment variable of terraform directory wherever it is stored. One can see the multiple ‘bat’ or ‘sh’ files for different cloud providers such as “terraform-provider-aws.bat”, ”terraform-provider-azure.bat” etc.
  • After that verify the installation by executing ‘terraform’ command. One can get the available commands and help from command prompt.

Building Simple Infrastructure

Creating an Example1.tf file in any working directory, makes sure no other .tf file is available on working directory because when infrastructure is build, terraform loads all the .tf files.

Example 1: .tf look like this:

Code Snippet:

Note

  • provider variable describes the cloud provider either it’s openstack, aws, azure etc.
  • aws_instance describes the EC2 instance of AWS having variable name example.
  • instance_type has “t2.micro” ami id is “ami-0d729a60” which is Amazon Linux AMI

Terraform Plan

Now go to terminal and go to .tf file working directory path, and apply command ‘terraform plan’ which describes whole infrastructure as planned, also allows to get the output. It will also help to know that how the infrastructure will be built as shown in the image below:

In the mentioned Screenshot, one can see the infrastructure, having single t2.micro instance, ami-7172b611 ami_id, and see the tags Name having Terraform-Server.

Terraform apply

Once the plan is ready, apply ‘terraform apply’ to launch infrastructure as mentioned in the screenshot below:

As shown in the screenshot, one can get status of every 10 seconds of building the infrastructure of AWS. Single service has been described in the screenshot. But one can set multiple instances and multiple aws services in a single configuration file.

Terraform show

After building whole infrastructure, if one wants to see the Infrastructure brief details, For ex. IP Address, Volume, DNS etc., without any provider’s login, one just have to fire ‘terraform show’ command, which enables viewing the entire infrastructure details as mentioned below:

Modify Existing Infrastructure

One of the most powerful feature of terraform is to modify the existing infrastructure, just for example, if one want to add 7 GB more EBS, just add EBS API’s to configuration file and re-apply the infrastructure, Terraform will add the EBS volume size without any downtime.

Ex. Mentioned below is the configuration file, and add some EBS api’s code to add 7 GB Volume.

Code Snippet:

After Modifying Infrastructure

To detect the changes, just re-enter the ‘terraform plan’ command through which one can get the following results as mentioned below:

The image shows that terraform detects the changes of Infrastructure to add 7 GB of EBS volumes in existing infrastructure, so terraform returns the above result. After that just apply the ‘terraform apply’ command to change the infrastructure. That’s why terraform is powerful.

Terraform Destroy

In order to destroy the entire infrastructure one has built, one need to just enter ‘terraform destroy’ command to destroy whole infrastructure, all the resources like EC2, ELB, RDS will be destroyed or terminated by applying following command.

On applying the following command, terraform asks for authentication to destroy the entire infrastructure. If one press ‘yes’, then terraform destroys one by one resources that have been built and the infrastructure is destroyed within few minutes. So that’s why terraform is so powerful and has plenty of features. One can build, manipulate, and destroys the whole infrastructure by code, and also terraform is a powerful orchestration tool.

This website uses cookies to improve your web experience.