The Alibaba Cloud Terraform Provider supports multiple authentication methods. Choose one based on your runtime environment and security requirements.
Authentication method priority
When configuring the Alibaba Cloud provider, select one authentication method. The provider searches for credentials in this order and stops at the first match:
-
Static configuration: The provider first reads the access_key, secret_key, security_token, or ecs_role_name parameters if they are explicitly specified in the provider block.
-
Environment variables: If no static configuration is found, the provider reads system environment variables, such as ALICLOUD_ACCESS_KEY and ALICLOUD_SECRET_KEY.
-
Shared configuration file:
-
Static configuration: The provider reads authentication information for the specified profile from the local shared configuration file. This information can include an AccessKey pair, ram_role_arn, ram_role_name, or sts_token.
-
Advanced configuration: If the shared configuration file does not contain a static configuration but is configured with CloudSSO, OAuth, an external program, or a chainable RAM role (ChainableRamRoleArn), the provider invokes the corresponding plugin to obtain a temporary Security Token Service (STS) credential.
-
-
URL credential: If none of the above methods provide a credential, the provider attempts to retrieve credentials from the URL specified by credentials_uri.
Security recommendations
For production environments, use these temporary credential-based methods:
-
Instance RAM role for ECS
-
RAM role assumption (AssumeRole)
-
OIDC IdP role assumption
Security advantages:
-
Eliminates hard-coded long-lived AccessKeys, reducing key compromise risk.
-
Limits permission duration through credential expiration.
Support for fine-grained authorization
For fine-grained access control by application, team, or project, use RAM role assumption and OIDC IdP role assumption. Attach dedicated roles and policies to each Terraform workload to enforce least privilege.
Terraform authentication methods
The Alibaba Cloud Terraform Provider supports these authentication methods:
|
Authentication method |
Instructions |
Use Cases |
|
(access_key) |
Defines an AccessKey pair directly in the configuration file. |
|
|
(ALIBABA_CLOUD_ACCESS_KEY) |
Reads AccessKey information from environment variables. |
|
|
(ecs_role_name) |
Obtains temporary credentials from the ECS instance metadata via the attached RAM role. |
Terraform runs on an ECS instance. |
|
OIDC role assumption (assume_role_with_oidc) |
Assumes a RAM role through an OIDC identity provider to obtain temporary credentials. |
Terraform runs in a Kubernetes cluster that supports OIDC, such as Alibaba Cloud ACK. |
|
(assume_role) |
Assumes a RAM role to obtain temporary credentials. |
|
|
(credentials_uri) |
Retrieves temporary credentials from a specified URI. |
Custom credential services. |
|
(profile) |
Manages multiple credential profiles in a single Alibaba Cloud CLI configuration file. Supported methods:
|
|
Shared configuration file
Terraform reads credentials from the Alibaba Cloud CLI's config.json file. Configure multiple credential types in one file and reference them by profile name. Configure credentials.
Default file paths:
-
Linux/macOS:
~/.aliyun/config.json -
Windows:
C:\Users\USER_NAME\.aliyun\config.json
Configuration example
Use profile to specify the credential name in the provider block. If the file is not at the default path, set shared_credentials_file:
provider "alicloud" {
region = "cn-hangzhou"
shared_credentials_file = "~/.aliyun/config.json"
profile = "TerraformTest"
}
Example config.json:
{
"current": "<PROFILE_NAME>",
"profiles": [
{
"name": "<PROFILE_NAME>",
"mode": "AK",
"access_key_id": "<ALIBABA_CLOUD_ACCESS_KEY_ID>",
"access_key_secret": "<ALIBABA_CLOUD_ACCESS_KEY_SECRET>"
},
{
"name": "<PROFILE_NAME1>",
"mode": "StsToken",
"access_key_id": "<ALIBABA_CLOUD_ACCESS_KEY_ID>",
"access_key_secret": "<ALIBABA_CLOUD_ACCESS_KEY_SECRET>",
"sts_token": "<SECURITY_TOKEN>"
},
{
"name": "<PROFILE_NAME2>",
"mode": "RamRoleArn",
"access_key_id": "<ALIBABA_CLOUD_ACCESS_KEY_ID>",
"access_key_secret": "<ALIBABA_CLOUD_ACCESS_KEY_SECRET>",
"ram_role_arn": "<ROLE_ARN>",
"ram_session_name": "<ROLE_SESSION_NAME>",
"expired_seconds": 3600
},
{
"name": "<PROFILE_NAME3>",
"mode": "EcsRamRole",
"ram_role_name": "<RAM_ROLE_ARN>"
},
{
"name": "<PROFILE_NAME4>",
"mode": "External",
"process_command": "<YOUR_COMMAND>",
"region_id": "<REGION_ID>",
"output_format": "json",
"language": "en"
},
{
"name": "<PROFILE_NAME5>",
"mode": "OIDC",
"oidc_provider_arn": "<OIDC_PROVIDER_ARN>",
"oidc_token_file": "<OIDC_TOKEN_FILE>",
"ram_role_arn": "<ROLE_ARN>",
"ram_session_name": "<ROLE_SESSION_NAME>",
"expired_seconds": 3600
},
{
"name": "<PROFILE_NAME6>",
"mode": "ChainableRamRoleArn",
"source_profile": "<PROFILE_NAME>",
"ram_role_arn": "<ROLE_ARN>",
"ram_session_name": "<ROLE_SESSION_NAME>",
"expired_seconds": 3600
},
{
"name": "<PROFILE_NAME7>",
"mode": "CloudSSO",
"cloud_sso_sign_in_url": "https://******/login",
"access_token": "eyJraWQiOiJiYzViMzUwYy******",
"cloud_sso_access_token_expire": 1754316142,
"cloud_sso_access_config": "ac-00s1******",
"cloud_sso_account_id": "151266******"
},
{
"name": "<PROFILE_NAME8>",
"mode": "OAuth",
"access_key_id": "<ALIBABA_CLOUD_ACCESS_KEY_ID>",
"access_key_secret": "<ALIBABA_CLOUD_ACCESS_KEY_SECRET>",
"sts_token": "<SECURITY_TOKEN>",
"region_id": "<REGION_ID>",
"output_format": "json",
"language": "<zh|en>",
"sts_expiration": "<STS_EXPIRATION>",
"oauth_access_token": "<OAUTH_ACCESS_TOKEN>",
"oauth_refresh_token": "<OAUTH_REFRESH_TOKEN>",
"oauth_access_token_expire": 1754316142,
"oauth_site_type": "<CN|EN>"
}
]
}
In config.json, the mode parameter specifies the credential type:
-
AK: Uses the user's AccessKey as the credential.
-
StsToken: Uses an STS token as the credential.
-
RamRoleArn: Obtains the credential using the ARN of a RAM role.
-
EcsRamRole: Obtains the credential using the RAM role attached to an ECS instance.
-
External: Obtains a credential by running an external command.
-
OIDC: Obtains the credential using an OIDC IdP ARN and an OIDC token.
-
ChainableRamRoleArn: Role chain. Uses
source_profileto reference another credential inconfig.jsonand obtains a new credential through role assumption. -
OAuth: Credential obtained by logging in to the CLI using OAuth.
-
CloudSSO: Credential obtained by a CloudSSO user via the Alibaba Cloud CLI.