簡體   English   中英

UnrecognizedClientException","errorMessage":"本地測試 lambda 函數時,請求中包含的安全令牌無效

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

我正在嘗試使用 aws sam 在本地調用我的 lambda 函數進行測試。 該函數從 dynamodb 表中讀取一個項目。 我已經啟動了一個本地 dynamodb 容器,在其中創建了所需的表。

運行以下命令以創建本地 dynamodb 容器。

  1. docker 網絡創建 lambda 本地
  2. docker run —-network=lambda-local —-name users -d -p 8000:8000 amazon/dynamodb-local
  3. aws dynamodb 創建表 --table-name 員工 --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 而言一切正常。

  1. aws dynamodb 列表表 --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 容器是否需要任何其他步驟?

拉姆達代碼

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 在本地機器中配置訪問和密鑰以及區域。 由於沒有正確的訪問和秘密密鑰或沒有配置它,會引發此錯誤。

請參閱以下鏈接以供參考: https : //docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM