UnrecognizedClientException","errorMessage":"在本地测试 lambda 函数时,请求中包含的安全令牌无效

UnrecognizedClientException","errorMessage":"The security token included in the request is invalid when testing lambda function locally

我正在尝试使用 aws sam 在本地调用我的 lambda 函数进行测试。该函数从 dynamodb table 中读取一个项目。我已经启动了一个本地 dynamodb 容器,其中创建了所需的 table。

运行 以下命令创建本地 dynamodb 容器。

  1. docker 网络创建 lambda-local
  2. docker 运行 —-network=lambda-local —-name users -d -p 8000:8000 amazon/dynamodb-local
  3. aws dynamodb create-table --table-name employees --attribute-definitions AttributeName=name,AttributeType=S --key-schema AttributeName=name,KeyType=HASH -- 键类型预配吞吐量 ReadCapacityUnits=5,WriteCapacityUnits=5 --endpoint-url=http://127.0.0.1:8000

然后使用下面的命令我能够验证就本地 dynamodb 而言一切正常。

  1. aws dynamodb list-tables --endpoint-url http://localhsot:8000

但是,当我尝试 运行 下面的命令时,出现错误。

sam local invoke <lambdaFunctionName> --docker-network lambda-local

我得到的错误-

START RequestId: 043b493d-8457-43f1-8eeb-dc641ac3816f Version: $LATEST
2021-10-27T08:17:00.778Z        043b493d-8457-43f1-8eeb-dc641ac3816f    ERROR   Invoke Error    
{"errorType":"UnrecognizedClientException","errorMessage":"The security token included in the request is invalid",
"code":"UnrecognizedClientException","message":"The security token included in the request is invalid","time":"2021-10-27T08:17:00.775Z","requestId":"NG2U0AEVI320VL5PLPTK8H3G63VV4KQNSO5AEMVJF66Q9ASUAAJG","statusCode":400,"retryable":false,"retryDelay":46.55385931289337,"stack":["UnrecognizedClientException: The security token included in the request is invalid","    at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:52:27)","    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)","    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)","    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14)","    at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)","    at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)","    at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10","    at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)","    at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:690:12)","    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:116:18)"]}
END RequestId: 043b493d-8457-43f1-8eeb-dc641ac3816f

我在这里错过了什么?使用 sam 在本地调用 lambda 函数并将其连接到本地 运行ning dynamodb 容器是否需要任何其他步骤?

lambda 代码

var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});

exports.lambdaHandler = async (event, context) => {
    const dynamoDB = new AWS.DynamoDB.DocumentClient();
    const params = {
        TableName: 'employees',
        Key: {
            name: "naxi"
        }
    };

    const result = await dynamoDB.get(params).promise();
    if (result.Item) {
        return result.Item;
    } else {
        return { error: 'Task not found.' };
    }
};

您需要使用 AWS CLI 在本地机器上配置访问密钥和密钥以及区域。出现此错误是因为没有正确的访问密钥和密钥,或者没有配置它。

参考以下link: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html