首页 > 解决方案 > 使用容器本地调用 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[]

问题:

  1. 如何通过 curl 在本地调用该函数,以便我可以在我的 Go 语言代码中捕获 Querystring 参数?

如果有人可以帮助我解决这个问题,那将非常有帮助。

编辑:在打破我的头一段时间后,我能够弄清楚。答案是

curl -X GET --url http://localhost:8080/2015-1/functions/function/invocations --header 'Content-Type: application/json' --data '{"queryStringParameters": {"name": "john"}}'

标签: goaws-lambdacontainersquery-string

解决方案


在尝试了一堆东西之后,我发现如果我们想在本地调用我的 lambda 并将一个值作为查询字符串参数传递,我需要按如下方式进行。

curl -X GET --url http://localhost:8080/2015-1/functions/function/invocations --header 'Content-Type: application/json' --data '{"queryStringParameters": {"name": "john"}}'

推荐阅读