A developer is creating an IAM role for an AWS Lambda function. The developer configures the permissions policy correctly but receives an authorization error stating that the function cannot assume the role. The developer inspects the role's trust policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
Which modification must the developer make to the trust policy to allow the Lambda function to assume the role?
- Change the Service principal in the trust policy from "ec2.amazonaws.com" to "lambda.amazonaws.com".Answer
- BAttach an IAM permissions policy to the role that grants the "sts:AssumeRole" action.
- CConfigure the AWS SDK client inside the Lambda function using hardcoded IAM user access keys.
- DChange the Action in the trust policy statement to "sts:AssumeRoleWithWebIdentity".
Answer
Change the Service principal in the trust policy from "ec2.amazonaws.com" to "lambda.amazonaws.com".
The trust policy determines which principals can assume the role. Because the current trust policy specifies the Amazon EC2 service principal ("ec2.amazonaws.com"), only EC2 instances can assume the role. To allow AWS Lambda to assume the role, the principal must be updated to the Lambda service principal ("lambda.amazonaws.com").
Step-by-Step Solution
Key Concept
IAM trust policies define which principals (users, roles, accounts, or services) are authorized to assume an IAM role.