AWS – Creating serverless functions with AWS Lambda

How to Configure Network Static IP Address on Ubuntu 19.10

AWS Lambda is quickly becoming an indispensable resource for building systems in the cloud. It fits in nicely for several utility use cases, such as running code each time an object is dropped into an S3 bucket, but it can also be used to construct entire applications. Lambda lends itself well to the microservices mentality, which is an important piece of the DevOps puzzle. A microservice is a small, fully independent component of an application. Microservices generally have a dedicated code repository and a dedicated data store so that they can be deployed in isolation from any other related services. By decomposing your architecture down to the individual function level, you can maximize your compute resources by configuring, deploying, and scaling each function separately.

How to do it…

Follow these steps to create a simple AWS Lambda function that processes messages from an Amazon Simple Queue Service (SQS) queue. The ability to automatically process all the messages in a queue with a Lambda function is an incredibly valuable feature that was announced by AWS in 2018. Anyone who has written code to poll a queue for messages knows that it can be surprisingly hard to do correctly, especially at scale with distributed systems. Adding this tool to your belt will allow you to tackle a wide variety of architectural challenges:

  1. Go to the SQS dashboard and create a new queue. Name the queue MySampleQ, choose Standard Queue, and click Quick-Create Queue.
  2. Go to the AWS Lambda dashboard and click Create function.
  3. Choose Author from scratch and name the function MyQReader.


  1. Choose Node.js 8.10 as the runtime and click Create function.
  2. Scroll down to the function code and paste in the following:
exports.handler = async (event) => {
    // Log the entire event'event', JSON.stringify(event))
    let numRecordsProcessed = 0
    event.Records.forEach(record => {
        // Process the message from SQS here
    const response = {
        statusCode: 200,
        body: JSON.stringify(`${numRecordsProcessed} messages handled!`),
    return response;
  1. Your Lambda function needs additional privileges to interact with SQS. Scroll down to the execution role and click the role name to view it in the IAM console.
  2. Expand the policy and click Edit policy.
  3. Click the JSON tab and add the following policy statement to the array of statements that are already present in the policy. Note that the best practice is to replace the asterisk (*) in the Resource property with the actual ARN of your SQS queue:
            "Effect": "Allow",
            "Action": [
            "Resource": "*"
  1. Go back to your browser tab with the Lambda function.
  2. Scroll down to  Designer and select SQS from the event sources.
  3. Select MySampleQ from the dropdown under Configure triggers:

Configuring triggers
  1. Click Add and then Save the function.
  2. Open a new tab (leaving the Lambda tab open) and go to the SQS dashboard. 


  1. Select MySampleQ and, from the Queue Actions dropdown, select Send a Message:

Sending a message
  1. Type some text into the box and click Send M essage.
  2. Go back to the Lambda function and click the Monitoring tab. Click View logs in CloudWatch.


  1. On the CloudWatch page, click the latest log stream and check out the log messages that have been sent from your Lambda function to confirm that the message was successfully processed:

CloudWatch logs being sent from the Lambda function
  1. Delete the Lambda function and the SQS queue to avoid future charges.

Delegating work to a queue can drastically improve the perceived performance of your web applications, and there is no easier way to accomplish queue processing than to wire up a Lambda function to SQS. Using a queue also helps you decompose your application into decoupled, separately deployable components, which reduces risks when you’re making future changes.

How it works…

Lambda functions run in containers that are fully managed by AWS. All of the implementation details are hidden from you, and, in most cases, you don’t need to know how or where your function runs. Of course, a little bit of knowledge of what’s going on behind the scenes can help in some situations. For example, it’s important to understand the penalty you pay when your function is run for the first time. This is called a cold start, and it can add a significant amount of latency if there are no ready resources that have already been provisioned to run your function.

For a busy application, this is often not a problem, since frequent function invocations guarantee that for most requests, your function will be warm. There are certain steps you can take to optimize your functions with cold starts in mind, such as increasing the configured memory size and making sure that you create resources such as database connection pools outside of your function code so that you don’t pay the cold startup price every time your function runs.

There’s more…

While it’s possible to create and edit Lambda functions solely within the Lambda dashboard, it is highly recommended that you use a system such as the AWS Serverless Application Model (SAM) to structure your applications. Use SAM in combination with a version control system such as AWS CodeCommit so that you can maintain a revision history and share your code with colleagues.

SAM templates are a superset of AWS CloudFormation and use abbreviated commands that make creating Lambda functions much easier. The following screenshot shows an example SAM template that was created using AWS Cloud 9, an excellent web-based Integrated Development Environment (IDE) that you can run from your own account:

Cloud9 and the Serverless Application Model

SAM templates make it easier to create Lambda functions with Infrastructure as Code (IaC), and they should be your default way of allowing your development teams to create serverless microservices.

See also

  • Check out AWS CodeStar, a service that allows you to create a complete microservice or website, along with a code repository, development environment, and CI/CD pipeline, all in a single command that takes just a few minutes to run:

Comments are closed.