Search code examples
amazon-web-servicesspring-bootjava-8aws-lambdamicroservices

How to deploy a SpringBoot microservice application(RESTful) as serverless to AWS Lambda?


I have developed a simple microservice, REST based using Java 8 and Spring Boot2.0. It has its own REST end points which I can call using Postman and I get the response very well. Now I have doubt in understanding the design & architecture if I want to deploy the same application on AWS cloud. I want my application to behave as serverless so I want to deploy on AWS using its Lambda service.

Please assist to clear my following doubts :- 1) First, can I upload my whole application code to AWS Lambda in order to make it serverless?

2) If yes, then do I need to use AWS API Gateway (compulsorily) to invoke my Lambda function when the request passes through it?

3) If yes (point 2), then end points which are there in my original microservice code will become ineffective and will be overridden by new API Gateway end points?

My whole doubt is about end points, which end point will be used to invoke the Lambda functions?

Please assist to clarify my doubt. If there is any sample reference material then it will be really great.

Cheers


Solution

  • Spring Boot and AWS Lambda don't naturally go together IMO.

    Lambda is pure code, it does not present itself as a HTTP Server, it is just triggered by one of the other AWS services (API Gateway, CloudWatch, S3, DynamoDB, Kinesis, SDK, etc.). The handler receives a JSON request from the calling service, and processes the request. Here is an example.

    API Gateway does much of what Spring Boot provides for you. API Gateway is always online waiting for HTTP requests to come in, for which you only pay for incoming requests, you do not pay for idle (the definition of serverless IMO).

    Once a request comes in, API Gateway wraps the request payload with some additional environmental data and sends it to your Lambda handler, which processes the request and returns some response to the client.

    Saying that, if you don't want to restructure your service, there are a couple of options open to you:

    1. Wrap into a Docker image and use an AWS Container Service, either using ECS or ElasticBeanstalk, neither of these are considered to be serverless.

    2. I have not tried this, but according to AWS:

    You can use the aws-serverless-java-container library to run a Spring Boot application in AWS Lambda. You can use the library within your Lambda handler to load your Spring Boot application and proxy events to it.

    See links to Convert your SpringBoot project and Deploy it to AWS Lambda.

    Hope this helps.