AWS Lambda, offered by Amazon Web Services (AWS), is a Serverless, scalable, and cost-efficient computing service that simplifies code execution in various applications. This Boto3 Lambda tutorial will guide you through deploying, updating, and managing AWS Lambda functions using the Python SDK (Boto3 library). Additionally, it will provide basic examples for beginners, explain how to integrate with API Gateway and address common questions. This comprehensive guide is ideal for automating AWS processes using Python and Boto3.

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.

Connecting 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:

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

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

Basic Examples for Beginners

For those new to AWS Lambda and Boto3, this section provides simple, step-by-step examples to get you started:

Creating a Simple Lambda Function

To create a Lambda function using Boto3, you need to use the create_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)

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')
    }

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

Note: the Handler parameter specifies that 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,
    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').

To create a Lambda function zip archive from Python code, you need to use the shutil.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'
            }
        },
    )

Basic Invocation of a Lambda Function

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'].read().decode("utf-8"))

Lambda function versions

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 you publish it. The new version is a copy of the unpublished version of the function.

Creating a 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.

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)

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.

Creating an 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])

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)

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)

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)

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)

Summary

This comprehensive tutorial on AWS Lambda using Boto3 in Python covered key aspects from basic function creation and invocation to advanced management techniques. We introduced beginners to Lambda functions with simple examples and answered frequently asked questions. Our step-by-step guide with code snippets and clear explanations aimed to simplify your journey into AWS automation using Python and Boto3. Whether you’re a beginner or an experienced developer, this tutorial is a practical reference for leveraging AWS Lambda’s full potential.