The AWS Provider: Auth, Regions & Tags
This section writes the infrastructure from the AWS course as code: a VPC across availability zones, internet and NAT gateways, security groups, an RDS PostgreSQL database and an EKS cluster. First, the provider that talks to AWS: hashicorp/aws, version 6 in this course (6.70.0 in the sandbox).
terraform {
required_version = ">= 1.6"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
provider "aws" {
region = "eu-west-1"
allowed_account_ids = ["123456789012"] # refuse to run against any other account
default_tags {
tags = {
Project = "linkly"
Environment = "prod"
ManagedBy = "opentofu"
Repository = "github.com/acme/linkly-infra"
}
}
}Where credentials come from
Never put access keys in .tf files. The provider finds credentials the same way the AWS CLI and SDKs do, in this order (simplified):
access_key/secret_keyin the provider block (don't);- environment variables
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKEN; - the shared config and credentials files, with a profile from
profile = "…"orAWS_PROFILE(including IAM Identity Center / SSO profiles afteraws sso login); - web identity tokens (
AWS_ROLE_ARN+AWS_WEB_IDENTITY_TOKEN_FILE): how EKS pods and CI systems with OIDC authenticate; - the container credentials endpoint (ECS tasks) and the EC2 instance metadata service (an instance profile).
| Who runs it | Recommended |
|---|---|
| an engineer | IAM Identity Center profile, short-lived credentials: aws sso login --profile linkly-dev |
| GitHub Actions / GitLab CI | OIDC federation to an IAM role, no stored secrets (Plan on Pull Request) |
| a runner inside AWS (CodeBuild, an EC2 or EKS runner) | its own IAM role |
Cross-account setups keep one set of credentials and assume a role per account:
provider "aws" {
region = "eu-west-1"
assume_role {
role_arn = "arn:aws:iam::210987654321:role/linkly-deploy"
session_name = "opentofu"
}
}The IAM lessons of the AWS course explain roles, trust policies and least privilege; the role used by plan on pull requests should be read-only, the one used by apply on main can write.
default_tags
Tags set in default_tags are added to every taggable resource the provider creates. The resource's own tags are merged on top, and tags_all shows the result. Planned offline with the teaching setup from Data Sources:
mkdir tags && cd tags
cat > main.tf <<'HCL'
terraform {
required_providers {
aws = { source = "hashicorp/aws", version = "~> 6.0" }
}
}
provider "aws" {
region = "eu-west-1"
access_key = "offline" # teaching setup only, see above
secret_key = "offline"
skip_credentials_validation = true
skip_requesting_account_id = true
skip_metadata_api_check = true
default_tags {
tags = { Project = "linkly", ManagedBy = "opentofu" }
}
}
resource "aws_sqs_queue" "jobs" {
name = "linkly-jobs"
tags = { Team = "platform", Project = "linkly-jobs" }
}
HCL
tofu init >/dev/null
tofu plan | sed -n '/+ tags /,/^ }/p;/+ tags_all/,/^ }/p' + tags = {
+ "Project" = "linkly-jobs"
+ "Team" = "platform"
}
+ tags_all = {
+ "ManagedBy" = "opentofu"
+ "Project" = "linkly-jobs"
+ "Team" = "platform"
}
The resource-level Project wins over the default. Cost reports, ownership lookups and cleanup scripts all depend on consistent tags, so set them once in the provider rather than on each resource.
Provider-defined functions
AWS provider 6 also ships functions, usable in any expression as provider::aws::<name> (OpenTofu 1.7+, Terraform 1.8+). They run inside the provider without calling AWS, so no credentials are needed:
mkdir fn && cd fn
cat > main.tf <<'HCL'
terraform {
required_providers {
aws = { source = "hashicorp/aws", version = "~> 6.0" }
}
}
locals {
deploy_role = provider::aws::arn_parse("arn:aws:iam::123456789012:role/linkly-deploy")
}
output "account_id" {
value = local.deploy_role.account_id
}
output "bucket_arn" {
value = provider::aws::arn_build("aws", "s3", "", "", "linkly-logs")
}
HCL
tofu init >/dev/null
tofu apply -auto-approve | sed -n '/^Outputs:/,$p'Outputs:
account_id = "123456789012"
bucket_arn = "arn:aws:s3:::linkly-logs"
Regions
AWS provider 6 added a region argument to most resources, so one provider configuration can place resources in several regions; aliases (from Providers) are still the tool when other settings differ, such as a different account role. Some services are global or pinned to us-east-1 (CloudFront certificates in ACM, some WAF and billing resources).
Test yourself
Where should a CI pipeline get its AWS credentials for Terraform/OpenTofu?
- Access keys in
provider "aws" {} - Long-lived keys in a repository secret
- OIDC federation to an IAM role, giving short-lived credentials per run
- The developer's laptop
default_tags has Project = "linkly" and a resource sets tags = { Project = "jobs" }. What ends up in tags_all?
Project = "linkly"Project = "jobs"- Both, as a list
- An error
What does allowed_account_ids protect against?
- Slow plans
- Running the configuration with credentials for the wrong AWS account
- Public buckets
- Missing tags