Last updated:
AWS Lambda Local Development: The Basics and a Quick Tutorial
What Is AWS Lambda Local Development
AWS Lambda local development is a framework that enables developers to build, test, and debug lambda functions on their local machines. This local execution environment allows developers to mimic AWS Lambda’s behavior before deploying functions to the cloud, ensuring that code will behave as expected when executed in the AWS environment.
Using AWS Lambda local development saves time and costs associated with repetitive deployments for testing, reducing the development cycle. It also isolates the development process from potential network issues, enabling a workflow that closely aligns with production environments.
This is part of a series of articles about serverless debugging.
AWS Lambda Local Development Features
By building systems locally with AWS Lambda, developers can take advantage of the following features.
Custom Backend Services
AWS Lambda local development allows the creation of custom backend services without the need for a server. Developers can focus on writing code for their chosen tasks and events, ensuring a microservices approach to back-end development. It simplifies the process of building isolated, single-function back-ends that can easily scale with demand.
The ability to simulate AWS Lambda’s environment locally ensures that everything works correctly before going live. This environment reduces potential debugging efforts by catching issues in the early stages of development, making the development process more efficient.
Bring Your Own Code
With AWS Lambda local development, developers can use their existing code, leveraging their preferred programming languages such as Python, Node.js, Go, or others supported by AWS Lambda. This allows for a smoother transition and integration of existing projects without the need to rewrite code.
Developers can use familiar development tools and approaches, ensuring that their code is up to their quality standards before deployment. This also allows for easier onboarding of new team members, as they can work within a familiar environment, reducing the learning curve associated with new tools.
Built-in Fault Tolerance
An AWS Lambda development setup provides built-in fault tolerance mechanisms that can be tested locally. This ensures that functions handle failures and recover appropriately, which is crucial for maintaining service reliability and performance. These features include automatic retries, error handling, and tracing capabilities that mirror the AWS cloud environment.
Testing fault tolerance locally allows developers to validate their disaster recovery strategies and service reliability. By simulating conditions such as network failures or invalid input, developers can ensure that their functions remain operational under various failure scenarios.
Fine-Grained Control Over Performance
Developers have control over the performance of their functions when working locally. This includes setting memory allocations, controlling function timeouts, and managing execution roles. Such controls help optimize functions for cost and performance, ensuring that they run efficiently both locally and when deployed to AWS Lambda.
By tuning performance parameters during local development, developers can anticipate how their functions will perform in production. This control helps in achieving optimal resource utilization and cost savings by identifying and solving performance bottlenecks during the initial stages of development.
Related content: Read our guide to AWS lambda testing
What Is AWS SAM?
AWS Serverless Application Model (SAM) is an open-source framework that simplifies building serverless applications on AWS. It allows developers to describe the structure of their serverless applications in a simple manner using SAM templates. These templates define AWS resources needed for the application, allowing for rapid deployment and easy management.
Using AWS SAM, the deployment process becomes simpler through version control and continuous integration pipelines. AWS SAM’s CLI offers functionalities like local debugging and testing, which closely emulate the AWS environment, reducing the lag between development and production stages. This leads to more reliable and maintainable serverless applications.
Quick Tutorial: Local AWS Lambda Development with AWS SAM
Here’s a guide to developing AWS Lambda functions locally.
Prerequisites
For local Lambda development, you will need to have the following installed on your system:
- docker: To mimic the lambda runtime on your machine
- python: Version 3.6 or higher
- pipenv: To manage python dependencies in a virtual environment
You can install pipenv by running the following command:
pip install pipenvSetting Up AWS SAM
Here are the steps to install and set up the SAM CLI on your machine:
1. Create a project directory:
mkdir sam-demo
cd sam-demo
2. Initialize a virtual environment using pipenv:
pipenv shell
pipenv will launch a subshell in a virtual environment. To exit this subshell, type exit. Don’t forget to do this once you’re done.
3. Install the SAM CLI within the virtual environment:
pipenv install aws-sam-cli
Initializing a SAM Project
Once the SAM CLI is installed, you can use the sam init command to initialize the SAM project. Use the command below to create a project based on the hello-world template, which includes a Lambda function and a REST API. This tutorial uses the function only, not the API.
sam init \
--name sam-example-app \
--app-template hello-world \
--runtime python3.9 \
--dependency-manager pip \
--package-type Zip \
--no-tracingModifying Your Project
After initializing your project, change the directories into the sam-example-app folder created by SAM:
cd sam-example-appYou can use and edit the files in this ‘hello-world’ example to meet your needs. For example, you can modify the template.yaml file to add extra serverless resource blocks. You can also modify the code inside the Lambda function’s python file: hello_world/app.py.
Building and Running Your Lambda Function
Now that you have set up and customized your SAM project, you can build and run the Lambda function code using these commands:
1. Build your lambda function:
sam build –use-container
2. Invoke your lambda function locally:
sam local invoke
Monitoring AWS Lambda with Dash0
Dash0 is an OpenTelemetry-native observability platform. The Dash0 Lambda Extension ships as a Lambda layer and captures traces, logs, and metrics from Python, Node.js, and Java functions without code changes. It also records the failures that standard OpenTelemetry instrumentation cannot see:
- Timeouts and out-of-memory errors: The extension creates synthetic traces for invocations that AWS terminates before your handler can emit a span.
- Cold starts: Every invocation records whether it was a cold start, and the initialization duration is available as a span attribute and as a metric.
- Trigger context: Traces include the invocation ID, the event source, and trigger details for SQS, DynamoDB Streams, Kinesis, SNS, and EventBridge.
- Log correlation: Logs are linked to the trace of the invocation that wrote them.
Functions that already use the OpenTelemetry SDK can send their telemetry to Dash0 directly. Because the data follows OpenTelemetry semantic conventions, Lambda spans connect to the rest of your traces.
Start a free trial of Dash0, or read how to set up AWS Lambda monitoring.
