Unable to assume role in lambda code
This is a Bug Report
Description
My lambda function tries to assume a role using aws sdk like this:
const sts = new aws.STS({ region: process.env.REGION });
const roleName = `${this.env.SERVICE_NAME}_ecr_role`;
sts.assumeRole({
RoleSessionName: roleName,
RoleArn: `arn:aws:iam::${this.env.ACCOUNT_ID}:role/${roleName}`,
Policy: JSON.stringify({
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Action: [
'ecr:GetDownloadUrlForLayer',
'ecr:PutImage',
'ecr:InitiateLayerUpload',
'ecr:UploadLayerPart',
'ecr:CompleteLayerUpload',
'ecr:GetAuthorizationToken',
'ecr:BatchCheckLayerAvailability',
'cloudwatchlogs:*',
],
Resource: `arn:aws:ecr:${this.env.REGION}:${this.env.ACCOUNT_ID}:repository/${this.env.SERVICE_NAME}/test_repository`,
},
],
}),
}).promise()
But it fails on
AccessDenied: Not authorized to perform sts:AssumeRole
at Request.extractError (/var/task/node_modules/aws-sdk/lib/protocol/query.js:40:29)
at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:668:14)
at Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /var/task/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:670:12)
at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:115:18)
at Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:668:14)
at Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /var/task/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:38:9)
Even when my lambda role has configured policy to allow sts:AssumeRole on * (here's the whole role definition: https://github.com/keboola/developer-portal/blob/master/serverless.yml#L362). Is it necessary to set something more then what I have or is it some kind of bug? Thanks for help.
Additional Data
- Serverless Framework Version you're using: 1.2
- Operating System: macOS 10.12
The role you are trying to assume needs to have a trust policy that allows the role with which your lambda executes to assume it.
Find the role in IAM, click on it, and click on trust relationships/edit trust relationships: the policy needs to include something like
"Principal": {
"AWS": [
"arn:aws:iam::AWS_ACCT_ID:role/ROLENAME", <-- to allow a role access
"arn:aws:iam::AWS_ACCT_ID:role:user/USERNAME" <--- if you want an IAM user to access
]
}
Read more here: http://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Principal
I see, I thought that trust policy on lambda.amazonaws.com service is sufficient. So I added the role lambda uses (https://github.com/keboola/developer-portal/blob/master/serverless.yml#L362) but it still doesn't work. ;o/
Can you perhaps show what the generated trust policy in IAM looks like. I don't use serverless yet.
Sure, this is the role that I try to assume:
"developerPortalEcrRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"RoleName": "jakub-dev-portal-ecr-role",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
],
"AWS": [
"arn:aws:iam::061240556736:role/jakub-dev-portal-lambda-role"
]
},
"Action": "sts:AssumeRole"
}
]
},
"Policies": [
{
"PolicyName": "jakub-dev-portal-ecr-policy",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecr:*"
],
"Resource": "*"
}
]
}
}
]
}
}
and this is lambda's role (from which I try to assume the role above):
"developerPortalLambdaRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"RoleName": "jakub-dev-portal-lambda-role",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
},
"Policies": [
{
"PolicyName": "jakub-dev-portal-lambda-policy",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:eu-west-1:061240556736:log-group:/aws/lambda/*:*:*"
},
{
"Effect": "Allow",
"Action": [
"s3:*"
],
"Resource": [
"arn:aws:s3:::jakub-dev-portal-resources/*"
]
},
{
"Effect": "Allow",
"Action": [
"cognito-identity:*",
"cognito-idp:*"
],
"Resource": [
"arn:aws:cognito-idp:eu-west-1:061240556736:userpool/eu-west-1_wO6iyVipc"
]
},
{
"Effect": "Allow",
"Action": [
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DetachNetworkInterface",
"ec2:DeleteNetworkInterface",
"ses:SendEmail",
"iam:CreateRole",
"iam:PutRolePolicy",
"ecr:*",
"sts:AssumeRole"
],
"Resource": "*"
}
]
}
}
]
}
}
I have read through and what you posted above should work.
What exactly is the error when you test your lambda function now? The same error or a different error.
Before I fixed my own lambda and role policies, my error message displayed the arn of the active role and also that of the role I was trying to assume. It helped me debug the issue.
Thanks for your check. The error is still the same: AccessDenied: Not authorized to perform sts:AssumeRole.
I'm having the same issue
Thanks for commenting @Shion093 👍
Could you please provide some more context on your service? How does your serverless.yml file look like? How does the code look like where you want to assume the role?
Thanks in advance!
Shouldn't the iamRoleStatements in serverless.yml be tweaked for the lambda function to work properly? Serverless documentation
Hi @pmuens , it was an error on my part, was giving the incorrect name on the roleArn, thanks for the reply.
Hi @pmuens , it was an error on my part, was giving the incorrect name on the roleArn, thanks for the reply.
@Shion093 thanks for getting back 👍! Glad to hear that you were able to resolve the error in the meantime.
Is there an example / tutorial to demonstrate how the lambda function can assume role for a federated user (cognito as idp)?
@neowulf33 There seems not to be those example/tutorial. You would better to read AWS documentation instead of Serverless one something like this http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_control-access_getfederationtoken.html
For me worked something like this: arn:aws:sts::USER_ID:assumed-role/ROLE_NAME/LAMBDA_FUNCTION_NAME
ex. arn:aws:sts::458485127617:assumed-role/testService-Dev-us-east-2-lambdaRole/testService-Dev-GetUserInfo
@Dawidpol Thank you so much, this also works for me!
Closing since this has can be solved via https://github.com/serverless/serverless/issues/3040#issuecomment-357582699.