Beginner’s Guide to Serverless Computing with AWS Lambda Functions


In the ever-evolving landscape of cloud computing, serverless architecture has emerged as a game-changer, offering developers a paradigm shift in building scalable and cost-effective applications. At the forefront of this revolution is AWS Lambda Functions, a serverless compute service provided by Amazon Web Services (AWS). In this article, we’ll explore the concept of AWS Lambda functions, delve into examples of small beginner projects, and discuss the official stacks supported by AWS Lambda. Additionally, we’ll touch upon running alternative stacks within frameworks like Bref for PHP and others within Docker containers.

Understanding AWS Lambda Functions:

AWS Lambda allows developers to run code without provisioning or managing servers. Instead, you can upload your code, and Lambda takes care of everything needed to run and scale your application with high availability. This serverless approach eliminates the need for infrastructure management, allowing developers to focus solely on writing code and building features.

AWS Lambda supports a variety of programming languages, including but not limited to:

  1. Node.js
  2. Python
  3. Java
  4. C#
  5. Ruby
  6. Amazon Linux

Getting Started with AWS Lambda:

Let’s kick off our exploration with a simple example in Node.js. Suppose you want to create a function that calculates the square of a given number. First, you need to create a Lambda function in the AWS Management Console and configure a trigger, such as an API Gateway or an S3 event. Once set up, you can write your code directly in the Lambda console or upload a deployment package containing your code.

// Lambda function to calculate the square of a number

exports.handler = async (event) => {
const number = event.number || 0;
const square = Math.pow(number, 2);
return { statusCode: 200, body: JSON.stringify({ result: square }) };
};

In this example, the Lambda function takes an input parameter number, calculates its square, and returns the result in JSON format. The event parameter contains the input data passed to the function.

Supported Stacks by AWS Lambda Functions:

AWS Lambda natively supports various programming languages and runtimes. As of the latest updates, the officially supported stacks include (as of the time of writing):

  1. Node.js: nodejs20.x, nodejs18.x
  2. Python: python3.11, python3.10, python3.9
  3. Java: java21, java17, java11, java8.al2
  4. C#: dotnet8
  5. Ruby: ruby3.2
  6. OS-Only: provided.al2023, provided.al2

These officially supported runtimes cater to a wide range of developers, ensuring flexibility and ease of integration with existing codebases.

Custom runtimes within AWS Lambda Functions:

While AWS Lambda provides a rich set of supported runtimes, developers often encounter scenarios where they need to use other languages or frameworks. This is where solutions like Bref for PHP and Docker containers come into play.

Bref for PHP:

Bref is an open-source project that extends AWS Lambda support to PHP. With Bref, PHP developers can seamlessly deploy serverless applications without compromising the language’s elegance and flexibility. To get started, install the Bref CLI and configure your project.

# Install Bref CLI
composer require bref/bref

# Initialize Bref in your project
vendor/bin/bref init

Now, you can write your PHP functions as classes and deploy them to AWS Lambda effortlessly.

Docker Containers:

For even greater flexibility, AWS Lambda supports custom runtimes through Docker containers. This means you can package your application and its dependencies into a container and run it as a Lambda function. This opens up a world of possibilities, enabling developers to use virtually any programming language or framework.

Limitations of AWS Lambda Functions:

While AWS Lambda offers a lot of flexibility and takes away the burden of managing servers, it does come with its own set of limitations that developers and businesses alike must contend with. Here are the most notable considerations.

Compute Resources

In 2024, AWS Lambda Functions scale to quite a significant degree, but they still don’t hold a candle to full scale data centers. For a vast majority of developers, this will not matter, but it does have to be said that at the time of writing, Lambda Functions offered by AWS have a maximum memory allocation limit of 10GB, which is also the maximum storage they can have. Additionally, developers have no direct control over the processing power available within the Lambda Function, though it does automatically scale as memory allocation is increased.

Bundled Code Limitations

A Lambda Function created with a direct upload of code only supports up to 50 MB. For NodeJS scripts, this poses challenges as Node Modules frequently go over this size limit even for relatively simple projects with a few dependencies. To circumvent this, you could use Layers within your Lambda Functions to extend this limit to 250 MB. Alternatively, you could upload your code to S3 and download it during function invocations – though keep in mind that this increases the time that a Lambda Function runs for, thereby increasing its invocation cost. Lastly, if you use a docker image as the bundle for your Lambda Function, you have a maximum image size of 10GB available to you inclusive of all layers.

Cost Considerations

AWS offers a very generous free tier for Lambda Function invocations. For beginners just getting in to developing on the platform, this free tier is sufficient for virtually any development or early production prototypes. However, once this free tier quota is exhausted, it is very important to keep a close watch on AWS Lambda costs. This is because of a few considerations:

  1. AWS Lambda is charged by the millisecond.
  2. The cost per millisecond is multiplied by the amount of memory allocated to the Lambda Function, in multiples of 128 MB.
  3. Multiple Lambda Functions can run concurrently, up to 1000 per AWS region.
  4. Lambda Functions have a retry policy, wherein each failed invocation retries 3 times

All of these considerations seem simple on paper, but require careful consideration and monitoring to ensure they don’t exceed your expectations.

Conclusion:

AWS Lambda Functions provide developers with a powerful and scalable serverless computing platform. With its support for multiple programming languages and integration capabilities, developers can easily build and deploy applications without the hassle of managing infrastructure. Additionally, solutions like Bref for PHP and Docker containers extend Lambda’s capabilities, allowing for even greater flexibility in choosing languages and frameworks. As you embark on your serverless journey, explore the endless possibilities that AWS Lambda brings to the table and elevate your development experience. Happy coding!

See also: Exciting Node.JS features for developers released in March 2024


2 responses to “Beginner’s Guide to Serverless Computing with AWS Lambda Functions”

  1. […] Food for thought: Try deploying this as a service to AWS Lambda! […]

  2. […] Looking to step into web development? Check out this post to get started! […]

Leave a Reply

Your email address will not be published. Required fields are marked *