1

我已经从这个存储库中克隆并部署了示例无服务器 Lambda 函数,https://github.com/serverless/examples/tree/master/aws-python-simple-http-endpoint,它有一个简单地打印当前时间:

import json
import datetime


def endpoint(event, context):
    current_time = datetime.datetime.now().time()
    body = {
        "message": "Hello, the current time is " + str(current_time)
    }

    response = {
        "statusCode": 200,
        "body": json.dumps(body)
    }

    return response

我已经部署了它:

> serverless deploy list functions
Serverless: Listing functions and their last 5 versions:
Serverless: -------------
Serverless: currentTime: $LATEST, 1

并且能够使用以下方法调用它serverless invoke

> serverless invoke --function currentTime --log
{
    "body": "{\"message\": \"Hello, the current time is 22:51:06.872660\"}",
    "statusCode": 200
}
--------------------------------------------------------------------
START RequestId: d4629611-e16b-4afa-80ef-5ac1a7331679 Version: $LATEST
END RequestId: d4629611-e16b-4afa-80ef-5ac1a7331679
REPORT RequestId: d4629611-e16b-4afa-80ef-5ac1a7331679  Duration: 0.32 ms   Billed Duration: 100 ms     Memory Size: 1024 MB    Max Memory Used: 43 MB  

在 AWS 控制台中,我在“API Gateway”服务下查找了 Lambda 函数的端点,

在此处输入图像描述

并尝试了curl它。但是,我收到一个缺少授权令牌的错误:

curl https://x6gnvhuuzh.execute-api.us-east-1.amazonaws.com/dev
{"message":"Missing Authentication Token"}⏎  

根据README.md那个例子,我应该看到与serverless invoke. 知道端点为什么返回Missing Authentication Token消息吗?

4

1 回答 1

0

显然我忘记了/ping在 Github 示例中附加到 URL。如https://docs.aws.amazon.com/apigateway/latest/developerguide/amazon-api-gateway-using-stage-variables.html中所述,

在此处输入图像描述

所以我输入了 URL/ping并得到了预期的结果:

在此处输入图像描述

于 2019-08-30T23:01:05.547 回答