The Alibaba Cloud Terraform Provider supports multiple authentication methods. The appropriate method depends on your Terraform runtime environment and scenario.
Authentication method priority
When you configure the Alibaba Cloud provider, you typically select only one authentication method. The Terraform Provider searches for identity credentials in the following order and stops as soon as it finds a valid credential:
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 enhanced security in production environments, use the following authentication methods that rely on temporary credentials:
Instance RAM role for ECS
RAM role assumption (AssumeRole)
OIDC IdP role assumption
These methods provide the following security advantages:
Avoid hard coding or exposing long-lived AccessKeys in your configuration to reduce the risk of key compromise.
They limit the duration of permissions using credentials with fixed expiration periods.
Support for fine-grained authorization
For fine-grained control over resource access based on dimensions such as applications, teams, or projects, use RAM role assumption and OIDC IdP role assumption. You can attach dedicated roles and access policies to different Terraform workloads to follow the least privilege principle.
Terraform authentication methods
The Alibaba Cloud Terraform Provider offers the following authentication methods:
Authentication method | Instructions | Use Cases |
(access_key) | Defines an AccessKey (AK) in plaintext in the configuration file, or passes it as a parameter. |
|
(ALIBABA_CLOUD_ACCESS_KEY) | Reads AccessKey (AK) information from environment variables. |
|
(ecs_role_name) | Obtains the access credential for the attached RAM role from the metadata of an ECS instance. | Terraform runs on an ECS instance. |
OIDC role assumption (assume_role_with_oidc) | Obtains an access credential by assuming a role through OpenID Connect (OIDC). | Terraform runs in a Kubernetes cluster that supports OIDC, such as Alibaba Cloud ACK. |
(assume_role) | Obtains an access credential by assuming a RAM role. |
|
(credentials_uri) | Specifies a URI to obtain a temporary credential. | Obtaining credentials from a custom credential service, such as an API or a file. |
(profile) | Configures multiple authentication methods in a unified file using the Alibaba Cloud command-line interface (CLI). Obtains an access credential by specifying a profile name. The supported authentication methods include the following:
|
|
Shared configuration file
Terraform supports using access credentials from a shared configuration file. The shared configuration file is the config.json file generated by the Alibaba Cloud command-line interface (CLI). You can configure multiple types of credential information, such as static AccessKeys, ECS roles, and role assumption, in a single file and reference them using a profile name. For more information about configuration methods, see Configure credentials.
The default paths for the shared configuration file are as follows:
Linux/macOS:
~/.aliyun/config.jsonWindows:
C:\Users\USER_NAME\.aliyun\config.json
Configuration example
In the Terraform provider configuration, you can use the profile parameter to specify the name of the credential configuration to use. If the configuration file is not in the default path, you can use the shared_credentials_file parameter to specify the absolute path of the configuration file. The following code provides a sample configuration:
provider "alicloud" {
region = "cn-hangzhou"
shared_credentials_file = "~/.aliyun/config.json"
profile = "TerraformTest"
}The following is an example of a config.json file:
{
"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 the config.json file, you can use the mode parameter to specify different credential types:
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: Dynamically obtains a credential by running an external program command.
OIDC: Obtains the credential using an OIDC IdP ARN and an OIDC token.
ChainableRamRoleArn: Uses a role chain. It specifies the name of another credential in the
config.jsonfile usingsource_profileto obtain a new credential.OAuth: The credential obtained by logging on to the CLI using OAuth.
CloudSSO: The credential obtained by a CloudSSO user using the Alibaba Cloud CLI.