AWS Lambda (Part 1) - Getting Started with Serverless

Only having a conceptual understanding of AWS Lambda I set out to learn more about it. The idea of this post is to gain the most basic understanding, a foundation if you will, of AWS Lambda in order to further build on and learn from this.

Before we get started, there are a few things I should mention; firstly, you will require an AWS account to follow along if you are interested, and secondly, everything I am showing here will be free if you are on a Free Tier account (please make sure you stay within these quotas though).

What is AWS Lambda

AWS Lambda allows you to run code (functions) without needing a server, hence it is serverless. This means I can have a simple Hello World example running on the internet somewhere without needing a server. This explanation obviously doesn’t nearly do AWS Lambda justice, but I want to keep it simple.

How to run said Hello World example

Let’s get started with a simple example to understand how this works.

  1. From the AWS console, navigate to the Functions page and select Create function (top right).
  2. From the Create function page; select “Author from scratch”, name the function and, optionally, select a different runtime; I selected Python 3.9. As for “Permissions” and “Advanced settings” you can leave them as is.
  3. Once the function is created the Function overview will display the function’s code, along with other pieces of information about the function.

This is an AWS Lambda Function in its simplest form. The next step would be to execute the function.

Run the Hello World function

An AWS Lambda function is only useful when it is run, so how do you run it? It is possible to trigger a Lambda function with a test event.

  1. Click on the Test button in the Code source section of the function page.
  2. From the Configure test event dialog you can name the test event while leaving the other options as is and then click the Save button (bottom right).

    The Event JSON section can also be safely ignored for the moment, this will become useful later when you want to pass properties to the function.
  3. Now with the Test event created, click on the Test button to trigger and execute the function.
    As can be seen below, the function executed and returned a response as defined in the function’s source code.

AWS Lambda real-world triggers

The event trigger above, which initiates and runs the function, is only one of many triggers which can be used as function triggers within the AWS Lambda ecosystem. Any number of triggers can trigger a function to run the code defined within it.

Perhaps a useful real world example will be to visit a URL and have the function execute, after which the response is returned to the browser. Using a HTTP request life-cycle / pipeline is always useful for me to help my understanding of a new technology or concept.

AWS has another service, AWS API Gateway, which can be used to receive HTTP requests and act as a trigger for our AWS Lambda function.

  1. From the Lambda function overview page, select the “Add trigger” button.

    As mentioned before, there are many triggers which can be used to trigger an AWS Lambda function and these can be seen from the dropdown list.
  2. To configure our trigger, let’s select the following options:
    a. From the first dropdown list, select the API Gateway;
    b. Create an API option from the “Create a new API or attach an existing one” section;
    c. HTTP API as API Type, and
    d. Set the Security as Open and click Add (bottom right).

Now that the API Gateway trigger has been added to the AWS Lambda function it can be used to trigger the function’s code.

The API endpoint will be presented in the Configuration → Triggers panel, and simply navigating to this URL using a browser will essentially instruct the API Gateway to trigger the Lambda function.

And that is pretty much how easy it is to have an AWS Lambda function execute from a HTTP request made from a browser to an API endpoint which triggers the Lambda function, which in turn, via AWS API Gateway, responds to the browser.

Update the Function

It is also fairly straight forward to update the function and the changes will be seen immediately once these are deployed.

To do this, you would need to:

To do this, you would need to:

  1. Navigate to the function overview and make a change to the response message.
  2. There will be a message notifying you that the changes have not been deploy.
  3. Click on the Deploy button.
  4. Once deployment is complete, head back to the browser and refresh the page

Resources

When creating a Lambda function or any serverless applications in general, there are several resources which are created during the process, many which are required, and some that are optional (optional in the sense that if the server application requires a database layer, you’ll have to add that).

These all work in unison to achieve our desired outcome. In the case of this simple example, the following resources were created:

  1. The actual Lambda function
  2. The API Gateway HTTP API resource
  3. The execution Permission and Role

Clean up

Before moving on, its is a good idea to clean up / remove the resources which have been created during the previous steps.

To delete a Lambda function

  1. Navigate to the Lambda function overview page.
  2. Choose Actions, Delete.
  3. In the Delete function dialog box, choose Delete.

To delete an API Gateway API

  1. Open the API Gateway page and select the API.
  2. Choose Actions, Delete.
  3. In the Delete API dialog box, choose Delete.

To delete the execution role

  1. Open the Roles page of the AWS Identity and Access Management (IAM) console.
  2. Select the function's role (my-function-role-<random>).
  3. Choose Delete role.
  4. In the Delete role dialog box, confirm the deletion and click Delete.

To delete the log group

Although these logs aren’t necessarily an AWS resource, it may be worth cleaning them up.

  1. Open the Log groups page of the CloudWatch console.
  2. Select the function's log group (/aws/lambda/myHelloWorldFunction).
  3. Choose Actions, Delete log group(s).
  4. In the Delete log group(s) dialog box, choose Delete.

Conclusion

This is all good and well, but it is not useful for a developer who may have several API endpoints as Lambda functions in a CRUD-like API. Surely there is an easier way to build serverless applications than having to navigate to multiple pages using the AWS Console.

And there is, its called the the AWS Serverless Application Model (SAM) which is an open-source framework for building serverless applications.

In part two we take a look at how you can progress to the next level using AWS SAM.


If you enjoyed the post, please consider to subscribe so that you receive future content in your inbox :)

Psssst, worried about sharing your email address and would rather want to hide it? Consider using a service I created to help with that: mailphantom.io

Also, if you have any questions, comments, or suggestions please feel free to Contact Me.