Let's start with AWS Lambda
Payal Gupta
Posted on February 15, 2022
This post will cover the following content:
- What is AWS Lambda?
- Why should you use AWS Lambda?
- How to use AWS Lambda?
Let's get started...
What is AWS Lambda?
Lambda is a highly available, serverless, event-driven compute service that lets you run code without provisioning or managing servers or clusters. You can trigger Lambda from over 200 AWS services and software as a service (SaaS) applications, and only pay for what you use.
Why should you use AWS Lambda?
Lambda is best suited for shorter, event-driven workloads, since Lambda functions run for up to 15 minutes per invocation.
Also, when you use Lambda, you are only responsible for your code and Lambda will take care of the rest i.e, balance of memory, CPU, network, and other resources to run your code.
This would mean that you cannot log in to compute instances or customize the operating system on provided runtimes because Lambda will perform operational and administrative activities on your behalf, including managing capacity, monitoring, and logging your Lambda functions.
If you are looking to manage your own compute resources, you can use EC2 or EBS (Elastic Beanstalk) as per your requirements.
How to use AWS Lambda?
You can create, invoke, and manage your Lambda functions using any of the following interfaces:
AWS Management Console – Provides a web interface for you to access your functions.
AWS Command Line Interface (AWS CLI) – Provides commands for a broad set of AWS services, including Lambda, and is supported on Windows, macOS, and Linux.
AWS SDKs – Provide language-specific APIs and manage many of the connection details, such as signature calculation, request retry handling, and error handling.
AWS CloudFormation – Enables you to create templates that define your Lambda applications.
AWS Serverless Application Model (AWS SAM) – Provides templates and a CLI to configure and manage AWS serverless applications.
For demo purpose, I will be using AWS Management Console to get started with Lambda service.
So, let's create our first Lambda function using console...
- Go to Lambda Service in the console
- Go to Functions page
- Click Create Function. You should see the below flyout.
Now, in this flyout, there are various option that we should understand. Let me help you with that.
First, you need to choose one of the options out of the following to create a function
Author from scratch: This is the default code that Lambda creates to start with a simple "Hello World" example.
Use a blueprint: This option is to create a Lambda application with sample code and setup configuration for typical scenarios.
Container image: Choose this option if you have a container image which you would like to use to deploy a Lambda function
Browse serverless app repository: Choose this option for deploying serverless applications from the AWS Serverless Application Repository
For now, I will go with the first option "Author from scratch" and fill the basic information:
Function name: Enter a name that describes the purpose of your function. I've entered "Lambda-demo"
Runtime: Choose the language to use to write your function. Note that Lambda provides runtimes for .NET (PowerShell, C#), Go, Java, Node.js, Python, and Ruby. I'm selected "Node.js 14.x"
Architecture: Choose the instruction set architecture you want for your function code. Keeping default i.e, x86_64
Permissions: By default, Lambda will create an execution role with permissions to upload logs to Amazon CloudWatch Logs. You can customize this default role later when adding triggers.
You have three options to choose for Execution role.
Create a new role with basic Lambda permissions - Default option
Use an existing role - if you have an existing IAM role to use, you can choose this option
Create a new role from AWS policy templates - use this option if you would like to use AWS provided policy template for various services
I am going with the default option i.e, "Create a new role with basic Lambda permissions"
- After filling all the above details, Choose "Create function" in the bottom right corner.
Now, let's invoke the Lambda function which you created...
Steps to Invoke Lambda Function:
- Under Functions, select your lambda function which you want to test.
- Go to Actions on the top right corner -> select 'Test' from the drop down.
- You can invoke your lambda function with a test event.You can either choose a template that matches the service that triggers your function, or enter your event document in JSON.
I will be using 'hello world' event template provided by AWS.
- So, choose 'New event'
- For template, select 'Hello World' from the drop down
- Template should look like this:
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
- Provide a name to your event function e.g. 'demo-event'
- Now, click 'Save changes' and choose 'Test'
- You should see the Execution results as below:
We can see the 200 OK response, memory used, billing duration for lambda function, total duration, etc in the summary section.
Further, under Log Output, you can see the logs generated for the test event or you can click on the 'Click here' which will direct you to view the same logs under the corresponding CloudWatch log group.
Lambda Monitoring
You can view the monitoring details of a Lambda function under the 'Monitor' tab.
Lambda sends runtime metrics for your functions to Amazon CloudWatch. Logs all requests handled by your function and automatically stores logs generated by your code through Amazon CloudWatch Logs.
As you can see in the screenshots, you can also view these logs under CloudWatch console by clicking 'View logs in CloudWatch' option.
Execution Role
In the end, you can check the details for the Execution Role that grants the function permission to access AWS services and resources. In our demo, this role was created when we created the Lambda function above.
In order to check execution role details of your Lambda function, go to 'Configuration' tab -> then select "Permissions' from the left pane. You should see the details as follows:
View the resources and actions that your function has permission to access by choosing 'By action' or 'By resource' column.
That's all I wanted to cover in this blog. Hope it was helpful. Thank you.
Posted on February 15, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.