AWS AssumeRole - User Is Not Authorized to Perform: sts:AssumeRole on Resource
You are encountering a classic IAM permission boundary issue. The error message User arn:aws:iam::123456789012:user/TVMUser is not authorized to perform: sts:AssumeRole on resource is explicit about the root cause: the user attempting the action lacks the specific trust relationship or inline policy required to assume the target role.
In your PHP implementation using the AWS SDK, you are invoking the sts:AssumeRole API. This action does not merely grant permissions; it establishes a temporary security context. For this transition to occur, two distinct checks happen simultaneously, and one of them is failing in your current setup.
The Missing Trust Relationship
The primary failure point lies in the configuration of createPic. You have applied an inline policy to the role, but you have not configured the Trust Policy (also known as the Permission Policy attached to the Role's identity).
In AWS IAM, a Role is different from a User. A User has permissions; a Role grants trust. The Trust Policy defines exactly who can assume the role and via what mechanism. Currently, your createPic role likely relies on its default or missing trust settings. Even if you attach an inline policy to the role saying "Allow sts:AssumeRole", that statement is technically ignored regarding the identity of the caller unless the Trust Policy explicitly permits it.
To fix this, you must edit the trust relationships for arn:aws:iam::123456789012:role/createPic. The condition block in your PHP code uses a custom policy (Policy parameter), which implies an intent to use the STS API with specific constraints. However, the fundamental prerequisite is that the Trust Policy must allow the principal arn:aws:iam::123456789012:user/TVMUser.
The correct configuration for the role's trust policy should look like this:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456789012:user/TVMUser"
},
"Action": "sts:AssumeRole"
}
]
}
Without this entry in the role's trust policy, AWS rejects the request immediately, regardless of what permissions are attached to the user or the role.
Inline Policies vs. Trust Policies
A common misconception is that adding an sts:AssumeRole statement to a Role's inline policy (the policy you pasted into the console) allows assumption. This is incorrect. Inline policies on roles define what the role can do after it has been assumed (e.g., "Allow EC2:RunInstances"). They do not define who can assume the role.
The Trust Policy acts as a gatekeeper for identity. The inline policy acts as a permission grantor for actions. Your current setup has a gatekeeper that is closed, rendering the permissions inside the room inaccessible.
The PHP Implementation Trade-Offs
Looking at your PHP code using StsClient::factory, you are passing hardcoded credentials ('key' and 'secret'). While this works for quick scripts, it introduces significant security risks in production environments. Hardcoded credentials should generally be avoided; instead, use the Default AWS Credentials chain or inject them via environment variables to allow for seamless rotation.
Furthermore, the Policy parameter you are passing in the assumeRole call is a specific STS feature. It allows you to attach an additional inline policy to the temporary credentials generated during the session. However, this does not override the Trust Policy requirement. If the user cannot assume the role based on the Trust Policy, the custom policy logic never executes.
There is also a discrepancy in your source material regarding Account IDs. The error message references arn:aws:iam::123456789012:user/TVMUser and arn:aws:iam::123456789012:role/createPic, but the inline policy provided in the description uses account ID 791758789361. Ensure that all ARNs (Role ARN, User ARN, and Resource ARNs) reference the same AWS Account ID. Mixing accounts without configuring cross-account trust policies will result in immediate denial.
S3 Access Considerations
You mentioned wanting to create objects for an AWS bucket. Once you successfully assume the role, the temporary credentials returned will include a session token. These credentials inherit permissions from the createPic role's attached policies and any permissions defined in the STS assumption policy.
Ensure that the createPic role has an attached policy granting s3:PutObject, s3:GetObject, or similar actions on the specific bucket ARN (arn:aws:s3:::mybucket). The Trust Policy only controls access to the role itself; it does not grant access to S3 resources. If the role assumes successfully but cannot write to the bucket, you will then encounter an Access Denied error regarding S3, distinct from the current sts:AssumeRole error.
Conclusion
The resolution is straightforward: navigate to the IAM console, select the createPic role, go to the Trust relationships tab, and edit the policy. Add a statement that explicitly allows arn:aws:iam::123456789012:user/TVMUser to perform sts:AssumeRole. Once this identity trust is established, your PHP application will be able to retrieve the temporary credentials and proceed with S3 operations as intended.