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

Posted

技术标签:

【中文标题】UnrecognizedClientException","errorMessage":"本地测试lambda函数时请求中包含的安全令牌无效【英文标题】:UnrecognizedClientException","errorMessage":"The security token included in the request is invalid when testing lambda function locally 【发布时间】:2021-12-12 13:04:09 【问题描述】:

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

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

    docker network create lambda-local docker run —-network=lambda-local —-name users -d -p 8000:8000 amazon/dynamodb-local aws dynamodb create-table --table-name employees --attribute-definitions AttributeName=name,AttributeType=S --key-schema AttributeName=name,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 --endpoint-url=http://127.0.0.1:8000

然后使用以下命令,我可以验证就本地 dynamodb 而言一切正常。

    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 函数并将其连接到本地运行的 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.' ;
    
;

【问题讨论】:

【参考方案1】:

您需要使用 AWS CLI 在本地计算机中配置访问密钥和密钥以及区域。引发此错误的原因可能是没有正确的访问权限和密钥或未对其进行配置。

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

【讨论】:

不知何故,我假设为了在本地测试这个,我真的不需要正确配置 cli。一旦我解决了这个问题,它就会按预期工作。非常感谢!

以上是关于UnrecognizedClientException","errorMessage":"本地测试lambda函数时请求中包含的安全令牌无效的主要内容,如果未能解决你的问题,请参考以下文章