AWS Lambda functions issue

故事扮演 提交于 2019-12-25 01:48:54

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/56472969/aws-lambda-functions-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!