AWS Lambda Basics – Writing Serverless Code

Introduction

There are four key capabilities for a service or platform to be serverless:

  • No server management.
  • Flexible Scaling.
  • High availability (fault tolerence)
  • No idle capacity

In this post, we will learn the basics of AWS Lambda and how you can use it for different use cases with ease. This will be an introduction post, and shall provide a foundation for upcoming demos and posts for AWS learning.

AWS Lambda is the compute layer where your code is executed.

Lambda is based on functions:

  • Function as a service (FAAS) Offering.
  • Each function contains the code you want to execute, Configurations how to execute, Event-Source (optional) which can detect events and call your functions.
  • Lambda is always ON and ready to receive calls, automatically scales (run multiple copies of the function in parallel, based on demand).
  • Lambda is also stateless
  • Lambda is billing in 100ms increments.
  • Choose Memory, get CPU. (CPU and network capacity increases as allocate more memory).
  • You shall test and measure memory settings to optimize execution cost.

Beware of code-size and other limitations, more info on this link.

You can visit AWS Official Lambda Page for more information and updates.

Workloads you can consider for Lambda

Following are few workload types you can consider lambda:

  • Backend processing.
  • Event processing.
  • Stream processing.
  • Data processing.

Things that can trigger Lambda Functions

  • AWS resource triggers (DynamoDB ops, S3 events, Message Queue Ops etc.)
  • AWS endpoints (REST calls).

Why Lambda

  • Simply execute code.
  • Automatic scaling.
  • Fault tolerant.
  • Pay for usage.

Writing Lambda Code – Options

  • Locally: Develop locally, bundle and deploy to AWS.
  • Directly: in AWS editor.
  • or Mix.

Creating function package

Typically, you will bundle up the source-code/published output into a zip file

However, in this post, we will write lambda directly using AWS Web Console.

Debugging

  • Test events from Web Console.
  • Use logging and CloudWatch.

Getting Started with Lambda

You can Author from scratch, use a blueprint or other options to get started with AWS Lambda. The following screenshot shows different blueprints available ranging various level of complexity and different mix of AWS services:

You can check the official documentation on this link to check the developer guides and other information.

Writing a Simple Lambda Function

Well there are a lot of tutorials online to show how to write lambda functions, so I will not go into those specifics.

Here is the code, which is based on hello-world NodeJS blueprint and then I modified to return data modelling a library domain:

index.js

This is entry point, a handler function. Inside the function, we are retrieving books from a library and return those to caller.

book-library.js

This is simply an in-memory data store to return books in the library.

book.js

book entity with title and author info

Deploy and Test the Lambda function

Once we have written function code, we can deploy and test is directly from web console:

Deploy

Test

Integrating Lambda Function

Based on use-case, there are many different ways and AWS services, you can integrate with lambda functions while building solutions. Following are few of those arrangements:

API Gateway + Lambda

API Gateway is a very common component which receives HTTP/Web Socket requests and can delegate those requests to lambda functions:

Lambda with S3 and SNS

Use Case: Image uploaded to a certain S3 bucket and needed to be resized in different ways.

You could write a function to perform resizing, then configure the S3 bucket to call the lambda function every time a new object is uploaded to the bucket.

You could do a similar process with a SNS topic, whenever data is published to a certain topic, a lambda function is called and then can do something with the data.

Function URLs

A function URL is a dedicated HTTP(S) endpoint for your function. When your function URL is configured, you can use it to invoke your function through a browser, curl, Postman, or any HTTP client. When you configure a function URL from the main function page:

Click Create function URL button and following window will show up:

Here I am selecting NONE as Auth type, which results in public URL which anyone can call. For our demo purposes, its ok to select this type and click Create button and following picture shows that Function URL is created:

And if we click the URL, it opens the browser and you can see that the data is returned.

Lambda function URLs are a new feature (announced in April 2022. this month) and you can learn more about it on this link.

Summary

This was a very basic and short introduction of AWS Lambda and how you can use this compute FAAS offering from AWS to quickly write code for various situations.

In this post, we covered the basics of AWS lambda and wrote a simple function which returns some data when called using Function URL. We will be using lambdas a lot in later posts and will have opportunity to learn more advance use cases of Lambda functions.

Let me know if you have some questions or comments. Till next time, Happy Coding.


Discover more from Hex Quote

Subscribe to get the latest posts sent to your email.

3 thoughts on “AWS Lambda Basics – Writing Serverless Code”

Comments are closed.

Discover more from Hex Quote

Subscribe now to keep reading and get access to the full archive.

Continue reading