What is terraform and How it works (Part-1)

Posted by

What is terraform?

What is Terraform?

  • IAAC: Infrastructure as Code
  • Coding for Infra: Terraform is used for coding infrastructure.
  • Written in Go: The programming language used to develop Terraform.
  • Release:
    • Community: Command-line interface (cmd), free
    • Enterprise: Graphical user interface (GUI), paid
    • Cloud: Graphical user interface (GUI), paid
  • From HashiCorp: Terraform is developed by HashiCorp.

What is infrastructure?

In Terraform, “infrastructure” refers to the collection of resources and services that you set up and manage using code. This includes servers, databases, networking components, storage systems, and other resources that make up the IT environment. Essentially, it is everything needed to run applications and services in the cloud or on-premises.

These are providers in terraform – https://registry.terraform.io/browse/providers

In more Details:

What is Terraform?

Terraform is a tool that helps you manage and set up computer infrastructure (like servers, databases, and networking) using code. This approach is known as “Infrastructure as Code” (IAAC).

Why Use Terraform?

  1. Consistency: By using code to set up infrastructure, you ensure that the setup is consistent every time.
  2. Version Control: You can track changes over time, just like with software code.
  3. Automation: It automates the process of setting up and managing infrastructure, saving time and reducing errors.

Key Features

  1. Coding for Infra: Instead of clicking around in a web console to set up resources, you write scripts (infrastructure code) to do it.


This script sets up an Amazon Web Services (AWS) virtual machine (VM) with a specific image and type.

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

2. Written in Go: Terraform itself is a program written in the Go programming language. You don’t need to know Go to use Terraform; you write configurations in HashiCorp Configuration Language (HCL) or JSON.

3. Release Versions:

  • Community Edition: Free version that you run from the command line.
  • Enterprise Edition: Paid version with a graphical user interface and additional features for businesses.
  • Cloud Edition: Paid version hosted by HashiCorp with additional cloud-specific features.

4. From HashiCorp: The company behind Terraform, HashiCorp, is known for developing tools for managing infrastructure and development workflows.

Example Workflow

  1. Write Configuration: You write a configuration file describing the desired state of your infrastructure.
provider "aws" {
  region = "us-west-2"

resource "aws_s3_bucket" "example" {
  bucket = "my-unique-bucket-name"
  acl    = "private"

    This configuration sets up an S3 bucket in AWS.

    1. Initialize: Run terraform init to initialize the project. This command sets up the project directory and downloads necessary plugins.
    2. Plan: Run terraform plan to see what changes Terraform will make to your infrastructure based on your configuration.
    3. Apply: Run terraform apply to create the infrastructure. Terraform will create the S3 bucket as specified in your configuration.

    Simplified Example

    Imagine you need to set up a website server:

    1. Without Terraform:
      • Log in to the cloud provider’s console.
      • Click through several screens to create a server.
      • Set up networking manually.
      • Repeat these steps for every server or environment.
    2. With Terraform:
      • Write a script to define the server and its configuration.
      • Run the script using Terraform.
      • Terraform handles all the steps automatically
    resource "aws_instance" "web_server" {
      ami           = "ami-0abcdef1234567890"
      instance_type = "t2.micro"

    This script will create a web server with the specified settings.

    By using Terraform, you simplify the process of managing infrastructure, making it more reliable and easier to handle, especially in large-scale environments.

    Why Terraform?

    1. Multi-Cloud Support:
      • AWS: Uses CloudFormation (written in YAML).
      • Azure: Uses ARM (Azure Resource Manager, written in YAML).
      • Google Cloud (GC): Uses Deployment Manager (written in YAML).
      Each cloud provider has its own tool and language for managing infrastructure. This can make it difficult to manage infrastructure across multiple clouds.
    2. One Coding Specification and Standard for All:
      • Terraform uses a single language (HCL – HashiCorp Configuration Language) to manage infrastructure across different cloud providers. This makes it easier to maintain and standardize your infrastructure code.
    3. Single Command for Management:
      • With Terraform, you use one set of commands to create, update, and destroy infrastructure. This simplifies the process significantly.


    AWS CloudFormation (YAML)

    To create an S3 bucket in AWS using CloudFormation:

        Type: "AWS::S3::Bucket"
          BucketName: "my-bucket"

    Azure ARM (YAML)

    To create a storage account in Azure using ARM templates:

      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "",
      "resources": [
          "type": "Microsoft.Storage/storageAccounts",
          "apiVersion": "2019-04-01",
          "name": "mystorageaccount",
          "location": "eastus",
          "sku": {
            "name": "Standard_LRS"
          "kind": "StorageV2",
          "properties": {}

    Google Cloud Deployment Manager (YAML)

    To create a storage bucket in Google Cloud using Deployment Manager:

    - name: my-bucket
      type: storage.v1.bucket
        location: US

    Terraform (HCL)

    To create the same resources using Terraform:

    # AWS S3 Bucket
    provider "aws" {
      region = "us-west-2"
    resource "aws_s3_bucket" "my_bucket" {
      bucket = "my-bucket"
      acl    = "private"
    # Azure Storage Account
    provider "azurerm" {
      features {}
    resource "azurerm_storage_account" "example" {
      name                     = "examplestorageacct"
      resource_group_name      = "example-resources"
      location                 = "eastus"
      account_tier             = "Standard"
      account_replication_type = "LRS"
    # Google Cloud Storage Bucket
    provider "google" {
      project = "my-project"
      region  = "us-central1"
    resource "google_storage_bucket" "example" {
      name     = "my-bucket"
      location = "US"


    • Consistency: With Terraform, you write one type of configuration file (HCL) for all your cloud providers.By using code to set up infrastructure, you ensure that the setup is consistent every time.
    • Ease of Use: You use the same set of commands (terraform init, terraform plan, terraform apply) regardless of the cloud provider.
    • Flexibility: Terraform works across multiple cloud providers, making it easier to manage hybrid or multi-cloud environments.
    • Version Control: You can track changes over time, just like with software code.
    • Automation: It automates the process of setting up and managing infrastructure, saving time and reducing errors.

    How terraform works ?

    Below things needed to start with terrafrom

    Step 1: Download terraform executable file

    Step 2: Terraform providers like Azure , Aws, Google cloud etc. platform

    Step 3: Code file needed which written in hashcorp language in .tf file.

    Step 4: Terraform state file generated once code deployed. Terraform keeps track of your infrastructure’s current state using a state file

    Workflow for terraform:

    • The Terraform workflow begins with the user writing configuration files (filename.tf) in HashiCorp Configuration Language (HCL) to define the desired state of the infrastructure.
    • The user then runs terraform init to set up the working directory and download necessary provider plugins.
    • Next, the user runs terraform plan to generate an execution plan, which outlines the changes Terraform will make to achieve the desired state. After reviewing the plan, the user executes terraform apply to apply the changes, creating, updating, or deleting resources as specified.
    • Terraform maintains the current state of the infrastructure in a state file (terraform.tfstate).
    • When the infrastructure is no longer needed, the user can run terraform destroy to remove all managed resources, completing the lifecycle of infrastructure management with Terraform.

    Terraform Workflow in details:

    User Writes Configuration Files:

    • You, the user, write configuration files (filename.tf) using HashiCorp Configuration Language (HCL). These files describe the desired state of your infrastructure.


    provider "aws" {
      region = "us-west-2"
    resource "aws_instance" "example" {
      ami           = "ami-0abcdef1234567890"
      instance_type = "t2.micro"

    This example configuration sets up an AWS EC2 instance.

    2.Initialize (init):

    • Run terraform init to set up your working directory containing Terraform configuration files. This command downloads the necessary provider plugins (e.g., AWS, Azure, Google Cloud).

    3.Plan (plan):

    • Run terraform plan to generate an execution plan. This command shows what changes Terraform will make to reach the desired state defined in your configuration files.


    terraform plan

      This will display a list of actions Terraform will perform, such as creating a new EC2 instance.

      4. Apply (apply):

      • Run terraform apply to apply the changes required to reach the desired state of the configuration. Terraform will create, update, or delete resources as necessary.


      terraform apply

      This will execute the plan and set up the EC2 instance as specified.

      5. Destroy (destroy):

      • Run terraform destroy to remove the infrastructure managed by Terraform. This command will delete all the resources defined in your configuration files.
      terraform destroy

      This will delete the EC2 instance created earlier.

      6. State Management (terraform.tfstate):

      • Terraform keeps track of your infrastructure’s current state using a state file (terraform.tfstate). This file is critical for mapping real-world resources to your configuration, so Terraform knows what exists and what needs to be created, updated, or deleted.

        Components Involved

        • Terraform Executable:
          • The terraform command-line tool that you use to run the above commands.
        • Configuration Files (filename.tf):
          • Files where you define your infrastructure using HCL.
        • Terraform Providers:
          • Plugins that allow Terraform to interact with different cloud providers (e.g., AWS, Azure, Google Cloud).
        • Cloud APIs:
          • Terraform communicates with cloud provider APIs to create, update, or destroy resources.

        Example in Action

        Imagine you want to create an S3 bucket on AWS:

        1. Write Configuration:
        provider "aws" {
          region = "us-west-2"
        resource "aws_s3_bucket" "example" {
          bucket = "my-unique-bucket-name"
          acl    = "private"

        This configuration defines an S3 bucket.

        2. Initialize:

          terraform init

          This command sets up Terraform to work with AWS.


          terraform plan

          This shows what Terraform will do (e.g., create an S3 bucket).


          terraform apply

          This creates the S3 bucket as defined.

          5.Check State:

          • Terraform keeps track of this S3 bucket in the terraform.tfstate file.

          6. Destroy:

            terraform destroy
            1. This command will delete the S3 bucket.


            • Write configuration files: Define your infrastructure in code.
            • Initialize: Set up Terraform and download necessary plugins.
            • Plan: Preview the changes Terraform will make.
            • Apply: Apply the changes to create, update, or destroy resources.
            • State Management: Terraform keeps track of your infrastructure’s state in a state file.
            • Destroy: Remove the infrastructure when it’s no longer needed.

            Some More Examples of Infrastructure Components in Terraform

            Compute Resources: Virtual machines (VMs), containers, and serverless functions.

            • Example:
            resource "aws_instance" "web_server" {
              ami           = "ami-0abcdef1234567890"
              instance_type = "t2.micro"

            Networking Resources: Virtual networks, subnets, load balancers, and DNS settings.

            resource "aws_vpc" "main" {
              cidr_block = ""
            resource "aws_subnet" "subnet_a" {
              vpc_id     = aws_vpc.main.id
              cidr_block = ""

            Storage Resources: Object storage (e.g., S3 buckets), block storage (e.g., EBS volumes), and file storage.

            resource "aws_s3_bucket" "example" {
              bucket = "my-unique-bucket-name"
              acl    = "private"

            This creates an S3 bucket for storing files.

            Database Resources: Managed database services, such as RDS instances or NoSQL databases.

            resource "aws_db_instance" "default" {
              allocated_storage    = 20
              engine               = "mysql"
              instance_class       = "db.t2.micro"
              name                 = "mydb"
              username             = "foo"
              password             = "bar"
              parameter_group_name = "default.mysql5.7"

            This sets up a MySQL database instance on AWS.

            Security Resources: Firewalls, security groups, IAM roles, and policies.

            resource "aws_security_group" "allow_http" {
              vpc_id = aws_vpc.main.id
              ingress {
                from_port   = 80
                to_port     = 80
                protocol    = "tcp"
                cidr_blocks = [""]

            Application Services: Managed services for running applications, such as AWS Elastic Beanstalk, Google App Engine, or Azure App Service.

            resource "aws_elastic_beanstalk_application" "example" {
              name        = "example-app"
              description = "My example Elastic Beanstalk application"

            How Terraform Manages Infrastructure

            1. Configuration Files:
              • You write Terraform configuration files using HCL (HashiCorp Configuration Language) to define the desired state of your infrastructure.
            2. Providers:
              • Terraform uses providers to interact with various cloud services (like AWS, Azure, GCP) and other APIs. Providers are plugins that define the resources and services available.
            3. State Management:
              • Terraform keeps track of the state of your infrastructure using a state file. This file records the current state of your resources, allowing Terraform to know what changes need to be applied to reach the desired state defined in your configuration files.
            4. Execution Plans:
              • When you run terraform plan, Terraform generates an execution plan that shows what changes will be made to your infrastructure. This step helps you verify that the changes are correct before applying them.
            5. Apply Changes:
              • Running terraform apply applies the changes to your infrastructure. Terraform creates, updates, or deletes resources as necessary to match the configuration files.

            Inline Feedbacks
            View all comments
            Would love your thoughts, please comment.x