简体   繁体   English

ObjectMapper通过API网关打破AWS Lambda

[英]ObjectMapper breaking AWS Lambda via API Gateway

There is an API Gateway calling a lambda function using the lambda proxy. 有一个API网关使用lambda代理调用lambda函数。

The below works fine body is logged and body is sent back: 下面的工作原理可以很好地记录正文并将正文发送回:

package com.dapper.cloud.function;

import java.util.Map;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

public class GrantJwt implements RequestHandler<Map<String, Object>, APIGatewayProxyResponseEvent>{ 
    @Override
    public APIGatewayProxyResponseEvent handleRequest(Map<String, Object> input, Context context){

        System.out.println(input.get("body").toString());

        return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody(input.get("body").toString());
    }
} 

When I update it to use the ObjectMapper like so: 当我更新它以使用ObjectMapper如下所示:

package com.dapper.cloud.function;

import java.util.Map;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.fasterxml.jackson.databind.ObjectMapper;

public class GrantJwt implements RequestHandler<Map<String, Object>, APIGatewayProxyResponseEvent>{ 
    @Override
    public APIGatewayProxyResponseEvent handleRequest(Map<String, Object> input, Context context){
        ObjectMapper m = new ObjectMapper();
        System.out.println(input.get("body").toString());

        return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody(input.get("body").toString());
    }
} 

The logs do not show the body and the response is: 日志不显示正文,响应为:

{
    "message": "Internal server error"
}

Can I use Jackson in AWS Lambda? 我可以在AWS Lambda中使用Jackson吗?

Parent POM 父POM

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-core</artifactId>
        <version>1.2.0</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-events</artifactId>
        <version>2.2.6</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.9</version>
    </dependency>
</dependencies>

Child Build 子代

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <createDependencyReducedPom>false</createDependencyReducedPom>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Well as foolish but if this helps anyone I am very happy. 太愚蠢了,但是如果这可以帮助任何人,我会很高兴。

ObjectMapper is not the issue. ObjectMapper不是问题。 I did a deployment with travis-ci a couple times and apparently when that happens if a memory_size and timeout is not set a default 128 mb and 3 second is used. 我使用travis-ci进行了几次部署,显然,如果未设置memory_size和timeout,则默认情况下会发生这种情况,默认使用128 mb且使用3秒。 This differs from the defaults of a new lambda which is 512 mb and 15 seconds. 这与新的lambda的默认值(512 mb and 15秒)不同。

I opened a request to change this on travis forum. 我在travis论坛上提出了更改此要求的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM