Terraform Cloud Provider Authentication
A cloud platform never builds anything for a stranger. Terraform must prove who it is before AWS, Azure, or Google Cloud creates a single server. Think of a hotel key card. The front desk issues a card, and the door opens only for the person holding it. Credentials work as that key card for Terraform. Safe methods exist for handing Terraform its card, and each one appears below.
The Golden Rule: Keep Secrets Out of Code
Never type an access key into a .tf file. Configuration files live in Git, and anyone with repository access can read every line. A leaked key lets strangers create resources on your bill. Supply credentials from outside the code instead.
Wrong Way
provider "aws" {
region = "us-east-1"
access_key = "AKIAEXAMPLEKEY" # never do this
secret_key = "example-secret-value" # never do this
}Right Way
provider "aws" {
region = "us-east-1"
}The provider block stays clean. Credentials arrive through environment variables, a profile, or a role.
How the AWS Provider Finds Credentials
The AWS provider checks several places in a fixed order and stops at the first match.
Diagram: AWS Credential Search Order
Terraform needs credentials
|
v
+---------------------------------+
| 1. Provider block arguments | (avoid this one)
+---------------------------------+
| not found
v
+---------------------------------+
| 2. Environment variables | AWS_ACCESS_KEY_ID ...
+---------------------------------+
| not found
v
+---------------------------------+
| 3. Shared credentials / profile | ~/.aws/credentials
+---------------------------------+
| not found
v
+---------------------------------+
| 4. Role attached to the machine | EC2 or container role
+---------------------------------+
|
v
Login succeeds or failsMethod 1: Environment Variables
Environment variables suit a laptop or a pipeline. They exist only inside the terminal session.
export AWS_ACCESS_KEY_ID="YOUR_ACCESS_KEY_ID"
export AWS_SECRET_ACCESS_KEY="YOUR_SECRET_ACCESS_KEY"
export AWS_REGION="us-east-1"
terraform planMethod 2: Named Profiles
A profile stores credentials under a friendly name. The AWS CLI creates the profile when you run aws configure --profile dev. Point Terraform to the profile in the provider block.
provider "aws" {
region = "us-east-1"
profile = "dev"
}Profiles make it easy to switch between a personal account and a company account.
Method 3: Roles
A role grants temporary permissions without any stored key. Servers and pipelines use roles because temporary access expires on its own, which limits damage when something goes wrong.
provider "aws" {
region = "us-east-1"
assume_role {
role_arn = "arn:aws:iam::123456789012:role/TerraformDeployRole"
}
}Azure Authentication
The simplest start is the Azure CLI. Run az login in the terminal and the Azure provider borrows that session.
az login
az account set --subscription "YOUR_SUBSCRIPTION_ID"Automated systems use a service principal. A service principal acts like a robot user. Provide its details through environment variables.
export ARM_CLIENT_ID="YOUR_APP_ID"
export ARM_CLIENT_SECRET="YOUR_APP_SECRET"
export ARM_TENANT_ID="YOUR_TENANT_ID"
export ARM_SUBSCRIPTION_ID="YOUR_SUBSCRIPTION_ID"Google Cloud Authentication
Application default credentials let the Google provider reuse your login.
gcloud auth application-default loginA pipeline points to a service account key file through one variable.
export GOOGLE_APPLICATION_CREDENTIALS="/secure/path/key.json"Choosing the Right Method
| Situation | Best Choice |
|---|---|
| Learning on a personal laptop | CLI login or a named profile |
| Team laptop with several accounts | Named profiles |
| Pipeline in the cloud | Role or service principal |
| Server inside the cloud | Role attached to the machine |
Safety Checklist
- Add
.env,*.tfvarswith secrets, and key files to.gitignore. - Grant only the permissions Terraform needs. A narrow permission set limits mistakes.
- Rotate keys on a schedule and delete keys nobody uses.
- Prefer temporary credentials over long-lived keys.
- Rotate any key immediately when it appears in a commit.
Key Points
- Terraform must authenticate before a cloud platform builds anything.
- Credentials never belong inside
.tffiles. - AWS checks the provider block, environment variables, profiles, and machine roles in that order.
- Azure uses
az loginfor learning and a service principal for automation. - Google Cloud uses application default credentials or a service account key file.
- Roles and temporary credentials beat permanent keys for safety.
