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.
- From the AWS console, navigate to the Functions page and select Create function (top right).
- 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.
- 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.
- Click on the Test button in the Code source section of the function page.
- 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.
- 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.
- 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.
- 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:
- Navigate to the function overview and make a change to the response message.
- There will be a message notifying you that the changes have not been deploy.
- Click on the Deploy button.
- Once deployment is complete, head back to the browser and refresh the page
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:
- The actual Lambda function
- The API Gateway HTTP API resource
- The execution Permission and Role
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
- Navigate to the Lambda function overview page.
- Choose Actions, Delete.
- In the Delete function dialog box, choose Delete.
To delete an API Gateway API
- Open the API Gateway page and select the API.
- Choose Actions, Delete.
- In the Delete API dialog box, choose Delete.
To delete the execution role
- Open the Roles page of the AWS Identity and Access Management (IAM) console.
- Select the function's role (
- Choose Delete role.
- 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.
- Open the Log groups page of the CloudWatch console.
- Select the function's log group (
- Choose Actions, Delete log group(s).
- In the Delete log group(s) dialog box, choose Delete.
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.