Going serverless: Your first AWS Lambda

Have you been looking for a straightforward and no over-engineered way to write your first serverless application? Have you been wondering what is AWS Lambda and how I can deploy my serverless application on AWS cloud? This is what we will be discussing in this article: Creating your first AWS Lambda and execute it on AWS infrastructure without provisioning any servers – thus constituting to be ‘serverless’.

Our Use Case

Our Lambda function is going to do a very simple job, but not printing Hello World. We will trigger our AWS Lambda with a test event, fetch a small string from that event, concatenate multiple strings and return as a response. In short – Trigger, Process, Response. The code we will be using can be found here on GitHub, which will be uploaded to AWS Lambda in simple basic steps using the AWS Console.

But basics first:

Before you can start with writing a lambda function, you need to understand how it works.

Point of entry

The point of entry of a Lambda are triggers which are actions caused by specific events. The most common ways to trigger a lambda are:

  • API Gateway event: This is the most classic way to trigger a lambda via an API.
  • S3 event: In this case, S3 generates an event when operations such as object creation, deletion, restore are performed on a specific S3 bucket or object.
  • DynamoDB event: As soon as modifications occur on a DynamoDB item or table, events are generated and Lambda is invoked. However, table and data level activities are recorded by DynamoDB Streams.

“A DynamoDB stream is an ordered flow of information about changes to items in a DynamoDB table.”

AWS Developer Documentation
  • SNS notifications: Following the pub-sub pattern, messages are published to an SNS topic to which an AWS Lambda is subscribed to. The AWS Lambda receives the message as input payload and gets triggered.
  • SQS messages: Lambda performs the polling operation on an SQS queue, receives the message as input payload and processes it.

Runtime

AWS Lambda supports several runtime environments for your serverless application. This is chosen based on the programming language you function is written in.

Handler

The vital part of AWS Lambda is the handler. As soon as the event triggers the Lambda, Handler is called which takes JSON request, processes the request and sends a JSON response. At the time of the Lambda creation, the handler is specified which will be used when the lambda is invoked.

public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
		RequestController controller =
	            new RequestController(
	                input,
	                context, COMPONENT_NAME + " " + API_VERSION);
	        return controller.handle();
	}

We are developing this lambda to get triggered by an AWS API Gateway, gets the JSON request, forwards it to the controller which processes the request and returns the response event.

public APIGatewayProxyResponseEvent handle() {
      return execute();
}

private APIGatewayProxyResponseEvent execute() {
      String dummyBodyContent = context.getAwsRequestId() + " " + input.getBody() + " " + dummyInput;
      APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent = 
                              buildResponse(HttpResponseStatus.OK.getStatusCode(), dummyBodyContent);
      return apiGatewayProxyResponseEvent;
}

Permissions

In order to use AWS resources, AWS Lambda assumes an execution role. The execution role consists of operations it is allowed to perform on AWS resources.

Build and Deployment

Since our java sample serverless application has Gradle build system configured, the first thing to do is to build it using the following command:

./gradlew clean build

This results in the creation of a zip artefact which consists of necessary dependencies required to invoke as an AWS Lambda. Using the AWS lambda console, create the new function from scratch as shown below:

Create Lambda using AWS Console

As mentioned earlier, execution role is one of the vital components of Lambda execution workflow. Since this is a very simple example which does not use any other AWS resource, we will create an execution role with basic Lambda permissions as shown below:

AWS Lambda execution role creation via AWS Console

Once the AWS lambda is created, the next step is to upload the artefact created in the previous step using gradle build command. In the function package, select Upload and upload the artefact (.zip) which can be found in build/serverless/sample-aws-lambda-java.zip. Make sure that you are pointing to the right handler of the project, which is com.java.handler.ComponentHandler::handleRequest

AWS Lambda Console for uploading the artiefacts and pointing to the right handler

Let’s test how it is working for us now. AWS provides a significant amount of test event but we can configure a simple test event using this dummy event.

2-aws-lambda-test-events.png

Once the test event is configured, we can trigger the event using the Test button. The result is a successful invocation of AWS Lambda, which concatenates multiple strings together along with request body as shown below:

Conclusion

In this article, I introduced you to AWS Lambda for your serverless applications. I dissected AWS lambda for you, discussed some of the important components within the AWS Lambda workflow execution. We later build a sample java serverless application, configured and deployed using AWS Lambda console and tested it in the end. With this activity, some questions remain: What happens when we have several serverless application for multiple deployments across different environments, each deployment requiring different permission policies and different triggers? This will be discussed further in the next subsequent articles.

Published by backdoorcodr

Passionate about Cloud, Data; and everything that revolves around them!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: