Getting started with Terraform.

Rahul Kumar Sharma
3 min readNov 15, 2021
Source: Google

Terraform is a tool provided by HashiCorp which used to define infrastructure in code format. It supports different providers such as AWS, Azure, Google Cloud Platform and many more. Terraform was coded in the ‘GO’ language. It is used for building, changing, versioning the complete infrastructure.

This tool assists in the creation of a precise execution plan for achieving a given goal. Terraform implements the plan to develop the infrastructure after the blueprint of the approach is successfully prepared. When the configuration changes during the process, Terraform can identify all of them and provide modified execution remedies that may be used.

Features of Terraform

  1. Infrastructure as a Code
  2. Re-usability of Code
  3. Terraform Lint(Linting helps you quickly find any syntax errors and other code validation errors in your Terraform scripts.)
  4. Rendering of State and Plan.
  5. Open Source
  6. Declarative(What end result we want.)

Difference between Ansible and Terraform

Ansible

  1. Ansible follows the procedural style.
  2. It is mainly used for configuring the infrastructure.
  3. We can deploy apps using ansible.
  4. It maintains all components in working condition and repairs the issue instead of replacing the the entire infrastructure.
  5. We need to write each step in order to obtain the end result.

Terraform

  1. Terraform is completely declarative.
  2. It is mainly used to create infrastructure.
  3. We can deploy Load Balancer, VPC, RDS, EC2 using terraform.
  4. Once we give the end instruction, it carries out all the steps and gives us the final result.
  5. Terraform is used to conserve the environment in a steady state.

How Terraform works?

To work with terraform, we need to connect terraform to cloud providers like AWS using the secret key and access key. Once it gets connected we can create infrastructure as defined in the code.

Core Engine of terraform mainly takes config parameter file named as TF-Config which have details what to configure and State file. It makes a plan like what needs to be created/modified or removed. There is two state-defined current state and desired state.

Terraform Components

  1. Terraform Executable
  2. Terraform Files
  3. Terraform Plugins
  4. Terraform State

Terraform Installation in Ubuntu

wget https://releases.hashicorp.com/terraform/1.0.7/terraform_1.0.7_linux_amd64.zip

unzip terraform_1.0.7_linux_amd64.zip

sudo mv terraform /usr/local/bin

sudo apt-get install python3-pip -y

sudo pip3 install awscli

awscli — is used to connect the ec2 with your AWS account

Terraform Lifecycle

Write a terraform file with the extension .tf and save it.

Example: To create EC2 in AWS.

Once the file is written, you can execute the following commands:

Source: Google
  1. terraform init- Initialize Terraform
  2. terraform plan- To dry test Terraform
  3. terraform apply- Apply Terraform
  4. terraform destroy- Destroy the resources created by Terraform

Best Practices for Terraform Implementation

  1. Use single shared state location.
  2. Use Validate and Plan commands frequently.
  3. Separate modules and environment code.
  4. Use Terraform registry for best practice.
  5. Abstract code to reuse.

Terraform Registry — Registry

Terraform — Terraform Signup

GitHub — AWS Terraform

--

--