• Guide Content

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:

Setting 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-tracing

Modifying Your Project

After initializing your project, change the directories into the sam-example-app folder created by SAM:

You 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

AWS Lambda Observability, Debugging, and Performance Made Easy with Lumigo

Lumigo is a serverless monitoring platform that lets developers effortlessly find Lambda cold starts, understand their impact, and fix them.

Lumigo can help you:

  • Solve cold starts easily obtain cold start-related metrics for your Lambda functions, including cold start %, average cold duration, and enabled provisioned concurrency. Generate real-time alerts on cold starts, so you’ll know instantly when a function is under-provisioned and can adjust provisioned concurrency.
  • Find and fix issues in seconds with visual debugging – Lumigo builds a virtual stack trace of all services participating in the transaction. Everything is displayed in a visual map that can be searched and filtered.
  • Automatic distributed tracing – with one click and no manual code changes, Lumigo visualizes your entire environment, including your Lambdas, other AWS services, and every API call and external SaaS service.
  • Identify and remove performance bottlenecks – see the end-to-end execution duration of each service, and which services run sequentially and in parallel. Lumigo automatically identifies your worst latency offenders, including AWS Lambda cold starts.
  • Serverless-specific smart alerts – using machine learning, Lumigo’s predictive analytics identifies and alerts on issues before they impact application performance or costs, including alerts about AWS Lambda cold starts.

Get a free account with Lumigo resolve Lambda issues in seconds.