Sie sind auf Seite 1von 7

Ameya Pandilwar Follow

εɴɢiɴεεʀ • appʀεɴευʀ • ғʀεεlaɴᴄεʀ


Mar 25 · 4 min read

Taking the rst step towards serverless


computing
Serverless computing may be the hottest thing in cloud computing
today, but what, exactly, is it? In fact, it is considered disruptive to
traditional cloud computing and serverless computing simply means
that you, the developer, does not have to deal with the server. A
serverless computing platform like AWS Lambda allows you to build
your code and deploy it without ever needing to con gure or manage
underlying servers. Your unit of deployment is your code; not the
container that hosts the code, or the server that runs the code, but
simply the code itself.

AWS Lambda is an example of a framework that makes it possible to


write and deploy functions that represent the serverless computing
paradigm.

1. Introduction
AWS Lambda is a serverless computing service provided by Amazon to
reduce the con guration of servers, OS, Scalability, etc. AWS Lambda is
capable of executing code on AWS Cloud. It runs in response to events
on di erent AWS resources, which triggers AWS Lambda functions.
Pricing is pay-as-you-go which means we won’t shell our money on idle
lambda functions.

What is a Lambda function?

When you write code designed to run in AWS Lambda, you are writing
functions. The term functions comes from functional programming,
which originated in lambda calculus. The basic idea is to compose an
application as a collection of functions, which are methods that accept
arguments, compute a result, and have no unwanted side-e ects.

2. Maven Dependencies
To enable AWS lambda, we need the following dependency in our
project:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>

This dependency can be found at Maven repository.

We also need the Maven Shade Plugin to build the lambda application:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>

<createDependencyReducedPom>false</createDependencyReducedPo
m>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>

3. Create Handler
Simply put, to invoke a lambda function, we need to specify a handler;
there are 3 ways of creating a handler:

1. Creating a custom MethodHandler

2. Implementing the RequestHandler interface

3. Implementing the RequestStreamHandler interface

Let’s see how to do it using code examples.

3.1. Custom MethodHandler


We’ll create a handler method that will be the entry point for incoming
requests. We can use JSON format or primitive data types as input
values.
Also, the optional Context object will allow us to access useful
information available within the Lambda execution environment:

public class LambdaMethodHandler {


public String handleRequest(String input, Context
context) {
context.getLogger().log("Input: " + input);
return "Hello World - " + input;
}
}

3.2. RequestHandler Interface

public interface RequestHandler<I, O> {


/**
* Handles a Lambda function request
* @param input The Lambda function input
* @param context The Lambda execution environment
context object.
* @return The Lambda function output
*/
public O handleRequest(I input, Context context);
}

We can also implement the RequestHandler into our class and override
the handleRequest method which will be our entry point for requests:

public class LambdaRequestHandler implements


RequestHandler<String, String> {
public String handleRequest(String input, Context
context) {
context.getLogger().log("Input: " + input);
return "Hello World - " + input;
}
}

In this case, the input will be the same as in the rst example.

3.3. RequestStreamHandler Interface


We can also implement RequestStreamHandler in our class and simply
override the handleRequest method.
The di erence is that InputStream, ObjectStream and Context objects
are being passed as parameters:

public class LambdaRequestStreamHandler implements


RequestStreamHandler {
public void handleRequest(InputStream inputStream,
OutputStream outputStream, Context context) {
String input = IOUtils.toString(inputStream, "UTF-
8");
outputStream.write(("Hello World - " +
input).getBytes());
}
}

4. Build Deployment File


With everything con gured, we can create the deployment le by
simply running:

mvn clean package shade:shade

The jar le will be created under the target folder.

5. Create Lambda Function Via Management Console


Sign in to AWS Amazon and then click on Lambda under services. This
page will show the lambda functions list, which is already created.

Here are the steps required to create our lambda:

1. “Select blueprint” and then select “Blank Function”

2. “Con gure triggers” (in our case we don’t have any triggers or
events)

3. “Con gure function”:

• Name: Provide MethodHandlerLambda,

• Description: Anything that describes our lambda function

• Runtime: Select java8

• Code Entry Type and Function Package: Select “Upload a .ZIP and
Jar le” and click on “Upload” button. Select the le which
contains lambda code.

• Under Lambda function handler and role:


• Handler name: Provide lambda function handler name
com.baeldung.MethodHandlerLambda::handleRequest

• Role name: If any other AWS resources are used in lambda


function, then provide access by creating/using existing role and
also de ne the policy template.

• Under Advanced settings:

• Memory: Provide memory that will be used by our lambda


function.

• Timeout: Select a time for execution of lambda function for each


request.

1. Once you are done with all inputs, click “Next” which will show
you to review the con guration.

2. Once a review is completed, click on “Create Function”.

6. Invoke the Function


Once the AWS lambda function is created, we’ll test it by passing in
some data:

• Click on your lambda function from lists and then click on “Test”
button

• A popup window will appear which contains dummy value for


sending data. Override the data with “AWS Lambda”

• Click on “Save and test” button

On the screen, you can see the Execution result section with
successfully returned output as:

"Hello World - AWS Lambda"

7. Conclusion
In this quick intro article, we’ve created a simple AWS Lambda app
using Java 8, deployed that to AWS and tested it.

Das könnte Ihnen auch gefallen