Skip to main content

Command Palette

Search for a command to run...

Basics of IaC with Terraform

hands on terraform

Updated
3 min read
Basics of IaC with Terraform

Introduction

In this series, we are going to focus our attention on Terraform. This series is different in the sense that I will be learning along as I write about it.

Terraform is an open-source Infrastructure as Code (IaC) tool designed by HashiCorp. It enables users to define and provision infrastructure and resources in a declarative manner using a high-level configuration language.

One key thing about Terraform that makes it so powerful is that it is a cloud-agnostic tool, allowing provisioning of infrastructure across 100+ providers including AWS, Azure, and GCP.


Basics First

Before diving into the code, let’s walk through some basics first. Every Terraform project follows a standard lifecycle:

Init → Write → Plan → Apply

This lifecycle is consistent across all Terraform projects and defines how infrastructure is initialized, configured, reviewed, and deployed.


First Code Snippets

Any Terraform project starts by defining the provider. Here’s a simple example:

// This defines and sets the provider for the Terraform project.
// Terraform will use this to download the required provider code
// and interact with the provider APIs.

provider "google" {
  credentials = file("creds.json")
  project     = "Hello-World"
  region      = "us-east-1"
}

Once the provider is set, you can start creating resources:

resource "aws_instance" "hello-instance" {
  ami           = "ami-id-here"
  instance_type = "t2-micro"
}

In the above snippet:

  • aws_instance defines the type of resource.

  • "hello-instance" is the user-defined name.

  • You can later refer to it as aws_instance.hello-instance.

There’s also a data block, which is useful for referencing shared configuration such as AMI IDs or instance types without repeating values across multiple resources. We’ll use it later when the need arises.


Example 1 — Create an EC2 Instance

In this example, we’ll create an EC2 instance using Terraform. To set up Terraform and AWS CLI, follow this video tutorial. I personally prefer using WSL on Windows for development.


Solution Code

If required, you may need to read up about subnets and networking first.

// Setup the provider and mention the profile Terraform should use for login
provider "aws" {
  region  = "us-east-1"
  profile = "terraform"
}

// Step 1: Create a VPC for development
resource "aws_vpc" "vpcdev" {
  cidr_block = "10.0.0.0/16"

  tags = {
    environment = "development"
  }
}

// Step 2: Create a subnet in the VPC created above
resource "aws_subnet" "devsubnet1" {
  vpc_id            = aws_vpc.vpcdev.id
  cidr_block        = "10.0.1.0/24"
  availability_zone = "us-east-1a"

  depends_on = [aws_vpc.vpcdev]
}

// Step 3: Create an EC2 instance in the subnet
resource "aws_instance" "devvm1" {
  ami           = "ami-0be2609ba883822ec"
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.devsubnet1.id

  depends_on = [aws_subnet.devsubnet1]
}

References


Tags: aws, basics, getstarted, iac, terraformCategory: Infra as Code

Hey Terraform

Part 3 of 3

In this short series, we will include some articles to go through how to write your own terraform code.

Start from the beginning

Terraform Modules

divide and rule

More from this blog

B

Beyond Backfills

12 posts

Beyond Backfills is a space to explore the art and craft of data engineering. The goal isn’t just moving bytes—it’s understanding the systems that shape them, and the human curiosity that follows.