Follow me on Twitter
Connect on LinkedIn
What is AWS Lambda
AWS Lambda is a serverless computing service provided by AWS. It is a service that runs your code in response to an event and automatically manages the resources required for running your code. You don't need to worry about any underlying resources which are required.
One example of such an event is a file uploaded to the S3 bucket. You can trigger your lambda function when the file is uploaded to your S3 bucket. You can think of a data processing job where files are being uploaded to your bucket and you want to start processing as soon as files arrive in the bucket. This will help you achieve real-time data processing use cases.
Now that you know what the AWS lambda function is, let's create our first Lambda function.
Create the file named app.py and include the following code in it.
# This is a handler
def handler_name(event, context):
# Your logic
return True
You must be wondering what is handler_name function. Basically, the handler function is a method in your code that processes events. When you invoke your lambda function, lambda runs your handler method. For example, if you have configured an event for triggering the lambda function on file upload in an S3 bucket, upon successful upload of the file in a bucket, Lambda will run your method named handler_name .
Now that we know what the handler is, we need to know how we define the handler's name. It depends on 2 things.
- The filename in which your handler method is written
- Your handler method name
For example, the filename is app.py and the method name is lambda_handler , your lambda handler name will be app.lambda_handler .Basically filename.handler_method_name
When your Lambda function is invoked, lambda passes 2 arguments to your handler function. event and context (Lambda context)
event: The first argument is event which is the JSON document that contains data for your Lambda function to process
context: The context object provides methods and properties that provide information about the invocation, the function, and the runtime environment. For example, it contains a unique ID for each lambda invocation.
Let's assume event
data is {first_name: "Mayank", last_name: "Patel"}
. Now update your lambda_handler with the following code.
def lambda_handler(event, context):
message = 'Hello {} {}, this is from Lambda Function!'.format(event['first_name'], event['last_name'])
return {
'message' : message
}
Now, we need to build the deployment package.
Prepare a deployment package with no dependencies
Open your terminal and run the following commands
mkdir my_function
cd my_function
- Copy your
app.py
file in this folder -
zip lambda_deployment_package.zip app.py
This will generate your deployment package named lambda_deployment_package.zip in your project root directory
Prepare a deployment package with dependencies
Let's update the handler method with the following content which has a dependency on python requests library
import requests
def lambda_handler(event, context):
response = requests.get("https://www.example.com/")
print(response.text)
return response.text
Now, open your terminal and run the following commands.
mkdir my_function
cd my_function
- Install requests library in the
venv
folder using the following command pip install --target ./venv requests
- Now create the deployment package with installed library in venv
cd venv
zip -r ../lambda_deployment_package.zip .
- The above commands will generate your deployment package named lambda_deployment_package.zip in your project root directory
- Now add the
app.py
file to the root of your zip file using below command cd ..
zip lambda_deployment_package.zip app.py
After following the above steps, now you have your lambda deployment package ready locally. Now, it's time to deploy this package.
Follow the below steps to deploy your local lambda package.
There are 2 ways you can deploy your lambda function. Using AWS CLI or from the console.
Deploy using AWS CLI
Here, I assume you have configured AWS CLI.
Please create the execution policy. It gives your lambda function access to AWS resources.
aws iam create-role --role-name lambda-ex --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}
Now, you need to add permission to your role
aws iam attach-role-policy --role-name lambda-ex --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Now, run the below command to deploy your package.
aws lambda create-function --function-name my-function --zip-file fileb://lambda_deployment_package.zip --handler app.lambda_handler --runtime python3.9 --role arn:aws:iam::123456789012:role/lambda-ex
Here, replace 123456789012 with your AWS Account ID. You can get it by logging into the AWS console and clicking on your name in the top-right corner.
Deploy using AWS Console
Please follow the below steps to deploy your lambda function
- Login to your AWS console and search/open the AWS Lambda service
- Click
Create function
- Give input to Function name and select appropriate runtime. We will choose
python3.9
and hitCreate function
- Now, select your newly created function if not selected
- Under the code tab, click Upload from option. There are 2 ways to upload. Either you can upload the lambda package zip file from the local disk or you can upload your zip file to S3 and give S3 location. We will upload it from the local disk. Upload and hit Save
- Now, under the Runtime settings section, hit Edit and give your Handler name. In our case, it is
app.lambda_handler
and hit Save
Now, you can test your lambda function. However, there are some key settings that you may want to set for your function.
- Memory & Timeout: Under the Configuration tab -> General configuration, hit Edit update Memory and Timeout
- Concurrency: You can set concurrency for your lambda function under Concurrency sub-tab
Test the Lambda function
To test your newly deployed serverless lambda function, head to the Test sub-tab.
You can either Save your Test event by giving the name and providing input to Event JSON if applicable and hit Save.
If you don't want to Save and just want to Test, hit Test after giving input to Event JSON.
So far, I have covered basic lambda function package preparation, deployment, and setting key configuration.
Example Use Cases
- Realtime data processing: For example, if you want to process the data that you are receiving in the form of files, you can choose to upload files in the S3 bucket and trigger your lambda function as soon as the file arrives in the S3 bucket
- If you are developing the website and you want to host the back-end service on AWS Lambda function
- If you want to perform some sort of data analytics on top of the data you have in your warehouse
- Log analysis
- Document conversion
Benefits of Lambda
- No servers to manage
- Continuous scaling
- Increases innovation
- Pay-for-use billing model
- High availability
So far I have covered the basics of AWS lambda which include writing Lambda handler method, preparing lambda package with and without dependencies, and deploying lambda function using AWS CLI and from AWS Console.
There is more to it like configuring VPC, Retry behavior, Limitation of lambda package size, Lambda layering, etc.
I will be covering more details in my future blogs about AWS serverless service. If you want me to explain in detail or explain any of the use case, let me know in comment below.
Thank you for taking the time in reading my blog!
Top comments (0)