useGrant

Alibaba Cloud Assume Role

Assume Alibaba Cloud RAM roles using OpenID Connect.

This guide shows how to authenticate with Alibaba Cloud using OIDC-based role SSO. Your applications get temporary security credentials without embedding long-term credentials.

The temporary credentials returned by Alibaba Cloud consist of an access key ID, an access key secret and a security token, which your applications use to access Alibaba Cloud resources.

Prerequisites

  • A useGrant provider with a client
  • An Alibaba Cloud account
  • The Alibaba Cloud CLI installed on your system

Before you start, plan your security strategy so that credentials are only issued in a predictable way. Define at least one condition on your role's trust policy, so untrusted identities can't request credentials for your cloud resources.

Step 1: Add an OIDC identity provider in Alibaba Cloud

  1. Sign in to the Alibaba Cloud console and open the RAM service.
  2. In the sidebar, select Integrations → SSO.
  3. Under Role-based SSO, select the OIDC tab and click Create IdP.
  4. Fill in the details and click Create.

For the issuer URL, use your provider's issuer URL, shown on the provider's domain settings page in useGrant, or your custom domain.

For the client ID (audience), use the audience you set when creating the client in useGrant, for example ram.aliyuncs.com.

See the Alibaba Cloud documentation for more details and options.

Step 2: Create a role in Alibaba Cloud

  1. In the RAM console, select Identities → Roles in the sidebar and click Create Role.
  2. Select IdP as the trusted entity type and click Next.
  3. Enter the role name and an optional note.
  4. Select OIDC as the IdP type.
  5. Select the identity provider from the previous step and set the conditions:
    • Audience: the audience you set on the identity provider in step 1.
    • Subject: the subject shown on your client in useGrant.
  6. Click Create.

See the Alibaba Cloud documentation for more details and options.

Step 3: Assume the role with the Alibaba Cloud CLI

Alibaba Cloud expects the token audience to be an array and the token type header to be JWT instead of at+jwt. Set the audienceAsArray and useJwtType options when you create the access token, so the token is compatible with Alibaba Cloud.

Run the following command to assume the role:

aliyun sts AssumeRoleWithOIDC \
  --region cn-beijing \
  --OIDCProviderArn <OIDC_PROVIDER_ARN> \
  --RoleArn <ROLE_ARN> \
  --OIDCToken <USEGRANT_ACCESS_TOKEN> \
  --RoleSessionName <SESSION_NAME>

Alibaba Cloud returns temporary credentials with an AccessKeyId, AccessKeySecret and SecurityToken. What you can access with them depends on the policies attached to the role. For example:

{
  "RequestId": "3D57EAD2-8723-1F26-B69C-F8707D8B565D",
  "OIDCTokenInfo": {
    "Subject": "KryrkIdjylZb7agUgCEf****",
    "Issuer": "https://auth.example.com/acme-prod",
    "ClientIds": "ram.aliyuncs.com",
    "ExpirationTime": "2026-10-20T04:27:09Z",
    "IssuanceTime": "2026-10-20T03:27:09Z",
    "VerificationInfo": "Success"
  },
  "AssumedRoleUser": {
    "AssumedRoleId": "33157794895460****",
    "Arn": "acs:ram::113511544585****:role/oidcrole/SessionName"
  },
  "Credentials": {
    "SecurityToken": "CAIShwJ1q6Ft5B2yfSjIr5bSEsj4g7BihPWGWHz****",
    "Expiration": "2026-10-20T04:27:09Z",
    "AccessKeySecret": "CVwjCkNzTMupZ8NbTCxCBRq3K16jtcWFTJAyBEv2****",
    "AccessKeyId": "STS.NUgYrLnoC37mZZCNnAbez****"
  }
}

You can also try the API interactively in the Alibaba Cloud OpenAPI Explorer.

By default, the credentials returned by AssumeRoleWithOIDC are valid for one hour. Use the optional DurationSeconds parameter to change this, from 900 seconds (15 minutes) up to the role's maximum session duration, which can be set between 1 and 12 hours.

Learn more about the AssumeRoleWithOIDC API in the Alibaba Cloud documentation.

Using Terraform

Instead of creating the identity provider and role manually, you can create both with Terraform:

data "alicloud_account" "current" {}

resource "alicloud_ims_oidc_provider" "idp" {
  issuer_url          = "<ISSUER_URL>"
  issuance_limit_time = "12"
  oidc_provider_name  = "<PROVIDER_NAME>"
  client_ids          = ["<AUDIENCE>"]
  fingerprints        = ["<FINGERPRINT>"]
}

resource "alicloud_ram_role" "role" {
  name = "OIDCRole"

  document = jsonencode({
    Version   = "1"
    Statement = [
      {
        Action    = "sts:AssumeRole"
        Condition = {
          StringEquals = {
            "oidc:aud" = ["<AUDIENCE>"]
            "oidc:iss" = "<ISSUER_URL>"
            "oidc:sub" = "<CLIENT_SUBJECT>"
          }
        }
        Effect    = "Allow"
        Principal = {
          Federated = [
            "acs:ram::${data.alicloud_account.current.id}:oidc-provider/${alicloud_ims_oidc_provider.idp.id}"
          ]
        }
      }
    ]
  })
}

resource "alicloud_ram_role_policy_attachment" "attach" {
  policy_name = "ReadOnlyAccess" # replace with the policy you want to attach
  policy_type = "System"
  role_name   = alicloud_ram_role.role.name
}

ROS templates in JSON or YAML can't create OIDC providers. To use ROS, embed the Terraform template with the ALIYUN::ROS::Stack resource inside your ROS template.

Replace the placeholders with your own values:

  • PROVIDER_NAME is the name of the identity provider in Alibaba Cloud.
  • AUDIENCE must match the audience you set on the client in useGrant, for example sts.aliyuncs.com or ram.aliyuncs.com.
  • ISSUER_URL is your provider's issuer URL or custom domain.
  • CLIENT_SUBJECT is the subject generated when you created the client.
  • FINGERPRINT is the certificate fingerprint of your issuer's domain, shown on the provider's domain settings page.

On this page