Terraform Tutorial · Terraform Basics · Lesson 4 of 25
Terraform Providers
Providers are plugins that let Terraform manage a platform such as AWS, Azure, Google Cloud or Kubernetes. Learn how to declare, configure and pin them.
What is a provider?
A provider is a plugin that knows how to talk to one API. The AWS provider creates EC2 instances and S3 buckets; the Kubernetes provider creates deployments; the GitHub provider creates repositories. There are thousands on the Terraform Registry.
Declaring providers
terraform {
required_version = ">= 1.6"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0" # any 6.x version
}
}
}
provider "aws" {
region = "eu-west-1"
}
terraform init downloads the providers and records their exact versions and checksums in
.terraform.lock.hcl.
Version constraints
| Constraint | Means |
|---|---|
= 6.2.0 |
Exactly this version |
>= 6.0 |
6.0 or newer (risky: includes future major versions) |
~> 6.2 |
6.2 or newer, but below 7.0 |
~> 6.2.1 |
6.2.1 or newer, but below 6.3 |
~> (the "pessimistic" operator) is the usual choice: you get fixes and features, but not breaking major
upgrades.
Several regions or accounts: aliases
provider "aws" {
region = "eu-west-1"
}
provider "aws" {
alias = "us"
region = "us-east-1"
}
resource "aws_s3_bucket" "logs_us" {
provider = aws.us
bucket = "my-logs-us"
}
Credentials
Never put access keys in .tf files. Providers read credentials from the environment (for AWS:
AWS_PROFILE, aws sso login, or an IAM role when running in CI or on EC2).
Test yourself
Which constraint allows 5.4.0 and 5.9.1 but not 6.0.0?
= 5.4>= 5.4~> 5.4~> 5.4.0
Show answer
Answer: ~> 5.4