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.
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"
}
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"
}
}
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"]
}
}
terraform init
terraform plan
terraform apply
To clean up:
terraform destroy
This simple lab showed me how Terraform uses providers and data sources to dynamically manage AWS infrastructure. A great foundation!