尝试初始化DynamoDB的客户端时,Amazon Lambda会超时

时间:2017-10-13 14:54:17

标签: java amazon-web-services amazon-dynamodb aws-lambda

我在Amazon的Lambda服务上上传了以下Java类:

ThreadLocal

我还有一个执行角色集,可以让我完全控制DynamoDB。我的权限应该非常好,因为我已经使用与其他使用Lambda和DynamoDB的项目完全相同的权限(唯一的区别是不同的请求类型)。

这个类的目的是让它由API网关(API网关 - > Lambda - > DynamoDB)调用,但是现在我只是想在Lambda上测试它(Lambda - > DynamoDB)

作为参考,如果重要,这里是DeviceRequest类:

public class DevicePutHandler implements RequestHandler<DeviceRequest, Device> {
    private static final Logger log = Logger.getLogger(DevicePutHandler.class);

    public Device handleRequest(DeviceRequest request, Context context) {
        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
        DynamoDBMapper mapper = new DynamoDBMapper(client);

        if (request == null) {
            log.info("The request had a value of null.");
            return null;
        }

        log.info("Retrieving device");
        Device deviceRetrieved = mapper.load(Device.class, request.getDeviceId());

        log.info("Updating device properties");
        deviceRetrieved.setBuilding(request.getBuilding());
        deviceRetrieved.setMotionPresent(request.getMotionPresent());
        mapper.save(deviceRetrieved);

        log.info("Updated device has been saved");
        return deviceRetrieved;
    }
}

这是Device类:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "deviceId", "building", "motionPresent" })
public class DeviceRequest {

   @JsonProperty("deviceId")
   private String deviceId;
   @JsonProperty("building")
   private String building;
   @JsonProperty("motionPresent")
   private Boolean motionPresent;
   @JsonIgnore
   private Map<String, Object> additionalProperties = new HashMap<String, Object>();

   @JsonProperty("deviceId")
   public String getDeviceId() {
       return deviceId;
   }

   @JsonProperty("deviceId")
   public void setDeviceId(String deviceId) {
       this.deviceId = deviceId;
   }

   @JsonProperty("building")
   public String getBuilding() {
       return building;
   }

    @JsonProperty("building")
    public void setBuilding(String building) {
        this.building = building;
    }

    @JsonProperty("motionPresent")
    public Boolean getMotionPresent() {
        return motionPresent;
    }

    @JsonProperty("motionPresent")
        public void setMotionPresent(Boolean motionPresent) {
        this.motionPresent = motionPresent;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

以下是我正在尝试使用以下代码测试Lambda的JSON输入:

@DynamoDBTable(tableName="DeviceTable")
public class Device {
    private String deviceID;
    private String building;
    private String queue;
    private boolean motionPresent;

    @DynamoDBHashKey(attributeName="Device ID")
    public String getDeviceID() {
        return deviceID;
    }

    public void setDeviceID(String deviceID) {
        this.deviceID = deviceID;
    }

    @DynamoDBAttribute(attributeName="Motion Present")
    public boolean getMotionPresent() {
        return motionPresent;
    }

    public void setMotionPresent(boolean motionPresent) {
        this.motionPresent = motionPresent;
    }

    @DynamoDBAttribute(attributeName="Building")
    public String getBuilding() {
        return this.building;
    }

    public void setBuilding(String building) {
        this.building = building;
    }

    @DynamoDBAttribute(attributeName="Queue")
    public String getQueue() {
        return this.queue;
    }

    public void setQueue(String queue) {
        this.queue = queue;
    }
}

没有抛出任何异常(我尝试将它包装在try / catch块周围)并且lambda超时是唯一发生的事情。我在DynamoDB客户端初始化之前的一开始就尝试使用log / print语句来查看是否可以正确读取请求,它看起来似乎正确解析了JSON字段。我还将客户端构建器分离出去,发现构建器对象可以初始化,但是超时时间来自构建器调用build()来构建客户端。

如果有人知道为什么会出现这种超时,请告诉我们!

1 个答案:

答案 0 :(得分:3)

Turns out that by bumping up the timout period AND the allotted memory, the problem get solved. Not sure why it works since the lambda always indicated that its memory usage was under the previously set limit, but oh well. Wish that in the future Amazon will provide better error feedback that indicates if a lambda needs more resources to run.