For the Love of Code - AWS Lambda

When I got started as a software developer, all I cared about was writing code and seeing it run. As I got further into my career, I realized that developing applications was more than just writing code. I also needed to consider the underlying infrastructure of my application, such as the platform my code would run on, the resources my application would need, how my application would scale when the workload increases, and many other things. And with the increasing popularity of cloud services such as AWS (Amazon Web Services) and GCP (Google Cloud Platform), there are now even more things to think about. So when I first heard about AWS Lambda, I initially thought “here comes yet another AWS service to learn”. But after spending some time learning about AWS Lambda I discovered what a great service it is. It handles all the underlying infrastructure of app development and frees us to focus on what we as developers care about the most: writing code.

AWS Lambda when used with other AWS services is a bit tricky to learn but AWS Lambda by itself is quite simple. To understand AWS Lambda we could start by understanding the following concepts: Functions, Microservices, and Serverless computing.

Functions

A function is simply a block of code that does one thing. Functions generally take input from function parameters and return an output based on the input. AWS Lambda codes are simply functions that are called with a specific set of input parameters based on the event that triggers them.

Microservices

In a traditional monolithic architecture, all application services are contained within a single application. Even though the code may be organized in a modular way it all still runs in a single application. With the microservice architecture, a monolithic architecture may be broken down into microservices. Each microservice may represent one of your application’s services and can work in isolation or together with other microservices. This is what AWS Lambda is best suited for.

Serverless Computing

Every web application needs a server to host it including serverless applications. Although a serverless application runs on a server, the hardware resources are allocated on demand by a provider. In the case of AWS Lamda, AWS takes care of the underlying infrastructure so all you have to think about is your code.

Understanding Lambda Functions

First, we need to know how to setup our Lambda code. Lambda codes are simply functions that are called in response to events and in turn events are triggered by other AWS services. A call from an API Gateway, a file uploaded to S3, or an error notification are a few examples of events that can trigger a Lambda function to be executed.

How to Create a Function with AWS Lambda

  1. Login to your AWS Account.
  2. Navigate to the AWS Lambda console. Lambda Navigation

  3. Create a Function.
    • In the Lambda console, click the Create Function button on the upper right of the page. Lambda Create
  4. Choose function options.
    • For Function Name you may set any name to identify your function.
    • For Runtime you may select the language you are most comfortable with.
    • The rest of the options may be left as default.
    • Click the Create Function at the bottom of the page. Lambda Create
  5. Edit and Deploy your code.
    • On the function page, you will see a visual overview of the function and an area at the bottom where you can write your code. By default, there will be a sample code in the code area. Notice that the function definition has an “event” parameter, this will be used later when testing the code. If you make changes to the code, remember to click the Deploy button afterwards. Lambda Function
  6. Setup a test event for your code.
    • Click on the Test button above the code area. This will require you to create an event. You will notice a JSON value in the Configure test event dialog, this will be the “event” value that will be passed as a parameter to your function.
    • Set an Event Name to identify your event.
    • Click the Create button. Lambda Test
  7. Test your code.
    • Click the Test button again to run the function. The function results will be shown in the Execution Results tab in the code section. Lambda Results

After creating our function, the next next thing we need to know is how to set up our function to respond to specific events.

How to setup a trigger event for Lambda functions

  1. Navigate back to your function page. Lambda Trigger
  2. Click on the “Add trigger” button in the Function overview section. This will prompt you to select a trigger configuration from a list of AWS services and a few other third-party services. Lambda Trigger
  3. Select trigger event. Depending on the trigger you select you may need to configure some settings before you can add the trigger. Lambda Trigger
  4. Click the Add button. The trigger should now appear in your function overview. That would be the event that would invoke your function.

Finally, after setting up our function and our trigger event, we need to know what data to expect from the event, in order for us to process the data and act accordingly.

Lambda functions accept 3 parameters: event, context, and callback. The callback parameter is only used when you are not using the “async” keyword for your function. The event parameter contains a JSON value based on the trigger event. To know what data to expect from each event, you can configure a test event and select an Event Template that matches the trigger event you just added. A sample JSON will be displayed showing you the format of the data you expect to receive in the “event” parameter of your function. Lambda Test

The context parameter contains information about the specific invocation of your Lambda function such as the AWS request ID, the memory allocated for the function, identity information, etc.

The callback parameter is a function object that receives 2 parameters an error message and a JSON response object.

In this article, it is my hope that it has given you a sense that AWS Lambda is not as difficult as it may initially seem. We have covered how to understand Lambda through creating functions, setting up triggers, and understanding Lambda function parameters. With AWS Lambda handling all the hardware resources behind-the-scenes all we really need to focus on is simply doing what we love - writing code.

For more information on AWS Lambda you can read the official documentation here.