Terraform Tutorial — Providers Practice Exercises
Test your understanding of provider configuration, versioning, and aliases.
Exercise 1: required_providers
Task: Write a terraform {} block that requires the AWS provider from HashiCorp with a version constraint that allows any 5.x release, and requires Terraform CLI version 1.7.0 or higher.
Answer
terraform {
required_version = ">= 1.7.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
Exercise 2: Version Constraints
Task: What versions would each constraint allow? Write the range for each.
version = "~> 5.31.0"version = "~> 5.0"version = ">= 5.0.0, < 6.0.0"version = "= 5.31.0"
Answer
~> 5.31.0→ >= 5.31.0, < 5.32.0 (patch versions only: 5.31.0, 5.31.1, 5.31.2, …)~> 5.0→ >= 5.0.0, < 6.0.0 (any 5.x version)>= 5.0.0, < 6.0.0→ Same as #2 — any 5.x version= 5.31.0→ Only version 5.31.0
Exercise 3: Multi-Region Setup
Task: Write provider configurations for AWS in us-east-1 (default) and ap-southeast-1 (aliased as aws.singapore). Then write two aws_s3_bucket resources — one in each region.
Answer
provider "aws" {
region = "us-east-1"
}
provider "aws" {
alias = "singapore"
region = "ap-southeast-1"
}
resource "aws_s3_bucket" "us_data" {
bucket = "myapp-us-data"
}
resource "aws_s3_bucket" "sg_data" {
provider = aws.singapore
bucket = "myapp-sg-data"
}
Exercise 4: Default Tags
Task: Configure the AWS provider with default tags so that every resource gets ManagedBy = "terraform" and Project = "web-app" tags without specifying them on each resource.
Answer
provider "aws" {
region = "us-east-1"
default_tags {
tags = {
ManagedBy = "terraform"
Project = "web-app"
}
}
}
Exercise 5: Provider Discovery
Task: You want to manage GitHub repositories and Cloudflare DNS records alongside your AWS infrastructure. Write the required_providers block with all three providers.
Answer
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
github = {
source = "integrations/github"
version = "~> 6.0"
}
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.0"
}
}
}
Note: The GitHub provider is published under integrations/github, not hashicorp/github.
Chapter Summary
In this chapter, you learned:
- A provider is a plugin that connects Terraform to a specific platform’s API
- The
required_providersblock declares which providers you need, their source, and version constraints - The
~>operator is the most common version constraint — it allows minor/patch upgrades - The
.terraform.lock.hclfile pins exact versions for reproducibility - Provider aliases let you use multiple configurations of the same provider (multi-region, multi-account)
- Default tags save repetition across all resources
Next up: Resources — defining infrastructure in code.