How to port a java Spring application to run in AWS Lambda - spring

I would like to port my Java Spring application to run in AWS Lambda. Is it possible? Are there any resources you could point me to? There are some tutorials available for Express, but not for Java Spring applications. Would greatly appreciate your help.

AWS Lambda is serverless and have limitation to upload binary and max execution time is 15 min.
You really not need Spring or any other framwork to run lambda as its working on own framwork
Plz share your usecase why need sparing with Lambda

Related

How to connect to the AWS services using IAM roles ARN in a Spring Boot application

I am using AWS SQS, SNS, and S3 services. So for that i have created the roles and queues in aws. Now I have roles ARNs and Queues ARNs. How can I connect to these services through my spring boot app?
I have gone through this link, but i didn't get how to use the cerdentials from AWSCredentialsProvider. Please help me in this.
Thanks in advance!
"I didn't get how to use the cerdentials from AWSCredentialsProvider."
I am going to answer this question using the recommended SDK - which is AWS SDK for Java V2. You may find V1 in old online content - but using V1 is not best practice.
There are different ways of handling creds when writing a Java App that uses AWS SDK for Java V2 - including a Spring BOOT app.
You can use an Environment variable provider:
Region region = Region.US_EAST_1;
RdsDataClient dataClient = RdsDataClient.builder()
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.region(region)
.build();
You can use the shared credentials and config files. This reads your creds from a Credential file located under .aws.
Region region = Region.US_EAST_1;
RdsDataClient dataClient = RdsDataClient.builder()
.region(region)
.build();
You can use a StaticCredentialsProvider where you put your creds in the code.
AwsBasicCredentials credentials = AwsBasicCredentials.create("<KEY>", "<Secret Key>");
StaticCredentialsProvider staticCredentials = StaticCredentialsProvider.create(credentials);
Region region = Region.US_EAST_1;
DynamoDbClient ddb = DynamoDbClient.builder()
.region(region)
.credentialsProvider(staticCredentials)
.build();
All of these credential ways are explained in the AWS Java V2 Developer Guide -- which I strongly recommend that any developer programming with the AWS SDK for Java V2 SDK read.
Finally, you will find code examples of writing a Spring BOOT example with the AWS SDK for Java v2 in the AWS Github code repo. For example.
Creating your first AWS Java web application
This creates an example Spring Boot web app that submits data to an Amazon DynamoDB table.
So the idea is that Assuming Roles is not application part, it's the infra service where your application is executing on.
For e.g.: If you have Spring Boot application running on EC2 (or Fargate, or Lambda, or Elastic Beanstalk or anywhere in AWS) that EC2 should have assumed the role. The "role" then should have rights to access SQS (or any service). Now when your application will try to use SQS running on EC2 with right role, everything will be fine.
If you're testing the code on your machine then it will not work as your machine has not assumed the role.

Deploy Spring Cloud Function to OpenFaaS

Spring Cloud Function is a way to write serverless functions with Spring Boot. There are only adapters for AWS, Azure, etc. but no way to deploy to OpenFaaS yet.
How do I deploy a Spring Cloud Function to OpenFaaS?
I created something using the official Java11 template but I am not sure if this is a good solution. You can find the code here: https://github.com/PySualk/spring-cloud-function-openfaas

It is correct to use spring boot with amazon lambda?

I would like a solution that allows to manage json objects, calls to SOAP services and access to the DB via Spring data on a set of lambda services.
But on the net I read that lambda functions should be very simple.
Maybe Spring cloud is the solution?
Regards
In my opinion Spring Boot isn't a good technology choice for AWS Lambda but it's possible. The best way to start using it is to checkout the different wrappers and helpers in aws-serverless-java-container. There is also a good blog post about Spring Cloud Functions on AWS Lambda if that's interesting to you.
The internet tells you that Lambda functions should be simple because then they're easier to scale. If you create another monolith using Spring Boot on top of AWS Lambda, then this is not what AWS Lambda should be used for.
Below I'm listing advantages and disadvantages of using Spring Boot in AWS Lambda. There are probably more but these are the first ones that came to my mind.
Advantages
You can setup an API Gateway with proxy integration to AWS Lambda and then handle all the REST endpoints like you're used to with Spring Boot.
You don't have to manage your instances because AWS Lambda is doing that for you.
As soon as one Lambda function instance has started a Spring Boot container, this function will respond in a consistent way in terms of performance as expected from Java (at least that's my experience with Java Lambda functions).
Disadvantages
More complexity because you need to map an API Gateway event to an HTTP event that Spring Boot can handle. That's what the wrappers mentioned above already do for you.
A longer cold start time for your Lambda functions because Spring Boot is too slow to startup quickly like it's expected from Lambda functions. This means, you'll have spikes in your response times when a new Lambda function instance is started. If this is unacceptable, then don't choose Spring Boot with AWS Lambda.
You'll have multiple Spring Boot containers running without much control because AWS Lambda spins up instances if they are needed. You can control the max and min number of instances, but that's it.
Making SQL queries from AWS Lambda can be a pain due to the uncontrollable number of instances. You might run out of connections quite quickly if a spike hits your Lambda functions because it spins up more and more instances.
Having said that, I have used Java Lambda functions in the past and it is great to run them in the background doing some data processing. However, I'm not convinced that they are a good choice for "customer facing" endpoints like a REST API (except if you keep a minimum of Lambda instances running but then you can question if AWS Lambda is the right choice for you).

Can we deploy spring micro services on AWS Lambda?

Is it recommended to deploy spring micro services on Lambda. I have recently deployed a sample spring boot app returning a sample data from AWS RDS Oracle DB on Lambda, Just analyzing the possibility if we could deploy two micro services in different Lambda functions and configure them using API gateway.
I am entirely new to it and trying to understand if Lambda can really support micro services patterns like Service discovery/Circuit breaker or Saga patterns if I deploy two or more services on different lambda functions. Would Lambda be used for such scenarios in Real Time ?
Can anyone help me if there would be limitations other than package/payload size limit or timeout limitations ?
Thanks in Advance
If you use java for serverless application like AWS lambdas I would recommend for looking a framework which supports Ahead-of-Time compilation which will boost a lot your application start. If your application is not bit it will be very easy to be rewritten with Micronaut (very similar to Spring boot development)
For instance have a look at Micronaut, Quarkus using with Graalvm. Spring Boot is not the best option using with directly with AWS lambdas, it is big package size + really a lot of initial time, not good for API services.
Target of max package sizes of 25 MB for small cold start with adjustable memory starting from 256MB, with spring boot this is not doable.
In case you are referring to Spring Boot Cloud Functions that's exactly they were made for.
Google Cloud Functions already have an Alpha product for running Java functions.
AWS Lambda seems to have support for this as well.
In case you simply wrote a Spring Boot app with embedded Tomcat that you packaged in a Docker container which you're spinning up on demand, beware that cold start can be an issue, especially with the overhead of the Tomcat.

Deploy my spring boot application into google cloud computer engine

I got a query to ask you all. I am looking for guides that help me deploy my spring boot application on google cloud computer engine, I type in my instance IP address when I test my spring boot application I unable to access it in REST API.
May I know do you have any guides or steps for me to follow to deploy successfully in google cloud computing engine. Why do I need to deploy in computer engine is because I deployed my angular at it and I deploy it both it seems that my angular project being replaced by my spring boot application.
Codelabs GCP / Spring series has deployment tutorials:
https://codelabs.developers.google.com/spring/
GCP has some "Getting Started" tutorials you can use here:
https://cloud.google.com/java/docs/
where the specific one for deploying a java app to GCE is here:
https://cloud.google.com/java/docs/tutorials/bookshelf-on-compute-engine
But the basic steps are as follows:
Write your Spring app
Build your Spring app
Run / test your jar locally
Push your jar to a location in Storage
Create a startup script for your GCE instance
Create a new GCE VM which uses your startup script using Console, Deployment API, or gcloud tool
After that, you need to ensure you have the proper network rules in place to be able to access your API publicly. If you do not wish to learn how to use GCE, I would suggest you look into using App Engine instead because then you do not need to learn how to deploy and instead can concentrate on your api. Here is a guide to do that

Resources