Deployment of the Java code to AWS Lambda
Introduction
In the cloud computing era, companies start using services like Google Cloud Platform, Amazon Web Services or Microsoft Azure. We can hear about the term “Serverless”. It doesn’t mean that we don’t have any servers. It means that third-party services provide us server infsrastrucutre, monitoring and scaling capabilities, so we don’t have to care about that stuff by ourselves and we can focus on writing code. We have concepts like Backend as a Service (BaaS) and Funtion as a Service (FaaS). In BaaS we can configure whole backend infrastructure using third-party cloud services. In this case, we can have database, REST API and other services, which we need depending on our use case. In FaaS, we have just a tiny piece of code responsible for a single job, which we can take and deploy into the cloud. AWS Lambda is an example of FaaS and we’ll focus on it in this article.
AWS Console
First of all, we need to create our AWS account to have an access to AWS console. You can find the console at: https://aws.amazon.com/console/. I had a few problems regarding registration, but finally I created my account. People in support are quite helpful. Please, keep in mind the fact, that you need to provide your debit card details during the registration and they’ll charge you about 3 USD or something like that, which is a verification procedure. In AWS Lambda we’ll get 1 million of free requests per month per cloud function, which is enough for tests or even not so demanding commercial services.
Serverless Framework
Serverless Framework is very convenient way of deploying Lambdas to AWS. You can install it as follows:
1 | npm install serverless -g |
Next, you can type:
1 | serverless login |
in order to log into Serverless. We can use serverless
or sls
alias for this tool.
We also need to authorize Serverless to be able to deploy our Lambdas to AWS. We need to go to AWS Console, in the upper right corner click our name of the user and choose “My Security Credentials”. Then, we need to expand “Access Keys” and create new key
and secret
values. Once we got them, we can authorize Serverless:
1 | sls config credentials --provider aws --key YOUR_KEY --secret YOUR_SECRET |
Serverless will store these credentials in ~./aws/credentials
file. Of course, this is very simple configruation. If we need more users, groups or more sophisticated authorization mechanisms, we should apply them later via IAM (Identity and Access Management) service.
Creating Lambda
Next, we can create our AWS Lambda template. When, we type:
1 | sls create --template |
We will see the list of available templates:
1 | Serverless: Generating boilerplate... |
We will use aws-java-gradle
template:
1 | sls create --template aws-java-gradle |
After that, we’ll get the following project structure:
1 | ├── build.gradle |
If you want to browse source of such template, you can visit my serverless-lambda-playground repo.
We can add our configurations in serverless.yml
file:
1 | service: aws-java-gradle |
Main class is the Handler
class:
1 | public class Handler implements RequestHandler<Map<String, Object>, ApiGatewayResponse> { |
We can modify handleRequest(Map<String, Object> input, Context context)
in order to perform any operation we want. It will be invoked after calling the Lambda.
In case of JVM projects, we need to build them first:
1 | ./gradlew build |
Deployment to the Cloud
Next, we can deploy our Lambda to the AWS Cloud!
1 | sls deploy |
It may take more time in the beginning, but updates should be faster.
We should see something like that:
1 | Serverless: Packaging service... |
Since that moment, our Lambda is deployed!
Be aware of the regions! I’m beginner with AWS, so I haven’t noticed that my Lambda was deployed to us-east-1
and I was checking it on the AWS console, while being switched to another region and I didn’t know why I cannot see my function.
Now, we can log into AWS Console and Navigate to Lambda and then to Functions. We should see our function:
When, we type:
1 | sls info |
We should see information about the Lambda:
1 | Service Information |
We can view details of our Lambda in AWS Console.
Exposing Lambda via API Gateway
We can also apply API Gateway to expose our Lambda to the external world.
We can make our endpoint open or secured.
Next, we can expand Gateway details and we should find “Invoke URL”.
We can call it:
1 | curl https://ipj76pb5yl.execute-api.us-east-1.amazonaws.com/prod/aws-java-gradle-dev-hello |
Of course, url will be different in your setup.
After that, we should receive response:
1 | { |
Hooray! Our Lambda is working on-line now!
Monitoring
In the “Monitoring” section of the AWS Lambda Functions Console, we can monitor usage of our service.
Update
If we want to update our function, we can simply modify it, rebuild code with ./gradlew build
and deploy it again with sls deploy
. Once we have it configured it’s really easy.
Function Removal
If we want to remove our Function, we can just type:
1 | sls remove |
and we’ll see the following messages:
1 | Serverless: Getting all objects in S3 bucket... |
Summary
To wrap up, AWS Lambda is a really convenient way to solve single tasks without worrying about infrastructure, deployment and scalability. If we need one job to be done - e.g. exposing endpoint, transforming images, sending notifications, tiny app running on the server or whatever is required, AWS Lambda is good choice for that. Moreover, thanks to Serverless Framework deployment becomes really easy. In addition, we can develop Lambdas in other languages like Kotlin, Groovy, Scala, Go, Python, Node.js, C# and F#, so we’re not limited just to one language. We also should remember that AWS is powerfull platform with about 100 different services and Lambda is just one of them. It’s good to familiarize with AWS and other cloud computing platforms like Azure, GCP and so on because more companies start to invest in that and such solutions are becoming standard in certain areas.