All Products
Search
Document Center

Terraform:Terraform authentication

Last Updated:Jun 04, 2026

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:

  1. 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.

  2. Environment variables: If no static configuration is found, the provider reads system environment variables, such as ALICLOUD_ACCESS_KEY and ALICLOUD_SECRET_KEY.

  3. Shared configuration file:

    1. 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.

    2. 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.

  4. 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

Static configuration

(access_key)

Defines an AccessKey pair directly in the configuration file.

  • Development and testing.

  • CI/CD workflows.

Environment variables

(ALIBABA_CLOUD_ACCESS_KEY)

Reads AccessKey information from environment variables.

  • Development and testing.

  • Standalone runtime environments, such as ECS instances.

Instance RAM role for ECS

(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.

Role assumption

(assume_role)

Assumes a RAM role to obtain temporary credentials.

  • Multi-account resource management.

  • CI/CD workflows.

URL credential

(credentials_uri)

Retrieves temporary credentials from a specified URI.

Custom credential services.

Shared configuration file

(profile)

Manages multiple credential profiles in a single Alibaba Cloud CLI configuration file. Supported methods:

  • AK

  • STSToken

  • ECSRamRole

  • CloudSSO

  • ChainableRamRoleArn

  • External

  • OAuth

  • Multi-account and multi-region resource management.

  • Multi-environment resource management.

Static configuration

Define access credentials directly in the provider block of a Terraform configuration file.

Parameters

Parameter

Description

Example value

access_key

The AccessKey ID.

abcd*******

secret_key

The AccessKey secret.

abcd*******

security_token (Optional)

The security token of a temporary STS credential.

abcd*******

provider "alicloud" {
  access_key = "<Your AccessKey ID>"
  secret_key = "<Your AccessKey secret>"
  # If you use an STS credential, configure security_token.
  # security_token = "<Your STS token>"
}

Example configuration

For security, pass credentials as variables instead of hard-coding them. Do not set default values:

variable "access_key_id" {
  description = "The AccessKey ID for operating your infrastructure"
}
variable "access_key_secret" {
  description = "The AccessKey Secret for operating your infrastructure"
}
variable "security_token" {
  description = "The Security Token for operating your infrastructure"
}
provider "alicloud" {
  access_key = var.access_key_id
  secret_key = var.access_key_secret
  # If you use an STS credential, configure security_token.
  # security_token = var.security_token
}

Use the terraform command with the -var option to pass variable values:

$ terraform plan -var access_key_id="<Your AccessKey ID>" -var access_key_secret="<Your AccessKey secret>" -var security_token="<Your STS token>"

Environment variables

Store credentials in environment variables. Terraform reads these when no static configuration exists.

Parameters

Parameter

Description

Example value

ALICLOUD_ACCESS_KEY

The AccessKey ID.

abcd*******

ALICLOUD_SECRET_KEY

The AccessKey secret.

abcd*******

ALICLOUD_SECURITY_TOKEN (Optional)

The security token of a temporary STS credential.

abcd*******

Example configuration

Linux/macOS
Note

Environment variables set with export are valid only for the current shell session. For persistence, add the export command to .bash_profile or .zshrc.

# AccessKey ID
$ export ALICLOUD_ACCESS_KEY="<Your AccessKey ID>"
# AccessKey Secret
$ export ALICLOUD_SECRET_KEY="<Your AccessKey secret>"
# If you use an STS credential, configure security_token.
$ export ALICLOUD_SECURITY_TOKEN="<Your STS token>"
Windows
  1. Right-click This PC on your desktop and select Properties > Advanced system settings > Environment Variables.

  2. Under System variables or User variables, click New to create the following environment variables: ALICLOUD_ACCESS_KEY, ALICLOUD_SECRET_KEY, and ALICLOUD_SECURITY_TOKEN (optional).

After setting environment variables, declare only non-credential information in the provider block:

provider "alicloud" {
  region = "cn-hangzhou"
}

You can also set the region via the ALICLOUD_REGION environment variable. If no region is specified in the provider block or environment variable, cn-beijing is used by default.

Environment variables are more convenient and secure than static configuration.

Instance RAM role for ECS

When Terraform runs on an ECS instance, authenticate using an instance RAM role. The instance automatically obtains and refreshes temporary credentials, eliminating AccessKey configuration. Instance RAM roles.

Parameters

Parameter Name

Description

Example value

ecs_role_name

The name of the RAM role attached to the ECS instance.

terraform-ecs-role

Example configuration

  1. Prepare an ECS instance that has public network access.

  2. Create a RAM role and attach it to the ECS instance.

  3. In the provider block, set the ecs_role_name parameter to the name of the RAM role:

provider "alicloud" {
  ecs_role_name = "<Name of the RAM role attached to the ECS instance>"
}

You can also set this parameter using the ALICLOUD_ECS_ROLE_NAME environment variable.

Instance RAM role authentication is recommended when running Terraform on ECS.

OIDC role assumption

OIDC role assumption obtains temporary credentials by assuming a RAM role using an OIDC identity, rather than an AccessKey. Manage OIDC IdPs.

Parameters

Configure the following parameters in the assume_role_with_oidc block:

Parameter Name

Description

Example value

oidc_provider_arn (Required)

The ARN of the OIDC identity provider, in the format: acs:ram::<Alibaba Cloud account ID>:oidc-provider/<RAM Provider role name>. Also configurable via ALIBABA_CLOUD_OIDC_PROVIDER_ARN.

acs:ram::151192xxxxxx:oidc-provider/ackrole

oidc_token (Optional)

An OIDC token issued by an external IdP. Set either oidc_token or oidc_token_file. Also configurable via ALIBABA_CLOUD_OIDC_TOKEN.

eyJhbGciOiJSUzI1NiIsImtpZCI6IjEifQ.************

oidc_token_file (Optional)

The absolute path of the file that stores the OIDC token. Set either oidc_token or oidc_token_file. Also configurable via ALIBABA_CLOUD_OIDC_TOKEN_FILE.

/var/run/secrets/oidc/token

role_arn (Required)

The ARN of the RAM role to assume, in the format acs:ram::<Alibaba Cloud account ID>:role/<RAM role name>. Also configurable via ALIBABA_CLOUD_ROLE_ARN.

acs:ram::151192xxxxxx:role/k8srole

policy (Optional)

A RAM access policy. If set, the effective permissions are the intersection of this policy and the policies attached to role_arn. If not set, the role_arn role's permissions apply.

{"Version":"1","Statement":[{"Effect":"Allow","Action":"oss:ListBuckets","Resource":"acs:oss:oss-cn-hangzhou:*:*"}]}

role_session_name (Optional)

A custom name for the role session, typically set to the identity of the caller. Default: terraform. Can be set via ALIBABA_CLOUD_ROLE_SESSION_NAME.

username

session_expiration (Optional)

Validity period in seconds. Range: 900 to the role's MaxSessionDuration (default: 3600). Increase up to 43,200 seconds by updating the role information.

900

Example configuration

This method calls AssumeRoleWithOIDC - Obtain a temporary credential for a RAM role of an OIDC IdP for role-based SSO. Before configuring, prepare:

  1. Create an OIDC IdP, obtain an OIDC token from the external IdP, and record the OIDC IdP's ARN and the OIDC token.

  2. Create a RAM role with an identity provider as the trusted entity, grant the role the required permissions (such as AliyunOSSFullAccess), and record the ARN of the role.

  3. (Optional) Write a custom RAM policy for fine-grained access control. This policy intersects with the role's attached permissions.

Add the following to the provider block:

provider "alicloud" {
  assume_role_with_oidc {
    oidc_provider_arn  = "<ARN of the OIDC IdP>"
    oidc_token         = "<OIDC token issued by the external IdP>"
    role_arn           = "<ARN of the RAM role>"
    policy             = "<Content of the RAM access policy>"
    role_session_name  = "<Custom role session name>"
    session_expiration = <Maximum validity period of the temporary access credential>
  }
}

ACK supports RRSA (RAM Roles for Service Accounts) to automatically create OIDC IdPs and issue tokens. Use RRSA to authorize different pods to access different cloud services. After enabling RRSA, the ack-pod-identity-webhook component injects the ALIBABA_CLOUD_OIDC_PROVIDER_ARN, ALIBABA_CLOUD_OIDC_TOKEN_FILE, and ALIBABA_CLOUD_ROLE_ARN environment variables into pods. In an ACK pod, the configuration simplifies to:

provider "alicloud" {
  region = "cn-hangzhou"
  assume_role_with_oidc {
    policy             = <<EOF
    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": "oss:ListBuckets",
          "Resource": "acs:oss:oss-cn-hangzhou:*:*"
        }
      ]
    }
    EOF
    session_name = "terraform-assume-role-session"
    session_expiration = 1000
  }
}

Without a custom policy, grant permissions directly to the RAM role:

provider "alicloud" {
  region = "cn-hangzhou"
  assume_role_with_oidc {
    session_name = "terraform-assume-role-session"
    session_expiration = 1000
  }
}

Role assumption

Role assumption (AssumeRole) enables permission separation and cross-account access. An existing identity (RAM user or ECS instance role) assumes a RAM role to obtain temporary credentials.

Parameters

Supported parameters:

  1. access_key and secret_key: The AccessKey of the RAM user assuming the role. The user must have sts:AssumeRole permission. Can also be set via environment variables.

  2. assume_role: A configuration block with the following parameters:

Parameter

Description

Example value

role_arn (Required)

The ARN of the RAM role to assume, in the format acs:ram::1234567890123456:role/<RAM role name>.

acs:ram::151192xxxxxx:role/k8srole

policy (Optional)

A RAM access policy. If set, the effective permissions are the intersection of this policy and the policies attached to role_arn. If not set, the role_arn role's permissions apply.

{"Version":"1","Statement":[{"Effect":"Allow","Action":"oss:ListBuckets","Resource":"acs:oss:oss-cn-hangzhou:*:*"}]}

session_name (Optional)

The custom role session name, used to distinguish operators in audit logs. Default: terraform.

terraform

session_expiration (Optional)

Validity period in seconds. Range: 900 to the role's MaxSessionDuration (default: 3600). Increase up to 43,200 seconds by Update role information.

3600

external_Id (Optional)

An external ID for the role, used to prevent the confused deputy problem. Use an external ID to prevent the confused deputy problem.

ID

Example configuration

This method calls AssumeRole - Obtain a temporary credential for a RAM role. Before configuring, prepare:

  1. Create a RAM user and grant the AliyunSTSAssumeRoleAccess policy (or a custom policy that contains the sts:AssumeRole permission).

  2. Create an AccessKey for the RAM user.

  3. Create a RAM role whose trusted entity is an Alibaba Cloud account, grant it the required permissions (such as AliyunOSSFullAccess), and record its ARN.

  4. (Optional) Write a custom RAM policy for fine-grained access control.

Configure the provider block:

provider "alicloud" {
  # The AccessKey ID of the RAM user that calls AssumeRole.
  access_key = "<AccessKey ID of the RAM user>"
  # The AccessKey secret of the RAM user that calls AssumeRole.
  secret_key = "<AccessKey secret of the RAM user>"
  assume_role {
    role_arn           = "<ARN of a RAM role>"
    policy             = "<Content of a RAM access policy>"
    session_name       = "<Custom role session name>"
    session_expiration = <Maximum validity period of the temporary access credential>
  }
}

This example lists OSS buckets in cn-hangzhou using role assumption. The access_key and secret_key are set via environment variables:

provider "alicloud" {
  # For security, the access_key and secret_key here are set as environment variables.
  region = "cn-hangzhou"
  assume_role {
    role_arn           = "acs:ram::11827xxxxxx:role/tf-assume-role"
    policy             = <<EOF
    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": "oss:ListBuckets",
          "Resource": "acs:oss:oss-cn-hangzhou:*:*"
        }
      ]
    }
    EOF
    session_name       = "terraform-assume-role-session"
    session_expiration = 1000
  }
}
Note

Before running this example, ensure the tf-assume-role role has AliyunOSSFullAccess permission. Otherwise, OSS access fails.

On an ECS instance, use an instance RAM role instead of hard-coding credentials. Grant AliyunSTSAssumeRoleAccess to the instance's RAM role:

provider "alicloud" {
  ecs_role_name = "<Name of the RAM role attached to the ECS instance>"
  region        = "cn-hangzhou"
  assume_role {
    role_arn           = "acs:ram::11827xxxxxx:role/tf-assume-role"
    policy             = <<EOF
    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": "oss:ListBuckets",
          "Resource": "acs:oss:oss-cn-hangzhou:*:*"
        }
      ]
    }
    EOF
    session_name       = "terraform-assume-role-session"
    session_expiration = 1000
  }
}

URL credential

Retrieves credentials from a specified URI.

Parameters

Parameter

Description

Example value

credentials_url

A URI that returns a temporary credential. Must return HTTP 200 with valid credential content.

http://credentials.uri/

region

The default region and zone.

Some services do not support cross-region access. Set this to your resource region.

cn-hangzhou

Example configuration

provider "alicloud" {
  region = "cn-hangzhou"
  credentials_url= "http://credentials.uri/"
}

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_profile to reference another credential in config.json and 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.