Understanding Terraform: A Beginner's Guide to Infrastructure as Code

Understanding Terraform: A Beginner's Guide to Infrastructure as Code

What is Terraform?

Terraform is an open-source infrastructure as a code(IaC) tool. It allows you to define and manage your infrastructure (like servers, databases, and networks) using code, instead of manually setting everything up.

What is IaC?

It enables you to define, provision and manage infrastructure across multiple cloud providers using a declarative configuration language.

It supports multi-cloud environments, allowing consistent and efficient infrastructure management.

This is useful because you don't need to manually go into the cloud console to create or change resources.

For Example:

Imagine you want to create EC2 instance

Instead of going into the cloud console (like AWS, Azure, or Google Cloud) and clicking through to create each of these things, you can write a Terraform script.

Here’s a simplified example of a Terraform script to create an AWS EC2 instance

Terraform Config

  • It uses .tf extension

  • Format is HCL( Hashicorp Config Language)

  • Declarative Language

Terraform Supports JSON format also

How to install Terraform?

Step 1: Download Terraform

  • Go to the official Terraform website.

  • Scroll down to the "Windows" section and download the latest version of Terraform in ZIP format.

Step 2: Extract the ZIP file

  • After downloading the ZIP file, extract it to a folder of your choice (e.g., C:\terraform).

Step 3: Add Terraform to the System PATH

  • To run Terraform from anywhere on your system, you need to add the folder containing terraform.exe to your system's PATH.

    1. Right-click on This PC or Computer and select Properties.

    2. Click Advanced system settings.

    3. Under System Properties, click Environment Variables.

    4. In the System variables section, select Path and click Edit.

    5. In the Edit environment variable window, click New and add the path to the folder where you extracted Terraform (e.g., C:\terraform).

    6. Click OK to save the changes.

Step 4: Verify the Installation

terraform --version

It shows like this:

Connect AWS to VS code:

Install AWS CLI

aws --version

Create Access key and Secret Access key:

It shows like this -

Create config file for EC2 instance -

Also cleanup the resources when work is done,

Benefits of Terraform:

  • Consistency: The same code can be used to create the same infrastructure every time, avoiding manual errors.

  • Automation: You can automatically set up, manage, and change your infrastructure.

  • Version control: Since the infrastructure is defined in code, you can store it in a Git repository, track changes, and collaborate easily.

Why Terraform?

Multiple resources using -

  • count

  • for_each

Terraform Modules

  • Modules are containers for multiple resources that are used together.

  • A module consist of a collection of .tf and/or .tf.json files kept together in a directory.

  • Modules are the main way to package and reuse resource configurations with Terraform.

Providing Features like

  • remote state management

  • version control system(VPC) integration

  • automated runs and

  • secure variable management