Michel
Michel

Reputation: 11749

AWS Lambda functions issue

I have recently started trying to use AWS Lambda functions. Here is what I get in the Execution results window of the Lambda console, when clicking the Test button.

Response:
{
  "statusCode": 500,
  "body": "{\"message\":\"Missing credentials in config\",
  \"code\":\"CredentialsError\",\"time\":\"2019-06-06T07:11:53.538Z\",
  \"originalError\":{\"message\":\"No credentials to load\",
  \"code\":\"CredentialsError\",\"time\":\"2019-06-06T07:11:53.538Z\"}}"
}

And here is the Lambda function source code:

var AWS = require('aws-sdk/dist/aws-sdk-react-native');

AWS.config.update({
    region:'ap-northeast-1'
});

exports.handler = async (event,context) => {
    var params = {
        UserPoolId: 'ap-northeast-1MY-POOL-ID',
        AttributesToGet: [
            'email'
        ],
        Limit: '2',
    };

    var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();

    try {
      const data = await cognitoidentityserviceprovider.listUsers(params).promise()
      const response = {
        inBound: event.sub,
        statusCode: 200,
        body: JSON.stringify(data)
      }
      return response;
    } catch (err) {
        const response = {
            inBound: event.sub,
            statusCode: 500,
            body: JSON.stringify(err)
        };
        return response;
    }
};

Since I have set up the roles and policy, I am not sure what kind of credentials and which config the error message is referring to.

Upvotes: 0

Views: 201

Answers (1)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

First, replace the existing import statement with follows.

var AWS = require('aws-sdk');

Second, allow the cognito user list permission to lambda role as follow.

{
  "Effect": "Allow",
    "Action": [
        "cognito-idp:ListUsers"
    ],
    "Resource": [
        "arn:aws:cognito-idp:ap-northeast-1:AWS_ACCOUNT_NO:userpool:*"
    ]
}

Note: Replace AWS_ACCOUNT_NO with your actual aws account number.

Upvotes: 1

Related Questions