Skip to content

AWS Lambda

What is AWS Lambda?

AWS Lambda is a serverless compute service that allows you to run code without provisioning or managing servers. You simply upload your code, and Lambda takes care of everything required to run and scale the execution to meet demand.

Main Concepts

Functions

A Lambda function is the core of AWS Lambda. It is the piece of code you run in response to events. You can write functions in multiple languages, including Python, Node.js, Java, and Go.

Triggers

Lambda functions are typically triggered by events. These events can come from various AWS services like S3, DynamoDB, or API Gateway, or even from custom sources.

Execution Role

An execution role is an IAM role that grants AWS Lambda permissions to access other AWS resources. The role defines what actions Lambda can perform during the execution of a function.

Timeout

The maximum duration our function can run before Lambda stops it (default: 3 seconds; maximum: 15 minutes).

Memory Allocation

Lambda allows us to allocate memory between 128 MB and 10 GB. The CPU and network resources scale with memory.

Environment Variables

Key-value pairs used to store configuration settings for our functions without hardcoding them.

Networking

Lambda functions can run in a default environment or inside a Virtual Private Cloud (VPC) for secure access to private resources.

Versions and Aliases

Lambda supports the management of versions and aliases for a function. A version represents an immutable snapshot of your function’s code, while aliases can point to specific versions for deployment.

Layers

Lambda layers are a way to manage common code and libraries that you want to reuse across multiple functions. Layers are uploaded as zip files and can be attached to Lambda functions.

Working with AWS Lambda

Step 1: Setting Up AWS CLI

Before we begin using the AWS CLI, ensure that AWS CLI is installed and configured on your system.

Step 2: Create a Lambda Function

To create a Lambda function, we’ll write a simple function and upload it using the AWS CLI. Let’s assume you’ve created a simple Python function called lambda_function.py.

Example Python function:

def lambda_handler(event, context):
return 'Hello, World!'

Now, we will package the function and create the Lambda function.

  1. Create a deployment package:
Terminal window
zip function.zip lambda_function.py
  1. Create the Lambda function using the AWS CLI:
Terminal window
aws lambda create-function \
--function-name helloWorldFunction \
--zip-file fileb://function.zip \
--handler lambda_function.lambda_handler \
--runtime python3.8 \
--role arn:aws:iam::YOUR_ACCOUNT_ID:role/YOUR_EXECUTION_ROLE

Replace YOUR_ACCOUNT_ID and YOUR_EXECUTION_ROLE with appropriate values for your account and execution role.

Step 3: Invoke the Lambda Function

After creating the Lambda function, we can invoke it using the AWS CLI.

Terminal window
aws lambda invoke \
--function-name helloWorldFunction \
output.txt

This command will execute the function and store the result in the output.txt file.

Step 4: Update the Lambda Function

If we need to make changes to our function, we can update it by uploading a new deployment package. For example, if we modify lambda_function.py, we would:

  1. Create a new zip package:
Terminal window
zip function.zip lambda_function.py
  1. Update the function with the new code:
Terminal window
aws lambda update-function-code \
--function-name helloWorldFunction \
--zip-file fileb://function.zip

Step 5: Set up a Trigger for the Lambda Function

You can trigger a Lambda function in various ways. Let’s set up an S3 trigger as an example.

  1. First, create an S3 bucket:
Terminal window
aws s3 mb s3://my-trigger-bucket
  1. Add an S3 event trigger to the Lambda function:
Terminal window
aws lambda add-permission \
--function-name helloWorldFunction \
--principal s3.amazonaws.com \
--statement-id my-s3-trigger \
--action "lambda:InvokeFunction" \
--source-arn arn:aws:s3:::my-trigger-bucket
  1. Configure the S3 bucket to trigger the Lambda function on object creation (via the S3 console or CLI).

Step 6: Delete the Lambda Function

Once we are done with the function, we can delete it using the following command:

Terminal window
aws lambda delete-function --function-name helloWorldFunction

Conclusion

In this tutorial, we’ve learned the basics of AWS Lambda, including core concepts such as functions, triggers, execution roles. Then we used the AWS CLI to create, invoke, update, and delete Lambda functions, as well as configure a trigger.