๐Ÿš€ Getting Started with Terraform โ€“ A Beginnerโ€™s Guide to Infrastructure as Code

BinereetDevopsBinereetDevops
4 min read

Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows developers and DevOps engineers to define and provision infrastructure using a simple declarative language.


๐Ÿ“ฆ Understanding Terraform Blocks

Terraform scripts are written in HashiCorp Configuration Language (HCL). The language is structured into three main blocks:

  • Resource โ€“ Used to define infrastructure components like EC2, S3, VPC, etc.

  • Variable โ€“ Used to define dynamic input values to avoid hardcoding.

  • Output โ€“ Used to return values after the infrastructure is created.


๐Ÿ›  Common Terraform Commands

Here are the four fundamental commands to interact with Terraform:

  • terraform init โ€“ Initializes Terraform and downloads required provider plugins (.terraform directory is created).

  • terraform plan โ€“ Shows the execution plan before applying any changes.

  • terraform apply โ€“ Provisions the resources defined in the configuration files.

  • terraform destroy โ€“ Destroys the infrastructure managed by Terraform.


๐Ÿ“ .terraform Directory and Providers

When you run terraform init, Terraform creates a .terraform directory. This contains the required provider plugins.

For example, Terraform has a built-in local provider that allows you to use the local_file resource to create local files.

You can also use external providers like:

  • AWS

  • Azure

  • GCP

These help you create resources like S3 buckets, EC2 instances, etc.


๐Ÿ“ Block Naming: Reference Name vs Actual Resource Name

Each resource block in Terraform has a reference name, which is used internally for referencing within the Terraform script. The actual name of the resource (like an S3 bucket) is defined in the block's arguments.

First you write Block name , Then you write parameters. In Parameters we have two things , one is the resource type and other is reference name. Actual name of the resource is written inside the block and that is called argument

resource "local_file" "my_file" {
  filename = "automate.txt"
  content  = "best content for devs - TWS"
}

Explanation:

  • resource: This keyword defines a resource block in Terraform.

  • "local_file": This is the resource type, which creates a file on the local filesystem.

  • "my_file": This is the resource name, a unique identifier for this resource or we can say reference name

  • filename = "automate.txt": This parameter specifies the name of the file to be created.

  • content = "best content for devs - TWS": This parameter defines the content to be written into the file.

๐Ÿงฐ Setting Up AWS Provider with Terraform

Letโ€™s walk through the steps to create an S3 bucket using Terraform.

Step 1: Install AWS Provider

Create a file named terraform.tf and define the AWS provider block.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }

  required_version = ">= 1.0"
}

Step 2: Install AWS CLI

Install AWS CLI from the official AWS documentation and configure it:

aws configure

This will ask for:

  • Access Key

  • Secret Key

  • Region

  • Output format

๐Ÿ‘‰ Create an IAM user on AWS with the necessary permissions to create S3 buckets and use the access keys here.


Step 3: Configure the AWS Provider

Create a new file provider.tf to configure your AWS credentials.

provider "aws" {
  region     = "us-east-1"
  access_key = "YOUR_ACCESS_KEY"
  secret_key = "YOUR_SECRET_KEY"
}

๐Ÿ” Tip: Itโ€™s best practice to use environment variables or secrets manager instead of hardcoding credentials.


Step 4: Create the S3 Bucket

Create s3.tf with the resource definition:

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

  tags = {
    Name        = "MyBucket"
    Environment = "Dev"
  }
}

โœ… Final Steps

Now you can run the following commands to provision the bucket:

terraform init
terraform plan
terraform apply

To destroy the infrastructure:

terraform destroy

๐Ÿ“Œ Conclusion

Youโ€™ve just taken your first steps into the world of Terraform and Infrastructure as Code! From understanding the basic building blocks to provisioning an S3 bucket on AWS โ€” youโ€™re all set to explore more complex infrastructure setups.

My linked Profile - https://www.linkedin.com/in/binereet-singh-9a7685316/

0
Subscribe to my newsletter

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

Written by

BinereetDevops
BinereetDevops