September 4, 2021

Boto3 Lambda – Complete Tutorial

Share this

By Tuvshinsanaa Tuul

September 4, 2021

boto3, boto3-course, lambda

AWS Lambda is a Serverless, highly scalable, and cost-efficient computing service provided by Amazon Web Services (AWS) that allows you to execute code in the form of self-contained applications efficiently and flexibly. It can perform any computing task, from serving web pages and processing streams of data to calling APIs and integrating with other AWS and non-AWS services. The primary use case of AWS Lambda is AWS automation. This Boto3 Lambda tutorial covers working with AWS Lambda service using Python SDK for AWS (Boto3 library) to deploy AWS Lambda functions, update them, and manage permissions for other services to invoke the AWS Lambda function.

If you’re new to the Boto3 library, we encourage you to check out the Introduction to Boto3 library article.

Prerequisites

To start automating Amazon EC2 and making API calls to manage EBS volumes, snapshots, and AMIs, you must first configure your Python environment.

In general, here’s what you need to have installed:

  • Python 3
  • Boto3
  • AWS CLI tools

We also suggest you use aws-vault for managing access to multiple AWS environments.

How to connect to AWS Lambda API using Boto3?

The Boto3 library provides you with two ways to access APIs for managing AWS services:

  • The client that allows you to access the low-level API data. For example, you can access API response data in JSON format.
  • The resource that allows you to use AWS services in a higher-level object-oriented way. For more information on the topic, look at AWS CLI vs. botocore vs. Boto3.

Here’s how you can instantiate the Boto3 Lambda client to start working with Amazon EC2 APIs:

import boto3
AWS_REGION = "us-east-1"
ec2_client = boto3.client("lambda", region_name=AWS_REGION)

At this moment in time, the Boto3 library does not support Lambda resources.

How to create an AWS Lambda function using Boto3

To create a Lambda function using Boto3, you need to use thecreate_function() method of the Lambda Boto3 client. In addition, you need to zip your AWS Lambda function code and create a Lambda execution IAM role.

Following is the code for creating an IAM role which will later be used to execute a Lambda function.

import boto3
import json
iam = boto3.client('iam')
role_policy = {
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

response = iam.create_role(
  RoleName='LambdaBasicExecution',
  AssumeRolePolicyDocument=json.dumps(role_policy),
)
print(response)
Boto3 Lambda - Create Lambda execution role
Create a Lambda execution role

Here’s an example of a simple Lambda function that returns “Hello World.”

AWS Lambda functions need an entry point handler that accepts the arguments event and context. These variables store Lambda function event object and execution context in a Python dictionary, which can be easily serialized/deserialized to JSON object or JSON data.

import json
def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps('Hello World')
    }
Boto3 Lambda - Zip Python Code
Zip Python Code

The following code will create a new AWS Lambda function called helloWorldLambda.

Note: the Handler parameter is specifying the entry point for the Lambda function will be a function called lambda_handler in the handler.py file.

import boto3
iam_client = boto3.client('iam')
lambda_client = boto3.client('lambda')
with open('lambda.zip', 'rb') as f:
	zipped_code = f.read()
  
role = iam_client.get_role(RoleName='LambdaBasicExecution')
response = lambda_client.create_function(
    FunctionName='helloWorldLambda',
    Runtime='python3.9',
    Role=role['Role']['Arn'],
    Handler='handler.lambda_handler',
    Code=dict(ZipFile=zipped_code),
    Timeout=300, # Maximum allowable timeout
    # Set up Lambda function environment variables
    Environment={
        'Variables': {
            'Name': 'helloWorldLambda',
            'Environment': 'prod'
        }
    },
)
print(response)

Note: To deploy the Lambda function to the required AWS Region, you need to use the region_name argument in the Boto3 Lambda client, for example: lambda_client = boto3.client('lambda', region_name='us-west-2').

Boto3 Lambda - Create Lambda function
Create Lambda function using Boto3

To create a Lambda function zip archive from Python code, you need to use theshutil.make_archive() method. For example, consider the following project structure:

.
├── deploy.py
└── src
    └── index.py
1 directory, 2 files

Now, you can use the following code (deploy.py) to create a ZIP archive for your Lambda function and deploy it:

import os
import pathlib
import tempfile
import shutil
import boto3
AWS_REGION = 'us-east-1'
BASE = pathlib.Path().resolve()
LAMBDA_SRC = os.path.join(BASE, 'src')
IAM_CLIENT = boto3.client('iam')
LAMBDA_CLIENT = boto3.client('lambda', region_name=AWS_REGION)
with tempfile.TemporaryDirectory() as td:
    lambda_archive_path = shutil.make_archive(
        td,
        'zip',
        root_dir=LAMBDA_SRC
    )
    
    zipped_code = None
    
    with open(lambda_archive_path, 'rb') as f:
    	zipped_code = f.read()
    
    role = IAM_CLIENT.get_role(RoleName='LambdaBasicExecution')
    
    response = LAMBDA_CLIENT.create_function(
        FunctionName='helloWorldLambda',
        Runtime='python3.9',
        Role=role['Role']['Arn'],
        Handler='index.lambda_handler',
        Code=dict(ZipFile=zipped_code),
        Timeout=300,
        Environment={
            'Variables': {
                'Name': 'helloWorldLambda',
                'Environment': 'prod'
            }
        },
    )

As a result of the above code execution, you should see a new Lambda function in the AWS web console:

Boto3 Lambda - Created Lambda function
helloWorldLambda function

How to invoke the AWS Lambda function using Boto3

To invoke the Lambda function, you need to use the invoke() function of the Boto3 client. To send input to your Lambda function, you need to use the Payload argument, containing JSON string data. Data provided to the Payload argument is available in the Lambda function as an event argument of the Lambda handler function.

import boto3, json
lambda_client = boto3.client('lambda')
test_event = dict()
response = lambda_client.invoke(
  FunctionName='helloWorldLambda',
  Payload=json.dumps(test_event),
)
print(response['Payload'])
print(response['Payload'].read().decode("utf-8"))
Boto3 LambdaInvoke Lambda function
Invoke Lambda function

Lambda function version

You can use versions to manage the deployment of your functions. For example, you can publish a new version of a function for beta testing without affecting users of the stable production version. Lambda creates a new version of your function each time that you publish the function. The new version is a copy of the unpublished version of the function.

Create a Lambda function version

To create/publish a new Lambda function version, you need to use the publish_version() method of the Lambda client.

You can change the function code and settings only on the unpublished version of a function. When you publish a version, the code and settings are locked to maintain a consistent experience for users of that version.

The publish_version() function publishes a version of your function from the current snapshot of $LATEST.

import boto3
lambda_client = boto3.client('lambda')
response = lambda_client.publish_version(
    FunctionName='helloWorldLambda',
)
print(response)
Boto3 Lambda - Publish new Lambda version
Publish the new Lambda function version

Lambda function version alias

You can create one or more aliases for your Lambda function. A Lambda alias is like a pointer to a specific function version. Users can access the function version using the alias Amazon Resource Name (ARN). Alias names are unique for a given function.

Each Lambda function alias has a unique ARN. An alias can point only to a function version, not to another alias. You can update an alias to point it to a new function version.

Event sources such as Amazon Simple Storage Service (Amazon S3) invoke your Lambda function. These event sources maintain a mapping that identifies the function to invoke when events occur. If you specify a Lambda function alias in the mapping configuration, you don’t need to update the mapping when the function version changes.

Create a Lambda function version alias

Tocreate a Lambda function alias, you need to use the create_alias() method of the Lambda client.

To create an alias for the Lambda function, you must first publish a new version for Lambda.

import boto3
lambda_client = boto3.client('lambda')
response = lambda_client.create_alias(
    FunctionName='helloWorldLambda',
    Name='v1',
    FunctionVersion='1',
    Description='helloWorldLambda v1',
)
print(response)

List Lambda function aliases

To list Lambda function aliases, you need to use the list_aliases() method of the Lambda client. For each alias, the response includes information such as the alias ARN, description, alias name, and the function version to which it points.

You can optionally use the parameter FunctionVersion to filter out the result.

import boto3
lambda_client = boto3.client('lambda')
response = lambda_client.list_aliases(
    FunctionName='helloWorldLambda',
    FunctionVersion='1',
)
aliases = response['Aliases']
print([alias for alias in aliases])
Boto3 Lambda - List Lambda function aliases
List Lambda function aliases

Describe Lambda function

To describe the Lambda function, you need to use the get_function() method of the Lambda client. Response data will contain the configuration information of the Lambda function and a presigned URL link to the .zip file with the source code of your function.

Note: the URL is valid for 10 minutes. The configuration information is the same information you’ve provided as parameters when uploading the function.

import boto3
lambda_client = boto3.client('lambda')
response = lambda_client.get_function(
    FunctionName='helloWorldLambda'
)
print(response)
Boto3 Lambda - Get Lambda function
Describe Lambda function

Update Lambda function

Toupdate the Lambda function, you need to use the update_function_code() method of the Lambda client.

import boto3
lambda_client = boto3.client('lambda')
with open('lambda.zip', 'rb') as f:
    zipped_code = f.read()
response = lambda_client.update_function_code(
    FunctionName='helloWorldLambda',
    ZipFile=zipped_code
)
print(response)

To create a Lambda function zip archive from Python code, you need to use the shutil.make_archive() method.

import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
Boto3 Lambda - Update Lambda code
Update Lambda code

GrantInvokeLambda permission to AWS services

To grant permission to the AWS service to invoke the Lambda function, you need to use the add_permission() method of the Lambda client.

Permissions apply to the Amazon Resource Name (ARN) used to invoke the function, which can be unqualified (the unpublished version of the function), or includes a version or alias. If a client uses a version or alias to invoke a function, use the Qualifier parameter to apply permissions to that ARN.

This action adds a statement to a resource-based permissions policy for the function.

The following code will add permission for test-ap-s3-bucket S3 bucket to invoke helloWorldLambda Lambda function.

import boto3
lambda_client = boto3.client('lambda') 
response = lambda_client.add_permission(
    StatementId='S3InvokeHelloWorldLambda',
    FunctionName='helloWorldLambda',
    Action='lambda:InvokeLambda',
    Principal='s3.amazonaws.com',
    SourceArn='arn:aws:s3:::test-ap-s3-bucket/*',
)
print(response)
Boto3 Lambda - Add InvokeLambda permission to the S3 bucket
Add InvokeLambda permission to the S3 bucket
Boto3 Lambda - S3 Trigger added to the Lambda function
S3 Bucket trigger added to the Lambda function

Delete Lambda function

To delete the Lambda function, you need to use thedelete_function() method of the Lambda client.

import boto3
lambda_client = boto3.client('lambda')
response = lambda_client.delete_function(
    FunctionName='helloWorldLambda'
)
print(response)

Free hands-on AWS workshops

We recommend the following free AWS Lambda Workshops to get more hands-on experience on the subject:

Also, we recommend the following free AWS Step Functions Workshops:

From our experience, these are the best hands-on paid learning materials today related to Serverless, AWS Lambda, and Step Functions:

Summary

In this article, we’ve covered how to manage the AWS Lambda service using Python SDK for AWS (Boto3 library) to deploy, update and delete AWS Lambda functions.

Tuvshinsanaa Tuul

Hi, Tuvshinsanaa Tuul from Mongolia. I have a Bachelor of Information System. I'm a Software Engineer experienced with JavaScript, AWS, Python, and PHP/Laravel.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Related Posts

AWS Spot Instance – The most important information
AWS Elastic IP – Everything you need to know
AWS CloudTrail – The most important information
AWS Snowball – The most important information

Subscribe now to get the latest updates!

>