dongliao1949 2018-06-12 07:49
浏览 139
已采纳

在AWS上测试Lambda函数时遇到问题

I have been trying to get my first lambda function in Go running via the Amazon API Gateway.

I have the following package set up in go. The goal is to send a JSON request and log and return the body of that request:

package main

import (
    "net/http"
    "log"
    "github.com/aws/aws-lambda-go/lambda"
    "github.com/aws/aws-lambda-go/events"
)

func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {

    //These log statements return empty regardless of JSON input.
    log.Print(request.body)

    return events.APIGatewayProxyResponse{
        StatusCode: http.StatusOK,
        Body: request.Body
    }, nil
}

func main() {
    lambda.Start(Handler)
}

I can build and zip this and upload it to the AWS lambda manager. The AWS lambda manager contains an option to use a test event which I can configure with a JSON string

{
  "testint": 1,
  "teststring": "test"
}

However, if I run this test, I get the following result:

{
  "statusCode": 200,
  "headers": null,
  "body": ""
}

I would expect the body to actually contain the json I passed to the function, but clearly something is going wrong.

  • 写回答

1条回答 默认 最新

  • dongyuan1984 2018-06-20 19:23
    关注

    There's a few minor things I changed and then it works

    First, log.Print(request.body) doesn't compile for me, but using request.Body is fine

    Second, the type you are using for the request is

    // APIGatewayProxyRequest contains data coming from the API Gateway proxy
    type APIGatewayProxyRequest struct {
        Resource              string                        `json:"resource"` // The resource path defined in API Gateway
        Path                  string                        `json:"path"`     // The url path for the caller
        HTTPMethod            string                        `json:"httpMethod"`
        Headers               map[string]string             `json:"headers"`
        QueryStringParameters map[string]string             `json:"queryStringParameters"`
        PathParameters        map[string]string             `json:"pathParameters"`
        StageVariables        map[string]string             `json:"stageVariables"`
        RequestContext        APIGatewayProxyRequestContext `json:"requestContext"`
        Body                  string                        `json:"body"`
        IsBase64Encoded       bool                          `json:"isBase64Encoded,omitempty"`
    }
    

    and in this Body is a field "body" that's a string. So altering your test data to

    {
      "body": "HELLO"
    
    }
    

    will give some data that passes through

    Lastly, the parameters for the Handler in all the examples seems to include a context object, so I added that

    func Handler(ctx context.Context, request events.APIGatewayProxyRequest) 
    

    Here is a complete version of your program that "worked for me"

    package main
    
    import (
        "context"
        "github.com/aws/aws-lambda-go/events"
        "github.com/aws/aws-lambda-go/lambda"
        "log"
        "net/http"
    )
    
    func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    
        //These log statements return empty regardless of JSON input.
        log.Print(request.Body)
    
        return events.APIGatewayProxyResponse{
            StatusCode: http.StatusOK,
            Body:       request.Body}, nil
    }
    
    func main() {
        lambda.Start(Handler)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题