🌐 Getting Started with Terraform: Providers, Resources, and Data Sources

🧠 Introduction

In this blog post, I’ll walk you through Labs 03, 04, and 05 of my Terraform learning journey. These cover providers, resource blocks, and data sources using AWS.

πŸ” Lab 03: Terraform Providers

Providers tell Terraform how to interact with cloud platforms. Here’s how I defined the AWS provider:

terraform {
  required_version = ">= 1.0.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

πŸ’» Lab 04: AWS Resource Blocks

This creates an EC2 instance with a dynamic AMI:

resource "aws_instance" "lab_ec2" {
  ami           = data.aws_ami.amazon_linux.id
  instance_type = "t2.micro"

  tags = {
    Name = "LabEC2Instance"
  }
}

πŸ”— Lab 05: Data Sources

This pulls the latest Amazon Linux 2 AMI:

data "aws_ami" "amazon_linux" {
  most_recent = true
  owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }
}

πŸ§ͺ Running the Code

terraform init
terraform plan
terraform apply

To clean up:

terraform destroy

🎯 Final Thoughts

This simple lab showed me how Terraform uses providers and data sources to dynamically manage AWS infrastructure. A great foundation!