Introduction to Function as a Service (FaaS) and AWS Lambda

The second part of our introduction to serverless series

Welcome to our second blog on the topic of serverless computing! This post will introduce you to Function as a Service (FaaS) and AWS Lambda. In a serverless architecture, a FaaS, such as AWS Lambda, Google Cloud Functions, and Microsoft Azure Functions, acts as an event handler and microservice, executing tasks in response to triggers like HTTP requests and database changes. For example, a database change when a new record is added or updated in a library’s catalog management system can automatically trigger newly added metadata to index, making them searchable for patrons. This post will focus on AWS Lambda. Lambda functions are like glue, connecting and communicating with other AWS servers and facilitating the exchange of information between components. 

Customers who use FaaS are only charged when the function executes and are not charged for idle computing time, making it a cost-effective, scalable, and flexible solution for ad hoc applications, such as image processing to improve image quality or extract, transform, and load (ETL) pipeline to extract metadata from digital books and materials.

In this post, you will learn the steps to create and deploy a simple AWS Lambda function in Python. This Lambda function receives an incoming event in a JSON format, processes the event content, and returns an appropriate output message. The complete program and deployment code example are available in the author’s GitHub repository. To make this process easier, we’ll cover both the AWS Console and AWS SAM Command Line Interface (AWS SAM CLI) methods.


More on this topic: Coding Small-Scale Projects with Big Impact


Diagram of a AWS lambda function
Diagram of a Lambda function

The event source for this Lambda function can be either another AWS service, such as AWS Simple Storage Service (S3), or an external service outside of AWS. For instance, when a file is uploaded to S3 for preservation, we may want to record its location in a database for future retrieval. Here’s how the process works in the above diagram:

  1. The S3 service sends an event containing file information to the Lambda function.
  2. The Lambda function processes the event and performs any necessary business logic.
  3. The Lambda function returns an output message.

Before you process, please have an AWS account and install AWS Command Line Interface (AWS CLI) and AWS SAM CLI.

AWS Console

  • Log in to the AWS Management Console and navigate to the AWS Lambda service.
  • Click on the “Create function” button.
  • Select “Author from scratch” and enter a name for your function.
  • Select “Python 3.8” as the runtime.
  • Click on the “Create function” button.
  • Under the “Code” tab, replace the existing code with the following Python code:
Screenshot of code to Python code to insert into AWS Lambda
  • Click the “Deploy” button.
  • Test the function by clicking on the “Test” button and then “Create new event.”
  • Use the default “Hello World” test event and replace the event JSON code with this code
  • Click on the “Save” button.
  • Verify that the test is saved successfully.
  • Click on the “Test” button, and you will see the output with the file location information.
The result

That is it. You have successfully created and deployed an AWS Lambda function using the AWS console!

AWS SAM CLI

Once you have successfully installed the AWS SAM CLI on your local machine, proceed to download the demo source code. The file will be in a zip format, so unzip it after download. 

  • Open a terminal and navigate to the “example1” folder within the unzipped directory. You should see the following folder structure:
Folder structure in the unzipped directory
  • Package and deploy your SAM application by running the following command:
    • sam build && sam deploy –guided
  • Follow the prompts to complete the deployment process, as shown below:
A screenshot of the deployment process
  • You will see the deployment was successful in a couple of seconds with information like below:
Screenshot of a successful deployment
  • Verify the function is working as expected by running the following command:
    • aws lambda invoke –function-name <The first value in the outputs shown above> –payload ‘{“body”:”{\”location\”: \”bucket/demo.txt\”}”}’ output.json
  • A “output.json” file should be created, containing the Lambda function return message indicating the file location information:
    • {“statusCode”: 200, “body”: “{\”message\”: \”File is located at: bucket/demo.txt\”}”}

By the end of this process, you have successfully created and deployed an AWS Lambda function using the AWS SAM CLI. 

Conclusion

In this blog, we walked you through the steps of creating and deploying an AWS Lambda function using both the AWS Console and AWS SAM CLI. We hope you now have a good understanding and practical experience of how to create an AWS Lambda function. In our next post, we will delve into serverless patterns and more complex serverless architectures, providing you with the knowledge you need to adopt existing patterns to address your specific use case and create your own serverless application.


Dimensions logo. "Dimensions Part of DIGITAL SCIENCE" written in black text next to a rainbow geometric shape.

Choice and LibTech Insights gratefully acknowledge our launch sponsor, Dimensions, a part of Digital Science.  Dimensions, is the largest linked research database available and provides a unique view across the whole research ecosystem from idea to impact.


Sign up for LibTech Insights (LTI) new post notifications and updates.

Interested in contributing to LTI? Send an email to Deb V. at Choice with your topic idea.

Leave a Reply

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