Providers & Resources
When you write a Terraform configuration, you’re essentially telling Terraform who to talk to and what to build. The “who” is the provider (AWS, Azure, GCP, Kubernetes, etc.), and the “what” is the resource (an EC2 instance, an S3 bucket, a VPC). Think of providers as translators that help Terraform speak the language of different platforms, and resources as the actual building blocks of your cloud Lego city.
Providers – The Translators
- Providers are plugins that allow Terraform to interact with external systems.
- Example: AWS provider lets Terraform create EC2, S3, VPC, etc.
- Without providers, Terraform wouldn’t know how to communicate with a cloud platform.
provider "aws" {
region = "us-east-1"
}
Resources – The Building Blocks
- Resources are the actual infrastructure objects you want to create.
- Each resource is defined with a type and a name.
- Example: An EC2 instance resource.
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0" # Value may change
instance_type = "t2.micro"
}
Here:
aws_instance= resource type.web= resource name.- Inside the block = configuration details.
Providers + Resources Together
- Provider defines where to build.
- Resource defines what to build.
- Together, they form the foundation of every Terraform configuration.
Hands‑On Lab / Demo
Lab: Create a Provider + Resource
# Provider Block: tells Terraform which cloud to use
provider "aws" {
region = "us-east-1"
}
# Resource Block: defines the infrastructure to create
resource "aws_instance" "web_server" {
ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI (ID may vary)
instance_type = "t2.micro" # Free-tier eligible instance
tags = {
Name = "MyWebServer"
}
}
- Provider Block
provider "aws"→ Tells Terraform to use AWS as the cloud provider.region = "us-east-1"→ Specifies the AWS region where resources will be created.
- Resource Block
resource "aws_instance" "web_server"→ Creates an EC2 instance namedweb_server.ami→ Defines the Amazon Machine Image (AMI) ID (the OS template).instance_type→ Defines the size of the instance (CPU, memory).tags→ Adds metadata (like naming the server).
Think of this file as the blueprint: it describes what you want, not how to build it.
Terraform Workflow with This Script
- Write: Save the script as
main.tf. - Init: Run
terraform init→ downloads AWS provider plugin. - Apply: Run
terraform apply→ provisions the EC2 instance. - Destroy: Run
terraform destroy→ removes the instance when no longer needed.
Plan: Runterraform plan→ shows what Terraform will create.
Example output:Code
+ aws_instance.web_server will be created
Best Practices
- Use meaningful names: Instead of
example, useweb_serverordb_instance. - Version control: Save
.tffiles in Git for collaboration and rollback. - Run
terraform planbefore apply: Always preview changes. - Keep files modular: Split resources into multiple
.tffiles for clarity. - Use variables: Store values like AMI IDs or instance types in
variables.tf.
Pro Tips & Best Practices
- Always pin provider versions for stability.
- Use meaningful resource names (
web_server,db_instance). - Group related resources logically in
.tffiles. - Keep provider configuration minimal and secure (avoid hardcoding secrets).
Summary & Cheatsheet
- Provider = Translator (connects Terraform to a platform).
- Resource = Building Block (defines infra objects).
- Together: Provider + Resource = Infrastructure definition.
Quick mnemonic: Provider tells where, Resource tells what
The Hackers Notebook
Providers and resources are the heart of Terraform. Without providers, Terraform has no voice; without resources, it has nothing to build. By mastering this chapter, you’ve unlocked the foundation of every Terraform project. From here, you’ll move on to variables and outputs, which make your configurations flexible and reusable like adding adjustable gears to your Lego machines.
