Deploy and invoke Lambda functions in LocalStack using VS Code Extension
Learn how to deploy and invoke AWS Lambda functions on your local machine using LocalStack's VS Code Extension.
LocalStack is a cloud service emulator designed for local development and testing of cloud applications. With LocalStack's Lambda emulation, you can define and deploy Lambda functions locally, alongside other serverless components like DynamoDB, SQS, SNS, EventBridge, and more. The Lambda emulation in LocalStack offers enhanced developer features, including:
Ability to attach a remote debugger using an Integrated Development Environment (IDE)
Additional configuration like cold starts, concurrency, and more!
In the past year, we introduced the LocalStack VS Code Extension, enabling you to deploy and invoke Lambda functions directly from your code editor. This blog post provides a step-by-step guide on setting up the LocalStack VS Code Extension, utilizing a sample application deployed through the Serverless Application Model (SAM). Furthermore, we will delve into the process of viewing and managing locally created resources through the LocalStack Resource Browser.
Prerequisites
localstack
CLI with theLOCALSTACK_AUTH_TOKEN
Visual Studio Code &
code
CLI
Recruiting Agency application with SNS, SQS, DynamoDB, Lambda and S3
This demo uses a public sample to showcase an event-driven recruiting agency application. The system utilizes SNS topics, a DynamoDB table, SQS queues, Lambda functions, and S3 buckets. The application consists of three primary services:
An anti-corruption service that processes a change data capture (CDC) event stream.
This stream is converted into events and transmitted to the SNS
JobEvents.fifo
topic.Subscribed services receive and process these events asynchronously.
An analytics service with an SQS FIFO
AnalyticsJobEvents.fifo
queue linked to theSNS FIFO JobEvents.fifo
topic.- SQS FIFO serves as an event source for Lambda functions, storing events in an S3 bucket.
An inventory service with an SQS FIFO
InventoryJobEvents.fifo
queue connected to the SNS FIFOJobEvents.fifo
topic.- It monitors
JobCreated
andJobDeleted
events, storing data in a DynamoDB table with SNS filter policy assistance.
- It monitors
These resources are deployed using the Serverless Application Model (SAM). The LocalStack VS Code Extension is to be used for creating the Lambda functions and invoking them directly from the code editor. For testing the event-driven architecture, LocalStack Resource Browsers would be utilized to visualize the created application resources.
Start your LocalStack container
Launch the LocalStack container on your local machine using the specified command:
export LOCALSTACK_AUTH_TOKEN=<your-auth-token>
DEBUG=1 localstack start
Replace
<your-auth-token
with your LocalStack Auth Token to start the LocalStack Pro container.
Setting DEBUG=1
activates more verbose logs, enabling you to observe the event-driven architecture in action during the invocation of Lambda functions. Once initiated, you'll receive a confirmation output indicating that the LocalStack container is up and running.
__ _______ __ __
/ / ____ _________ _/ / ___// /_____ ______/ /__
/ / / __ \/ ___/ __ `/ /\__ \/ __/ __ `/ ___/ //_/
/ /___/ /_/ / /__/ /_/ / /___/ / /_/ /_/ / /__/ ,<
/_____/\____/\___/\__,_/_//____/\__/\__,_/\___/_/|_|
๐ป LocalStack CLI 3.1.0
๐ค Profile: default
[12:12:44] starting LocalStack in localstack.py:494
Docker mode ๐ณ
...
โโโ LocalStack Runtime Log (press CTRL-C to quit) โโโโ
LocalStack supervisor: starting
LocalStack supervisor: localstack process (PID 16) starting
LocalStack version: 3.1.1.dev20240224232735
LocalStack Docker container id: 73597f01d11a
LocalStack build date: 2024-02-26
LocalStack build git hash: 323c0f8
...
Set Up the application
To set up the application on your local machine, follow these commands:
git clone https://github.com/localstack-samples/sample-sam-sns-fifo-dynamodb-lambda.git
cd sample-sam-sns-fifo-dynamodb-lambda
Next, open Visual Studio Code in the current directory using:
code .
If you encounter an issue where
code
is not recognized as a command, refer to the VS Code documentation to add it to thePATH
. Alternatively, manually open the directory from your VS Code.
After setting up the project, you can now install the LocalStack extension to deploy and invoke Lambda functions directly from Visual Studio Code.
Install the LocalStack VS Code Extension
To install the LocalStack VS Code Extension, follow these steps:
Open Visual Studio Code and go to the Extensions icon in the Activity Bar on the side.
In the Extensions view, you'll find the most popular extensions on the VS Code Marketplace.
Enter LocalStack in the search bar to filter the Marketplace offerings.
Click on the Install button next to the LocalStack VS Code Extension to download and install it.
Once the installation is complete, you can proceed to deploy the Lambda function using the newly installed LocalStack VS Code Extension.
Deploy & invoke the Lambda function
To deploy and invoke the Lambda function, navigate to the anti-corruption-service
directory and open the app.py
file. Within the CodeLens, you'll find two options:
Deploy Lambda function
Invoke Lambda function
Click on Deploy Lambda function to set up local Lambdas with the running LocalStack container. The VS Code Extension supports either a CloudFormation or Serverless Application Model (SAM) deployment configuration.
In this project, use the SAM configuration available in the template.yaml
file in the root directory.
You'll be asked to provide a unique name for the CloudFormation stack. Use recruiting-agency
or your custom name and press Enter to confirm.
A notification will indicate the start of the Lambda function deployment, followed by another notification confirming its creation.
Return to your file and click on the Invoke Lambda function CodeLens. Choose the CloudFormation stack name, which can be either recruiting-agency
or the custom name chosen during deployment.
Next, select the specific Lambda function to invoke, in this case, recruiting-agency-AntiCorruptionFunction-*
.
LocalStack will initiate the invocation of the Lambda function. The invoked function logs should be displayed in the Output panel of the LocalStack VS Code Extension, which is automatically shown after invoking the Lambda function.
To check the LocalStack logs, either use localstack logs
or view container logs on Docker Desktop. The logs include detailed information about the Lambda function invocation, such as duration and AWS service interactions.
localstack logs
...
2024-03-04T07:21:15.300 DEBUG --- [ asgi_gw_3] l.s.lambda_.provider : Lambda invocation duration: 257.48ms
2024-03-04T07:21:15.308 INFO --- [ asgi_gw_3] localstack.request.aws : AWS lambda.Invoke => 200
...
2024-03-04T07:21:15.769 DEBUG --- [ asgi_gw_3] l.services.sqs.models : deleting message 91c9263f-1976-4cc2-9d98-11b98531f39e from queue arn:aws:sqs:us-east-1:000000000000:AnalyticsJobEvents.fifo
2024-03-04T07:21:15.814 INFO --- [ asgi_gw_3] localstack.request.aws : AWS dynamodb.UpdateItem => 200
...
The DEBUG
configuration variable allows you to access verbose logs, providing a comprehensive view of the architecture during the invocation of the Anti-Corruption Service, which serves as an event producer for the Inventory and Analytics service (event consumer).
Visualize the local resources
To view the AWS resources created locally, you can use the LocalStack Resource Browser. This browser offers an integrated interface, akin to the AWS Management Console, facilitating CRUD operations (Create-Read-Update-Delete) for managing local resources.
After invoking Lambda, navigate to the DynamoDB Resource Browser.
Select the InventoryTable resource, and then click on Items. You can now visualize job listings from the Inventory service using the InventoryJobEvents.fifo
SQS queue and the recruiting-agency-InventoryFunction-*
Lambda function.
You can also access the S3 Resource Browser and click on the recruiting-agency-analyticsbucket-* resource.
This bucket is utilized by the analytics service to store processed events from the recruiting-agency-AnalyticsFunction-*
Lambda function, which uses the AnalyticsJobEvents.fifo
SQS queue.
Similarly, you can leverage the Resource Browser for other functionalities such as querying logs in CloudWatch Logs Groups, verifying SNS subscriptions, and more!
Conclusion
The LocalStack VS Code Extension enhances the developer experience (DevX) by accelerating the development cycle for locally running Lambda functions. Using LocalStack, you can establish a developer environment that prioritises repeatability, reproducibility, and local testing โ an upfront investment that pays off in the long term. Testing application code and infrastructure deployments frequently is the key to improve quality of your cloud applications, and LocalStack enables you to do that โ right on your local machine!
The LocalStack VS Code Extension has a few limitations:
Presently, it supports only CloudFormation and Serverless Application Model.
Invocation of the Lambda function is restricted to the
us-east-1
region.It exclusively supports Python Lambdas with an empty payload invocation.
You can check out the source code for the LocalStack VS Code Extension, and share feedback/bug reports on our public issue tracker, as we continue to improve the extension experience.