AssumeRole
Obtains a temporary identity credential (STS token) to assume a RAM role.
Operation description
Common scenarios
-
Temporary access authorization: Typically, invoke API operations from the server side to prevent access key leaks. However, in some file upload scenarios, it is better to use direct client-side uploads to avoid the overhead of server-side forwarding. In this case, the server can issue a temporary security token (STS token), and the client can use the STS token to directly upload resources. For more information, see Use temporary security tokens for mobile applications to access Alibaba Cloud.
-
Cross-account access: If you have multiple Alibaba Cloud accounts, such as Account A and Account B, and you want Account B to access specific resources of Account A. In this case, you can create a RAM role in Account A with Account B as the trusted entity, and grant authorization to a Resource Access Management (RAM) user or RAM role in Account B to assume the role. Then, the RAM user or RAM role in Account B can access the specific resources of Account A by assuming the role. For more information, see Access resources across Alibaba Cloud accounts.
Before you begin
You cannot invoke this operation by using an Alibaba Cloud account. You can only invoke this operation as a RAM user or a RAM role. Make sure that the caller (the RAM user or RAM role) is granted the authorization for STS role assumption (AliyunSTSAssumeRoleAccess).
Otherwise, the following error is returned:
You are not authorized to do this action. You should be authorized by RAM.
Causes and solutions:
The caller lacks the access policy that allows STS role assumption: Attach the system policy (AliyunSTSAssumeRoleAccess) or a custom policy to the caller. For more information, see Can I specify the RAM role that a RAM user can assume? and Grant permissions to a RAM user.
The trust policy of the RAM role does not include the caller, which means the RAM role does not allow the caller to assume it: Add a trust policy that allows the caller to assume the RAM role. For more information, see Modify the trust policy of a RAM role.
Best practices
An STS token is valid for a specified period after it is issued. We recommend that you set a reasonable token validity period and reuse the token within the validity period. This prevents the rate limit on STS token issuance from affecting your business when the request rate increases. For more information about the rate limit, see Is there a limit on how many times you can invoke the STS service?. You can set the token validity period by using the DurationSeconds request parameter.
In scenarios such as uploading or downloading OSS files from mobile clients, the access volume is large. Even if you reuse STS tokens, the throttling requirements may not be met. To prevent STS throttling from becoming a bottleneck for OSS access, try the OSS solution of including a signature in a URL. For more information, see Include a signature in a URL and Server-side signing and direct upload.
Try it now
Test
RAM authorization
|
Action |
Access level |
Resource type |
Condition key |
Dependent action |
|
sts:AssumeRole |
get |
*Role
|
|
|
Request parameters
|
Parameter |
Type |
Required |
Description |
Example |
| DurationSeconds |
integer |
No |
The validity period of the token. Unit: seconds. The minimum validity period is 900 seconds, and the maximum validity period is the You can set the |
3600 |
| Policy |
string |
No |
The additional access policy attached to the STS token to further restrict the permissions of the STS token. Details:
The value must be 1 to 2,048 characters in length. For more information about access policy elements and examples, see Basic elements of a access policy and Access policy examples. |
{"Statement": [{"Action": ["*"],"Effect": "Allow","Resource": ["*"]}],"Version":"1"} |
| RoleArn |
string |
Yes |
The ARN of the RAM role to be assumed. The trusted entity of the RAM role is an Alibaba Cloud account. For more information, see Create a RAM role for a trusted Alibaba Cloud account or CreateRole. Format: You can view the ARN of the RAM role in the RAM console or by calling an API operation. Details:
|
acs:ram::123456789012****:role/adminrole |
| RoleSessionName |
string |
Yes |
The name of the role session. This parameter is user-defined. Typically, set this parameter to the identity of the user who calls the API operation, such as the username. In ActionTrail logs, even if the operations are performed by the same RAM role, you can identify the actual operator based on the The value must be 2 to 64 characters in length and can contain letters, digits, and the following special characters: |
alice |
| ExternalId |
string |
No |
The external ID of the role. This parameter is provided by an external party to identify the role. The main purpose is to prevent the confused deputy problem. For more information, see Use ExternalId to prevent the confused deputy problem. The value must be 2 to 1,224 characters in length and can contain letters, digits, and the following special characters: |
abcd1234 |
| SourceIdentity |
string |
No |
The source identity. When you assume a role, you can specify the source identity of the caller as the initial identity of the session. The source identity persists throughout the chained sessions of role assumption and cannot be changed. This ensures the traceability and security of operations. The value of SourceIdentity must be 2 to 64 characters in length and can contain letters, digits, and the following special characters: |
Alice |
For more information about common request parameters, see Common parameters.
Response elements
|
Element |
Type |
Description |
Example |
|
object |
The response parameters. |
||
| RequestId |
string |
The request ID. |
6894B13B-6D71-4EF5-88FA-F32781734A7F |
| AssumedRoleUser |
object |
The temporary identity used for role assumption. |
|
| AssumedRoleId |
string |
The ID of the temporary identity. |
34458433936495****:alice |
| Arn |
string |
The ARN of the temporary identity. |
acs:ram::123456789012****:role/adminrole/alice |
| Credentials |
object |
The access credential. |
|
| SecurityToken |
string |
The security token. Note
The length of the security token is not fixed. Do not limit the maximum length of the security token. |
******** |
| Expiration |
string |
The time when the token expires. The time is displayed in UTC. |
2015-04-09T11:52:19Z |
| AccessKeySecret |
string |
The AccessKey secret. |
wyLTSmsyPGP1ohvvw8xYgB29dlGI8KMiH2pK**** |
| AccessKeyId |
string |
The AccessKey ID. |
STS.L4aBSCSJVMuKg5U1**** |
| SourceIdentity |
string |
The source identity. When you assume a role, you can specify the source identity of the caller as the initial identity of the session. The source identity persists throughout the chained sessions of role assumption and cannot be changed. This ensures the traceability and security of operations. If the source identity is not specified, this field is not returned. |
Alice |
Examples
Success response
JSON format
{
"RequestId": "6894B13B-6D71-4EF5-88FA-F32781734A7F",
"AssumedRoleUser": {
"AssumedRoleId": "34458433936495****:alice",
"Arn": "acs:ram::123456789012****:role/adminrole/alice"
},
"Credentials": {
"SecurityToken": "********",
"Expiration": "2015-04-09T11:52:19Z",
"AccessKeySecret": "wyLTSmsyPGP1ohvvw8xYgB29dlGI8KMiH2pK****",
"AccessKeyId": "STS.L4aBSCSJVMuKg5U1****"
},
"SourceIdentity": "Alice"
}
Error codes
|
HTTP status code |
Error code |
Error message |
Description |
|---|---|---|---|
| 400 | InvalidParameter.DurationSeconds | The Min/Max value of DurationSeconds is 15min/1hr. | DurationSeconds parameter is illegal, DurationSeconds minimum value is 15 minutes, maximum value is 1 hour |
| 400 | InvalidParameter.ExternalId | The parameter ExternalId is wrongly formed. | Parameters |
| 400 | InvalidParameter.RoleArn | The parameter RoleArn is wrongly formed. | Parameters |
| 400 | InvalidParameter.RoleSessionName | The parameter RoleSessionName is wrongly formed. | The format of the RoleSessionName parameter is invalid. |
| 400 | InvalidParameter.SerialNumber | The parameter SerialNumber is wrongly formed. | |
| 400 | InvalidParameter.TokenCode | The parameter TokenCode is wrongly formed. | |
| 400 | InvalidParameter.PolicyGrammar | The parameter Policy has not passed grammar check. | Parameter Policy syntax format check failed |
| 400 | InvalidParameter.PolicySize | The size of Policy must be smaller than 2048 bytes. | The policy parameter is invalid. The policy length must be less than 2048 bytes. |
| 400 | InvalidParameter.ContentType | The ContentType request header must be either "application/json" or "application/x-www-form-urlencoded". | |
| 500 | InternalError | STS Server Internal Error happened, please send the RequestId to us. | STS server internal exception |
| 403 | NoPermission | You are not authorized to do this action. You should be authorized by RAM. | |
| 403 | AuthenticationFail.ApiUsername | The specified api username is not legal. | |
| 403 | AuthenticationFail.ApiPassword | The specified api password is not legal. | |
| 404 | EntityNotExist.Role | The specified Role not exists . |
See Error Codes for a complete list.
Release notes
See Release Notes for a complete list.