Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda can't call Cognito Identity - IAM Role

I've got a bit of javascript which runs on my local machine but doesn't work from within the Lambda.

It timeouts when calling cognitoidentity.getOpenIdTokenForDeveloperIdentity

{
  "errorMessage": "2016-03-17T16:50:25.181Z 4fa3fa5a-ec60-11e5-8316-415fa39313da Task timed out after 15.00 seconds"
}

On local it works fine (calling into AWS production services) so it must be the policy I have attached to the Lambda.

Here are the policies I have:

AmazonCognitoDeveloperAuthenticatedIdentities

AWSLambdaVPCAccessExecutionRole

And this is the custom one I also have:

 {
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "mobileanalytics:PutEvents",
                "cognito-sync:*"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "lambda:InvokeFunction"
            ],
            "Resource": [
                "arn:aws:lambda:eu-west-1:myaccountid:function:users_login"
            ]
        }
    ]
}

The Lambda ARN was copied directly from that Lambda screen. Any ideas of what's missing?

like image 843
Daniel Wardin Avatar asked Mar 17 '16 16:03

Daniel Wardin


People also ask

How do you connect Lambda to Cognito?

To add a user pool Lambda trigger with the consoleGo to the Amazon Cognito console , and then choose User Pools. Choose an existing user pool from the list, or create a user pool. Choose the User pool properties tab and locate Lambda triggers. Choose Add a Lambda trigger.

How do you give IAM role to Lambda function?

Attach the IAM policy to an IAM roleNavigate to the IAM console and choose Roles in the navigation pane. Choose Create role. Choose AWS service and then choose Lambda. Choose Next: Permissions.

How do I allow API users to run AWS Lambda with their Amazon Cognito permissions?

To allow users to run Lambda with their Amazon Cognito permissions, follow these steps: Use the API Gateway console to establish your Amazon Cognito user pool as an authorizer. Then, assign the Amazon Cognito user pool as the authorizer for the method of your API.


1 Answers

it must be the policy I have attached

No, if that were the case you would be getting a permission denied error, not a timeout.

It looks like your Lambda function has VPC access. You need to configure a NAT gateway for your VPC in order for the Lambda function to have access to anything outside the VPC, including AWS services like Cognito.

like image 63
Mark B Avatar answered Sep 24 '22 21:09

Mark B