Terraform providers: required_providers, the AWS provider, aliases and data sources
A Terraform provider is a plugin that lets Terraform manage one platform's API, such as AWS, Azure or GitHub. You declare it in required_providers with a source address (hashicorp/aws) and a version constraint (~> 6.0), configure it in a provider block (region, credentials from the environment), and terraform init downloads it and records the exact version in .terraform.lock.hcl.

What is a Terraform provider?
Terraform's core knows the language, the plan and the state, but nothing about any particular cloud. Providers fill that gap. Each provider is a separate program that Terraform downloads and runs; it defines the resource types and data sources for one platform and turns create, read, update and delete into API calls.
The prefix of a resource type tells you its provider: aws_s3_bucket belongs to the AWS provider, azurerm_resource_group to the Azure provider, random_pet to the random provider. Providers are versioned and released separately from Terraform, so the AWS provider can add support for a new AWS service without a new Terraform release.
Providers are published on the Terraform Registry in three tiers: official (maintained by HashiCorp, under the hashicorp namespace, such as hashicorp/aws, hashicorp/azurerm, hashicorp/google), partner (maintained by the vendor, such as cloudflare/cloudflare or DataDog/datadog) and community.
Declaring providers with required_providers
Every configuration should say which providers it uses, in the terraform block:
terraform {
required_version = ">= 1.12"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.6"
}
}
}Each entry has three parts:
- Local name (
aws): the name used in this module, inprovider "aws"blocks and by default for resources startingaws_. Use the provider's preferred name unless two providers clash. - Source address (
hashicorp/aws): where to download it. The full form isHOSTNAME/NAMESPACE/TYPE; when the hostname is left out it isregistry.terraform.io, sohashicorp/awsmeansregistry.terraform.io/hashicorp/aws. Private registries use their own hostname. - Version constraint (
~> 6.0): which releases are acceptable.
If you use a resource without declaring its provider, Terraform assumes hashicorp/<prefix>. That works for official providers but fails for partner ones, so declare every provider explicitly.
Version constraints and the ~> operator
| Constraint | Allows |
|---|---|
= 6.74.0 or 6.74.0 | Exactly that version |
>= 6.0 | 6.0 or newer, including 7.x and later |
>= 6.10, < 7.0 | A range: both conditions must hold |
~> 6.0 | 6.0 and above, below 7.0 |
~> 6.10.0 | 6.10.0 and above, below 6.11.0 |
!= 6.5.0 | Anything except 6.5.0 |
~>, the pessimistic constraint operator, lets only the rightmost number given grow. It is the usual choice in root modules: you get bug fixes and new features but not the next major version, which may contain breaking changes. Reusable modules should state a minimum (>= 6.0) and leave the upper bound to the root module that calls them.
The dependency lock file and terraform init -upgrade
Constraints describe a range; the lock file pins the exact pick. The first terraform init chooses the newest version that matches and writes .terraform.lock.hcl:
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/aws" {
version = "6.74.0"
constraints = "~> 6.0"
hashes = [
"h1:...",
"zh:...",
]
}Commit this file. Later runs of terraform init, on your machine, a colleague's or in CI, install exactly 6.74.0 and verify the package against the recorded hashes, even if 6.80 has been released. To move to a newer version within your constraints, run:
terraform init -upgradeand commit the updated lock file. If you tighten a constraint so the locked version no longer matches, plan stops with Inconsistent dependency lock file until you run init -upgrade. The lock file only covers providers, not modules.
The hashes are per platform. If the team mixes macOS and Linux, record both so nobody gets a checksum mismatch:
terraform providers lock -platform=darwin_arm64 -platform=linux_amd64Configuring the Terraform AWS provider
A provider block configures a provider for the whole configuration. For AWS the essentials are the region and, often, tags applied to every resource:
provider "aws" {
region = "eu-west-2"
default_tags {
tags = {
project = "payments"
managed_by = "terraform"
}
}
}default_tags are merged into every taggable AWS resource; tags set on a resource win where keys clash. In plans, tags shows what you set on the resource and tags_all shows the merged result.
Provider blocks belong in the root module. Child modules declare the providers they need in required_providers and receive configurations from the caller. Since version 6 the AWS provider also accepts a region argument on most resources, which overrides the provider's region for that one resource.
AWS provider authentication: never hard-code keys
The AWS provider looks for credentials in the same places as the AWS CLI and SDKs. The usual options, best first:
- IAM Identity Center (SSO) profiles on laptops: run
aws sso login --profile dev, thenexport AWS_PROFILE=devor setprofile = "dev"in the provider block. - Shared config and credentials files (
~/.aws/config,~/.aws/credentials) with named profiles. - Environment variables:
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKEN, andAWS_REGION. CI systems usually set these from short-lived credentials, for example GitHub Actions exchanging an OIDC token for a role. - Workload identity when Terraform runs inside AWS: an EC2 instance profile or ECS task role, picked up automatically.
- Assuming a role on top of any of these:
provider "aws" {
region = "us-east-1"
assume_role {
role_arn = "arn:aws:iam::111122223333:role/terraform"
session_name = "terraform"
}
}The provider block also accepts access_key and secret_key arguments. Don't use them with real keys: the configuration is committed to Git, and the keys would be readable by everyone with access to the repository. If Terraform can't find any credentials, plan fails with No valid credential sources found; check aws sts get-caller-identity with the same profile and environment.
Multiple providers: aliases for regions and accounts
To use the same provider with two configurations, for example two regions, give the extra one an alias. A provider block without an alias is the default.
provider "aws" {
region = "us-east-1"
}
provider "aws" {
alias = "west"
region = "us-west-2"
}
resource "aws_s3_bucket" "primary" {
bucket = "acme-data-primary"
}
resource "aws_s3_bucket" "replica" {
provider = aws.west
bucket = "acme-data-replica"
}Resources and data sources select a non-default configuration with provider = aws.west. The same pattern covers several accounts: give each alias its own assume_role with a different role_arn.
Modules receive provider configurations through the providers argument:
module "replica_bucket" {
source = "./modules/bucket"
providers = {
aws = aws.west
}
}A module that needs more than one configuration of a provider declares the names it expects with configuration_aliases:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 6.0"
configuration_aliases = [aws.primary, aws.replica]
}
}
}The caller then maps both: providers = { aws.primary = aws, aws.replica = aws.west }. Multiple provider configurations are an explicit Terraform Associate objective (2c).
Terraform data sources vs resources
Providers offer two kinds of block. A resource creates and manages an object. A data source only reads: an existing VPC, the latest AMI, the current account ID.
data "aws_caller_identity" "current" {}
data "aws_ami" "al2023" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["al2023-ami-*-x86_64"]
}
}
resource "aws_instance" "web" {
ami = data.aws_ami.al2023.id
instance_type = "t3.micro"
tags = {
owner = data.aws_caller_identity.current.account_id
}
}resource | data | |
|---|---|---|
| Effect | Creates, updates and deletes the object | Reads existing information |
| Reference | aws_instance.web.id | data.aws_ami.al2023.id |
| When | Changes happen during apply | Usually read during plan |
| On destroy | The object is deleted | Nothing happens to the real object |
If a data source depends on a value that isn't known until apply, Terraform defers the read and the plan shows it with <=. Data sources are how one configuration uses infrastructure managed elsewhere without taking ownership of it; for outputs of another Terraform configuration there is the terraform_remote_state data source.
The terraform providers command
terraform providers shows which providers the configuration and the state need, and which module asked for each:
$ terraform providers
Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/aws] ~> 6.0
├── provider[registry.terraform.io/hashicorp/random] ~> 3.6
└── module.network
└── provider[registry.terraform.io/hashicorp/aws] >= 6.0
Providers required by state:
provider[registry.terraform.io/hashicorp/aws]It is the quickest way to debug a version conflict between modules. Related subcommands:
terraform providers lock: add hashes for other platforms to the lock file.terraform providers mirror DIR: download providers into a local directory for air-gapped installs.terraform providers schema -json: print every resource and argument the installed providers support.
terraform version also lists the installed provider versions. See all Terraform commands.
Common provider errors
- Failed to query available provider packages: a typo in
source, a partner provider used without declaring its source, or no network to the registry. - Inconsistent dependency lock file or required provider is not installed: run
terraform init, orterraform init -upgradeafter changing a constraint. - No valid credential sources found: the AWS provider found no credentials. Log in (
aws sso login), exportAWS_PROFILE, or check the CI role. - Provider configuration not present: you removed a provider block (often an alias) while resources in state still use it. Put it back, destroy or move those resources, then remove it.
- Checksum mismatch on another platform: the lock file lacks hashes for it. Run
terraform providers lockwith each-platform.
For what init does in detail, including backends and -upgrade, see terraform init.
Try it in a Lab
Chapter 2 of Planfile, Providers and resources, is free and covers the terraform block, version constraints, resources, references and data sources. Its Lab Connect a subnet to its VPC has you wire resources together and add an output; the Lab's init installs simulated providers, so no AWS credentials are needed.
Questions
What is the Terraform AWS provider?
It is the official HashiCorp plugin, source hashicorp/aws, that lets Terraform manage AWS resources such as aws_vpc, aws_instance and aws_s3_bucket. Version 6 is the current major version.
What does ~> mean in a Terraform version constraint?
It allows only the rightmost given number to increase. ~> 6.0 accepts 6.x but not 7.0; ~> 6.10.0 accepts 6.10.x but not 6.11.
Should I commit .terraform.lock.hcl?
Yes. It pins exact provider versions and checksums so every machine and CI run installs the same providers. Update it deliberately with terraform init -upgrade.
What is the difference between a data source and a resource in Terraform?
A resource creates and manages an object; a data source only reads information about something that already exists. Data sources are referenced with the data. prefix.
How do I use two AWS regions in one Terraform configuration?
Add a second provider "aws" block with an alias and a different region, then set provider = aws.ALIAS on the resources that should use it, or pass it to a module with providers = { aws = aws.ALIAS }.
Where do HashiCorp Terraform providers come from?
terraform init downloads them from the Terraform Registry (registry.terraform.io) by default, or from a private registry, a local mirror or a filesystem directory if you configure one.