Learn Terraform: Your First Steps in Infrastructure as Code

In this Terraform series article, let us understand Terraform - Infrastructure as Code, why we use this tool, and how to use it.

Before I begin, let us understand infrastructure.

  • In IT organizations, different environments such as DEV, QA, UAT, and PROD are typically used to host applications both internally and externally.

  • Infrastructure comprises a combination of servers, hardware, storage, networks, policies, and people, all of which need to be provisioned.

  • Whether it is existing or new infrastructure, we should be able to modify or create it as needed, either on the cloud or in our own data centers.

Now let's understand the problem statement if we use traditional methods / manual processes, or the Imperative approach for provisioning and modifying the infrastructure.

  • Provisioning infra will be slower

  • Difficult to scale

  • Error prone

  • Chances of over provisioning

To overcome these problems, we must automate the process of provisioning and managing infrastructure across different environments, ensuring they are in a desired and consistent state.

DevOps recommends using a methodology called Infrastructure as Code (IaC).

IAC - Infrastructure as Code

Infrastructure as Code is automation process, where you give your desired configuration of the infrastructure through a code and tool take care of achieving it.

We should only tell our desired configuration, not how to achieve it.

IAC helps to make sure

  • Configuration is version able

  • Configuration is repeatable

  • Configuration is testable

What is Terraform?

Terraform is an infrastructure as code tool and it helps us to automate the infrastructure process. By using this tool, we can manage the infrastructure On-Premises or Cloud resources.

Other similar tools are: Cloud Formation, Azure Resource Manager, Pulumi

Why Terraform?

  • It is an open source

  • Cloud Agnostic, meaning we can use this tool on any providers whether it is Cloud or On-Premises.

  • It works on Declarative approach, where we give our desired state through configuration file and tool take care of how to achieve.

  • Before applying the changes, we can see execution plan

  • Parallelism in provisioning - which means we create the resources in parallel.

  • Reusable & Scalable at any given point of time.

Terraform Architecture?

Terraform Architecture Introduction - Structure and Workflow

  • Install Terraform on your machine, which we call Terraform core.

  • Develop the configuration file using HCL (HashiCorp Configuration Language) is a domain-specific language used with Terraform for defining infrastructure as code.

  • Terraform reads the desired state and performs the following steps:

    • PLAN => It compares the desired state with the actual state and generates a plan or graph.

    • APPLY => It connects to the cloud, creates the desired state using providers, and generates a state file.

    • Different providers (plugins) enable Terraform to connect to the cloud.

Here is a simple example of a Terraform configuration file to create an AWS EC2 instance:

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "example-instance"
  }
}

How to install and setup Terraform?

Terraform can be installed on Linux, Windows, or any other machine.

You can install Terraform through a package repository or by downloading a specific version.

How terraform connects to AWS cloud?

  • When you run terraform, it will look for the environment variable ( AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)

      $export AWS_ACCESS_KEY_ID = [Your Access Key Id]
    
      $export AWS_SECRET_ACCESS_KEY=[Your Secret Key]
    
  • Second way, passing the access/secret key in the ./bashrc [login script] of user.

  • Other way, passing access/secret key through a credentials file:

    • Install AWS CLI and configure using below command.

        $ aws configure
      
    • Once you configured hidden folder will be created in user home directory [~/.aws/credentials]

In conclusion, in the next article, we will explore more topics about Terraform.

0
Subscribe to my newsletter

Read articles from Omprakash Kalagoni directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Omprakash Kalagoni
Omprakash Kalagoni

Greetings! ๐Ÿ‘‹ Hi, Iโ€™m Omprakash Kalagoni, a dedicated DevOps Engineer. Join me as I delve into the dynamic realms of cloud computing and DevOps through insightful blogs and updates. Passionate about automating workflows, optimizing deployments, and ensuring seamless integration across environments. Let's connect and explore the evolving landscape of technology together!