A developer is implementing a serverless application where an AWS Lambda function in Account A () needs to access a DynamoDB table in Account B (). The developer creates an IAM role named `CrossAccountDynamoDBRole` in Account B that has the required permissions to access the DynamoDB table. The Lambda function is configured with an execution role named `arn:aws:iam::111122223333:role/LambdaExecutionRole` and runs code that calls the `AssumeRole` API of AWS Security Token Service (STS) to assume `CrossAccountDynamoDBRole`.
However, when the Lambda function runs, the `AssumeRole` call fails with an `AccessDenied` error. The developer reviews the trust policy of `CrossAccountDynamoDBRole` in Account B:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
Which modification to the trust policy of `CrossAccountDynamoDBRole` in Account B will resolve this error?
- AKeep the service principal as `lambda.amazonaws.com` but add a condition to restrict the `aws:SourceArn` to the Lambda function's ARN:
"Condition": {
"ArnEquals": {
"aws:SourceArn": "arn:aws:lambda:us-east-1:111122223333:function:my-function"
}
} - Update the `Principal` block to reference the ARN of the Lambda function's IAM execution role in Account A:
"Principal": {
"AWS": "arn:aws:iam::111122223333:role/LambdaExecutionRole"
}
Answer - CUpdate the `Principal` block to trust Account A's root user, but change the policy's `Action` from `sts:AssumeRole` to `sts:AssumeRoleWithWebIdentity`:
"Principal": {
"AWS": "arn:aws:iam::111122223333:root"
},
"Action": "sts:AssumeRoleWithWebIdentity" - DChange the `Principal` service to the DynamoDB service principal so that the table can receive requests directly from the role:
"Principal": {
"Service": "dynamodb.amazonaws.com"
}