3

Getting 502 "Gateway Timeout" for AWS SpringLambdaContainerHandler lambda and API gateway integration

I am trying to have an API gateway bound to a lambda function using SpringLambdaContainerHandler in aws.

Here are the relevant classes.

package org.san.aws.subscription;

import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.spring.SpringLambdaContainerHandler;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;


public class LambdaHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse>{
    private SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
    LambdaLogger log = null;

    public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context context) {
        log = context.getLogger();
        log.log("entering method handleRequest");
        if (handler == null) {
            try {
                handler = SpringLambdaContainerHandler.getAwsProxyHandler(AppConfig.class);
            } catch (ContainerInitializationException e) {
                log.log("Cannot initialize spring handler"+e.getStackTrace());
            }
        }
        AwsProxyResponse response = handler.proxy(awsProxyRequest, context);

        return response;
    }
}

package org.san.aws.subscription;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages ={"org.san.aws.subscription", "org.san.aws.subscription.controller"})
public class AppConfig {

}

package org.san.aws.subscription.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("subscriptions")
public class SubscriptionController2 {

    @RequestMapping(method = RequestMethod.POST)
    public String createSubscription() throws Exception {
        return "Hello..";
    }
}

Using the following swagger spec

swagger: "2.0"
info:
  description: "subscription"
  version: "1.0.0"
  title: "subscription"
  termsOfService: ""
  contact:
    email: "abc@xyz.com"
  license:
    name: ""
    url: "http://localhost/"
schemes:
- "http"
paths:
  /subscriptions:
    post:
      tags:
      - "subscriptions"
      summary: "create a new subscription"
      description: ""
      operationId: "addSubs"
      consumes:
      - "application/json"
      - "application/xml"
      produces:
      - "application/xml"
      - "application/json"
      responses:
        200:
          description: "ok"
        201:
          description: "created"
        405:
          description: "Invalid input"
externalDocs:
  description: "Find out more about Swagger"
  url: "http://swagger.io"

For the lambda function deployment I have given Handler as org.san.aws.subscription.LambdaHandler Timeout for lambda is 30 san

I've created the API Gateway as "Import from swagger"

  Then under resources in API Gateway:
  Integration type -> Lambda Function
  Use Lambda Proxy integration -> Ticked
  Lambda Function -> subscriptionHandler
  Custom timeout -> 29000

Then I click "Deploy API" in Actions menu. Then Click on Resources and then click on the POST method that appears. Then click on Test in the /subscriptions - POST - Method Execution pane.

As the response I am getting the following:

 {
  "statusCode": 502,
  "headers": {
    "Content-Type": "application/json"
  },
  "body": "{\"message\":\"Gateway timeout\"}",
  "base64Encoded": false
}

then also in the logs I am getting:

Thu Dec 14 10:15:59 UTC 2017 : Successfully completed execution
Thu Dec 14 10:15:59 UTC 2017 : Method completed with status: 200

My goal is to get a simple example working. But I keep getting this error and still unable to find a resolution.

Any help is greatly appreciated to get this working.

Chacko
  • 1,506
  • 1
  • 20
  • 42
khami
  • 31
  • 2

0 Answers0