go - 使用容器本地调用 AWS Lambda 函数
问题描述
所以我一直在尝试探索 Lambda 容器镜像
我有一个 lambda 函数如下
测试.go:
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
fmt.Println("Request %s \n\n", request)
fmt.Println("Context %s \n\n", ctx)
name := request.QueryStringParameters["name"]
return events.APIGatewayProxyResponse{Body: fmt.Sprint("Hello %s", name), StatusCode: 200}, nil
}
func main() {
lambda.Start(handleRequest)
}
我的 Docker 文件为:
FROM amazon/aws-lambda-go
COPY bin/test /var/task
CMD [ "test" ]
我构建了上面带有标签为 testing_go 的 docker 文件,并按如下方式运行它
docker run -p 8080:8080 testing_go
并尝试按如下方式调用它
curl -X GET --url http://localhost:8080/2015-03-31/functions/function/invocations?name=JOHN --header 'Content-Type: application/json' --data '{}'
我看到容器日志显示以下上下文和请求的值
Request %s
{ map[] map[] map[] map[] map[] map[] { { } map[] 0 } false}
Context %s
context.Background.WithDeadline(2020-12-23 03:07:06.186819041 +0000 UTC [325h22m17.998420615s]).WithValue(type *lambdacontext.key, val <not Stringer>).WithValue(type string, val )
map[]
问题:
- 如何通过 curl 在本地调用该函数,以便我可以在我的 Go 语言代码中捕获 Querystring 参数?
如果有人可以帮助我解决这个问题,那将非常有帮助。
编辑:在打破我的头一段时间后,我能够弄清楚。答案是
curl -X GET --url http://localhost:8080/2015-1/functions/function/invocations --header 'Content-Type: application/json' --data '{"queryStringParameters": {"name": "john"}}'
解决方案
在尝试了一堆东西之后,我发现如果我们想在本地调用我的 lambda 并将一个值作为查询字符串参数传递,我需要按如下方式进行。
curl -X GET --url http://localhost:8080/2015-1/functions/function/invocations --header 'Content-Type: application/json' --data '{"queryStringParameters": {"name": "john"}}'
推荐阅读
- symfony - Symfony ICU 翻译在翻译后的消息后尾随换行符
- android - kotlin + Dagger 2:没有 @Provides 注释的方法无法提供 ApiService
- java - 使用 RxJava 向下传递变量的正确方法是什么?
- azure - Terraform Azure 如何获取 AKS 服务主体对象 ID
- assets - redmine plugin_assets 仍然为空
- jupyter-notebook - 在 jupyter 笔记本树中看不到隐藏文件夹
- c# - 从 4.6.1 项目引用 .net framework 4.7.2 程序集
- javascript - 在数组中添加时间值
- php - 如果我没有/不能使用 DI 注入服务,我如何直接从容器中获取服务?
- java - 是否可以在 Thrift 结构中定义联合、嵌套结构和容器的默认值?