Keywords: AWS STS | AssumeRole | IAM Role Trust Relationship
Abstract: This technical paper provides an in-depth analysis of common authorization errors in AWS STS AssumeRole operations, with a focus on the critical role of IAM role trust relationships. Through detailed configuration examples and code demonstrations, it explains how to properly set up role trust policies to ensure successful role assumption by IAM users. The paper also examines differences between policy simulator testing and actual API calls, offering complete troubleshooting guidance.
Problem Background and Error Analysis
When using the AWS STS AssumeRole operation, developers frequently encounter "user is not authorized to perform sts:AssumeRole" errors. This typically occurs when an IAM user possesses sts:* permissions but the target role lacks the necessary trust relationship configuration.
Core Function of Trust Relationships
IAM role trust relationships define which principals (such as IAM users, AWS services, or other AWS accounts) can assume the role. Even if the caller has sts:AssumeRole permissions, the STS service will reject the request if the target role's trust policy does not include the caller.
Complete Configuration Process
First, ensure the IAM user has appropriate STS permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["sts:AssumeRole"],
"Resource": "*"
}
]
}
Second, configure the trust relationship for the target role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::ACCOUNT-ID:user/USERNAME"
},
"Action": "sts:AssumeRole"
}
]
}
Node.js Implementation Example
Complete code for calling AssumeRole using AWS SDK in Node.js:
const AWS = require('aws-sdk');
// Configure AWS credentials
AWS.config.update({
accessKeyId: 'ACCESS_KEY',
secretAccessKey: 'SECRET_KEY',
region: 'us-east-1'
});
const assumeRole = async () => {
const sts = new AWS.STS();
const params = {
RoleArn: 'arn:aws:iam::ACCOUNT-ID:role/ROLE-NAME',
RoleSessionName: 'my-session',
DurationSeconds: 3600
};
try {
const data = await sts.assumeRole(params).promise();
console.log('Temporary credentials obtained successfully:', data.Credentials);
return data.Credentials;
} catch (error) {
console.error('AssumeRole failed:', error.message);
throw error;
}
};
// Execute AssumeRole operation
assumeRole();
Troubleshooting Key Points
When encountering authorization errors, check these critical aspects:
- Verify IAM user's STS permission scope includes the target role ARN
- Confirm the role trust relationship explicitly lists the caller
- Check role ARN correctness and account ID matching
- Ensure no other policies (like service control policies) block the operation
Best Practice Recommendations
In production environments, adopt the principle of least privilege:
- Create dedicated roles for different use cases
- Explicitly specify allowed principals in trust policies
- Use condition statements to further restrict access conditions
- Regularly audit and update permission configurations