serverless icon indicating copy to clipboard operation
serverless copied to clipboard

Unable to assume role in lambda code

Open JakubMatejka opened this issue 9 years ago • 16 comments

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

JakubMatejka avatar Jan 02 '17 14:01 JakubMatejka

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

dfasoro avatar Jan 04 '17 16:01 dfasoro

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/

JakubMatejka avatar Jan 04 '17 16:01 JakubMatejka

Can you perhaps show what the generated trust policy in IAM looks like. I don't use serverless yet.

dfasoro avatar Jan 06 '17 15:01 dfasoro

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": "*"
                }
              ]
            }
          }
        ]
      }
    }

JakubMatejka avatar Jan 06 '17 16:01 JakubMatejka

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.

dfasoro avatar Jan 06 '17 16:01 dfasoro

Thanks for your check. The error is still the same: AccessDenied: Not authorized to perform sts:AssumeRole.

JakubMatejka avatar Jan 06 '17 18:01 JakubMatejka

I'm having the same issue

Shion093 avatar Aug 11 '17 01:08 Shion093

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!

pmuens avatar Aug 11 '17 05:08 pmuens

Shouldn't the iamRoleStatements in serverless.yml be tweaked for the lambda function to work properly? Serverless documentation

neowulf avatar Aug 11 '17 17:08 neowulf

Hi @pmuens , it was an error on my part, was giving the incorrect name on the roleArn, thanks for the reply.

Shion093 avatar Aug 12 '17 04:08 Shion093

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.

pmuens avatar Aug 12 '17 08:08 pmuens

Is there an example / tutorial to demonstrate how the lambda function can assume role for a federated user (cognito as idp)?

neowulf avatar Aug 14 '17 23:08 neowulf

@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

horike37 avatar Aug 15 '17 01:08 horike37

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 avatar Jan 15 '18 04:01 Dawidpol

@Dawidpol Thank you so much, this also works for me!

PinkyJie avatar Oct 23 '18 12:10 PinkyJie

Closing since this has can be solved via https://github.com/serverless/serverless/issues/3040#issuecomment-357582699.

pmuens avatar Jan 15 '19 12:01 pmuens