Let’s run the “Hello World” example on AWS Lambda. I would recommend reading about the AWS Lambda – Serverless Programming before getting started with Lambda functions.
In this example, we will learn about the basics of Lambda, how to create a function, deploy without managing servers. You will also learn about the core vocabulary of AWS Lambda functions.
If you are a beginner then you can use the free tier of AWS. AWS free tier allows you a million request per month for AWS Lambda.
AWS Lambda Service
To get started with AWS Lambda, you must signup with AWS and login to the AWS Management Console. Now, go to the services page and you will see the “Lambda” link under the “Compute” section.
See the below screenshot for your reference:
Click on the Lambda link and you will see the AWS Lambda Home page like below screenshot:
This screen only appears when you are getting started for the first time. You can see the links in the left section. We will see them after creating a function to understand better.
Create Lambda Function
You can click on the create function link on the above screen only OR go to Dashboard and you will see the below screen.
Click on the link “Create function” and you will see the below screen.
AWS provides multiple ways to create the Lambda function:
Author from scratch
Author from scratch option will give you the option to choose every detail. This is the best start point for beginners as you would like to understand most of the settings and options. We will choose this option in this tutorial.
Use a blueprint
There are several sample lambda functions available on AWS with configurations presets for common use cases. So, if you are developing Lamda function for the business need then you can always check for your use case and choose the available blueprint.
Browse serverless app repository
AWS also has the repository for the Lambda function called serverless app repository. You can find a number of Lambda functions with all the code and configuration presets ready to deploy. You can choose one if it’s fitting in your use case.
Let’s choose the first option and author from scratch.
Let’s configure our first lambda function. Typically, a Lambda function consists of code, dependencies, and configurations.
Name of the function is the logical name which you usually create in programming to describe the purpose. Use only letters, numbers, hyphens, or underscores with no spaces.
You can choose runtime from supported runtime by AWS. A runtime is a version of a programming language or framework that you will use to write your function.
AWS Lambda supports the below runtime:
Note: You can create your own runtime as well.
You can create roles and permission to allow your Lambda function the access of other AWS service on behalf of you. In addition to the execution roles, your function also has the function policy that gives permissions to other functions to invoke it.
Let’s choose the option “Create a new role with basic Lambda permissions”. We will explore other options when we will create a Lambda function for AWS S3 object manipulations.
Click on the “Create function” button at the bottom of screen.
Explore Lambda Function
AWS will create your Lambda function in a few seconds. You will be redirected on the below page after successful creation of your function.
You can change the configurations for your Lambda function. For Example, you can set the request time out and memory. This is just to give you an idea, You can explore other settings as well.
Next is the function code, you will see the code editor and the index file open in it by default. You can change the code from the editor itself.
We have already discussed runtime in Lambda configuration. “A runtime is a version of a programming language or framework that you will use to write your function.”
The module-name.export value in your function. For example, “index.handler” calls exports.handler in index.js.
Configure Test Events
Now, It’s time to test your Lambda function. You can configure a test event by “Select a test event” dropdown. OR Just click on the “Test” button.
You can setup up to 10 test events for your function.
Once the test event has been configured, you can test your Lambda function. You will see that your event is available in dropdown now.
Run your Lambda Function
Now, we have already created a test event. Let’s do some changes in the Lambda function code and test the Lambda for the same.
Instead of printing hard-coded text, let’s print the key1 from test event.
Note: Never forget to save the code.
Now, click on the test button and you will see the below result as expected:
The Lambda Dashboard is divided into two parts:
- Resources – in the selected region. Total number of Lambda functions running in the selected region will display as count.
- Account-level metrics – This section will display all the metrices available for the lambda function running in the selected region. See the invocation metrice for the reference:
Delete Lambda Function
You can see all the running Lambda funtions by clicking the left menu option “Funtions”.
There is one Lambda function record in the list as we created only one. Let’s select this and try to delete this:
The point to notice here is that your Lambda function will be deleted permanently so make sure that you stored the code in some repository.
Click on the delete button to continue.