working with AWS Lambda – beginner guide

Published On: 4 March 2021.By .
  • Cloud

Hello Folks, If you are new to AWS lambda and don’t have any idea about what are lambda functions, this article is especially for you.  There is no prerequisite for this article. If you want to do it practically, then definitely you will need an AWS account. You can easily create an AWS account by following AWS console 

What is AWS Lambda?

AWS Lambda is an event-driven, serverless compute service that lets you run code without provisioning or managing servers. Lambda runs your code in stateless containers only when needed and scales automatically as per the demand, from a few requests per day to thousands per second.

What does Serverless mean?

Traditionally, we build and deploy our application on a server where we can control requests and manage resources. In this type of deployment, we have some degree of control and we will be charged for the resources we are using.

Whereas in serverless, the cloud provider dynamically allocates and manages all the resources used to run an application and we will be charged for the resources used in the execution of the code. once the code execution is completed, all resources are deallocated.

In serverless computing, we have to write the code(to be executed) in the form of a function. This function is also known as the Lambda function. Whatever code we want to execute, it will be executed inside a stateless container that can be triggered on an event. An event that can trigger a lambda function can be a HTTP request or a database event, queuing service, monitoring alert, file upload, cron job, etc. It can also be triggered by AWS Cognito user-pool. If you are working with AWS Cognito, you can find a list of events on which lambda functions can be triggered.

P.S. As in serverless, the code is in the form of a function, it is also referred as “Function as a Service” or in short, “FaaS”.

Steps to create a lambda function

Here are some steps you can follow in order to create your lambda function.

1. Login to aws console
2. Search keyword in services “Lambda functions”
3. Create your lambda function

In the steps to create a lambda function, you can select the runtime environment for your code like NodeJS, Java, Python, Ruby, Go, Rust, .net, etc.

Example of syncing Cognito user pool to MySQL DB

Here you can find sample code to sync your cognito userpool users with any database. Here we are using MySQL as a database and Python3.8 as a runtime environment.

Challenge
If you have a look at the above code, you can see that writing lambda functions is very straight forward. But here is a challenge and that is How your code will import pymysql, or from where you can find this package to upload to cloud providers so that your code can use this package.

Solution
You can create a virtual environment and install the package in that environment. From the environment, you can find the package and you can create a zip file that contains the pymysql package and your code. In the AWS console, you can find an option to upload the zip file of your lambda function.

Monitor Logs
You can see the logs in cloudwatch. You can find this option under the “Monitor” tab. Please refer the image attached below:

Related content

That’s all for this blog