How to do Cloud Automation using Terraform?

How to do Cloud Automation using Terraform?  zekeLabs
Posted on July 31, 2020, 5:25 p.m.

Cloud Automation using Terraform - Banner Image

 

What is Cloud Automation?

Cloud Automation is coming together of Cloud Computing with Infrastructure Automation. As cloud adoption is accelerated in the industry, the industry needs to automate the management of cloud infrastructure and cloud services. All of the public cloud service providers expose APIs to manage the cloud. Cloud automation tools leverage this feature to provision the cloud infrastructure by using the principle of “Infrastructure as code” (IaC). Cloud automation tools use some or other Declarative State Languages (DSL) to declare the desired state of the cloud infrastructure. The tools read the script written in DSL and convert it to API request. This API request is sent with proper authentication tokens to the cloud. Use of Cloud automation tools minimize the human effort, minimize the time required to bring up and tear down the cloud infrastructure, minimize the scope of the error, minimizes the cloud cost, and make the whole process repeatable and reusable. 

 

What is Terraform?

Terraform is an open-source infrastructure provisioning tool that can be used with any infrastructure provider that exposes an API. The infrastructure provider could be a public cloud, private cloud, CI/CD tools, or platforms such as Docker, Kubernetes, Github, and many more.  Terraform is built by Hashicorp, which also offers a commercial version aka Terraform Cloud. Terraform scripts are written in Hashicorp Configuration Language (HCL) which is a DSL also used with other Hashicorp tools such as Consul, Nomad, Vault as well.  Terraform can work with the various public cloud as well as on-premise data-centers. That makes Terraform a good tool to create a multi-cloud and hybrid-cloud environment. 

 

 

Steps for Cloud Automation using Terraform

Prerequisites:

  1. Terraform works on Windows as well as Linux-based systems. The System requirements for Terraform is not much, so a normal laptop with internet connection shall be fine.
  2. The user needs to have a cloud account and credentials with programmatic access.
  3. Sufficient privileges to create cloud services and infrastructure shall be granted to the user that she wants to manage. 
  4. The User should install the respective CLI if required. For example, the user needs AWS CLI configured with proper cloud credentials.
  5. To version-control the scripts, some source code management tools, such as Git is required.

 

Terraform installation

Assuming that you are working on a Linux-based system, run the following command - 

sudo curl -O https://releases.hashicorp.com/terraform/0.12.28/terraform_0.12.28_linux_amd64.zip

sudo unzip terraform_0.12.28_linux_amd64.zip

chmod +x  terraform 

mv  terraform /usr/bin/

Check whether terraform is working fine by running below command 

terraform --version 

terraform -help 

To enable auto-completion of terraform command run 

terraform -install-autocomplete

 

Congratulations !!! 

You are ready to initialize and use Terraform. 

 

Terraform Initialization 

Make and move to a directory for the workspace to keep all the scripts.

Provide AWS Cloud Credentials through the environment variables.

$ export AWS_ACCESS_KEY_ID="anaccesskey"

$ export AWS_SECRET_ACCESS_KEY="asecretkey"

$ export AWS_DEFAULT_REGION="ap-south-1"

Create a main.tf file. 

provider "aws" {

  region = ap-south-1

}

To initialize terraform execute - 

$terraform init 

It creates .terraform directory. Terraform keeps a lot of metadata in this directory. 

 

Terraform Configuration

To begin with, create a main.tf script. The main.tf script is written in HCL or JSON syntax. It contains the configuration of all the services and infrastructure that the user wants to create and manage through terraform. 

A sample main.tf file to create AWS ec2 instance is given below.  I assume that you have created a key-pair (ec2-user-key) in your AWS account 

provider "aws" {

}

resource "aws_key_pair" "ec2-user-key" {

key_name = "ec2-user-key"

public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAhoATn2P7U/dwctC803YV6bOweUGoOW4DECB2RupJC8po7H7cu7/RkHv0sG1Adct1bdQ9hOfAVjBjrePoEgorem8gR0vR7XD7WyGqfvsAvX5Cw0kAZgW4KU3HMD66EHzS0x9bhpJpwgFZPzLnBIqobBtEgeooZAtd0MEebEFP2uyLgEoEf3INYosEedJbbbXASOJOSLxici2WcyP/LItO4idprtryD04BIP9lH5Z8Ab0l0ANCLjk9u8PkoEt7HEO4fSWPFVoPE9ySRbPDvxhtOFggeNriHKYAVSQNt+7sXcdHadMB5TGl/xAAcEPcafOQjUgmxrJ6JuBWukhuuEJ77 [email protected]"

}

resource "aws_instance" "web"{

ami = "ami-009d6802948d06e52"

instance_type = "t2.micro"

key_name = "${aws_key_pair.ec2-user-key.key_name}"

   provisioner "local-exec" {

     command = "echo ${self.private_ip} > file.txt"

}

}

 

Executing Terraform

First, validate what all resources will be created using the script.  Use the terraform plan command to do the same - 

$Terraform plan 

The above command tells how many resources would be created. If it looks fine execute the apply command to create the resources on the cloud. 

$terraform apply 

It shall ask for confirmation, hit “y” to confirm the creation of infrastructure resources. 

Check your AWS account the ec2-instance must be created.

 

Congratulations again!!!  

You just launched a server on AWS Cloud using terraform.

 

Now that you have an automated cloud, you can check out our training in Terraform on our website.


zekeLabs - an L&D Guardian and a pioneer of the Proactive Revolution, is the most zealous organization with in-house subject matter experts (SMEs) & technology evangelists in India and Southeast Asia.




Keywords : cloud terraform Technology


Recommended Reading


Using Terraform with AWS

Terraform is an open-source software made by Hashicorp Inc and the open-source community. It is an Infrastructure-provisioning tool that uses a high-level language. The language which it uses is known as Hashicorp Configuration Language (HCL). Terraform ca...


Using Terraform with Azure

Terraform is open-source software built by Hashicorp along with the community. The objective is to provide automation for any API-based tools. Since all the cloud service providers expose an API, So Terraform can be used to automate and manage hybrid-cloud ...


How to build a Multi-Cloud and Hybrid-Cloud Infrastructure using Terraform?

In the era of cloud-wars, the CIOs often have a hard time adopting a single cloud. Putting all their infrastructure into one cloud is a risky proposition. So the best approach is to use the multi-cloud or hybrid cloud strategy. Having multiple cloud provide...


Know more about Terraform

Terraform is a tool made by Hashicorp. It is also used as a tool for cloud-automation. It is an open-source software to implement “Infrastructure as Code (IaC)”. The language used to write the terraform script is known as Hashicorp Configuration Language (H...


How to install Kubernetes Clusters Using Terraform?

Kubernetes is a container orchestration platform that can be used to deploy and manage a containerized applications. Generally, Microservices-based applications are first converted into Docker (or other container runtimes) images and then these microservice...