我已经从此存储库https://github.com/serverless/examples/tree/master/aws-python-simple-http-endpoint中克隆并部署了示例无服务器Lambda函数,该函数具有一个可以简单打印当前时间的端点:
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网关”服务下查找了Lambda函数的端点,
,并尝试curl
。但是,我因缺少授权令牌而收到错误消息:
curl https://x6gnvhuuzh.execute-api.us-east-1.amazonaws.com/dev
{"message":"Missing Authentication Token"}⏎
根据该示例的README.md
,我应该看到与serverless invoke
相同的输出。知道端点为何返回Missing Authentication Token
消息了吗?
答案 0 :(得分:0)
显然,我忘了像Github示例中那样,在URL后面附加/ping
。如https://docs.aws.amazon.com/apigateway/latest/developerguide/amazon-api-gateway-using-stage-variables.html所述,
所以我用/ping
输入了URL,并得到了预期的结果: