So far, we’ve built a single resource in Terraform using variables and outputs.
But in real-world projects, you’ll need:
- Multiple resources (e.g., S3 buckets, EC2 instances, databases)
- Integration with different providers (AWS, Azure, GCP, Kubernetes, etc.)
In this blog, we’ll cover:
- What Providers are in Terraform
- Creating multiple resources efficiently
- Real-world use cases and best practices
What are Providers in Terraform?
Think of providers as plugins that let Terraform talk to different services.
- AWS Provider → Manages AWS services like S3, EC2, RDS.
- Azure Provider → Manages Azure resources like VMs, Storage, Databases.
- GCP Provider → Manages Google Cloud resources like Buckets, VMs, BigQuery.
When you run terraform init
, it downloads the required provider plugins.
Example: AWS Provider Setup
provider "aws" {
region = var.region
}
Here:
- provider "aws" → Tells Terraform we’re using AWS
- region → Where resources will be deployed
Creating Multiple Resources
Let’s say we want 3 S3 buckets.
Instead of writing 3 separate resource blocks, we can use the count
argument.
resource "aws_s3_bucket" "my_buckets" {
count = 3
bucket = "my-terraform-bucket-${count.index}"
acl = "private"
}
This will create:
- my-terraform-bucket-0
- my-terraform-bucket-1
- my-terraform-bucket-2
Using for_each
for Named Buckets
If you want custom names:
variable "bucket_names" {
default = ["dev-bucket", "staging-bucket", "prod-bucket"]
}
resource "aws_s3_bucket" "my_buckets" {
for_each = toset(var.bucket_names)
bucket = each.key
acl = "private"
}
Now each bucket gets a name from the list.
Real-World Case Study: Multi-Environment Infrastructure
A startup managing dev, staging, and prod environments:
- Used for_each to create resources for each environment automatically.
- Added environment-specific tags for easy cost tracking in AWS.
- Used one Terraform script for all environments instead of maintaining 3.
Result: Reduced code duplication by 70%, simplified deployments.
Multiple Providers in One Project
Sometimes you need resources across multiple clouds or services.
Example: AWS for compute + Cloudflare for DNS.
provider "aws" {
region = "us-east-1"
}
provider "cloudflare" {
api_token = var.cloudflare_api_token
}
Now you can create AWS S3 buckets and Cloudflare DNS records in one Terraform project.
Best Practices
- Separate provider configurations for clarity when using multiple providers.
- Use variables for region, environment, and sensitive data.
- Tag all resources with environment and owner info for cost tracking.
- Use workspaces for managing dev/staging/prod environments cleanly.
What’s Next?
Now we know:
- How providers connect Terraform to services
- How to create multiple resources with minimal code
Bibliography
- HashiCorp. Terraform State Documentation. Available at: https://developer.hashicorp.com/terraform/language/stateHashiCorp.
- Terraform Backend Configuration. Available at: https://developer.hashicorp.com/terraform/language/settings/backends
- AWS. S3 Backend for Terraform. Available at: https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.htmlHashiCorp.
- Terraform Remote State Best Practices. Available at: https://developer.hashicorp.com/terraform/cloud-docs/workspaces/state
- Azure. Azure Storage Backend for Terraform. Available at: https://learn.microsoft.com/en-us/azure/developer/terraform/store-state-in-azure-storage