# Deploy and invoke Lambda functions in LocalStack using 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:

* [Hot reloading of Lambda functions with each code change](https://docs.localstack.cloud/user-guide/lambda-tools/hot-reloading/)
    
* [Ability to attach a remote debugger using an Integrated Development Environment (IDE)](https://docs.localstack.cloud/user-guide/lambda-tools/debugging/)
    
* [Persistence of function state using Cloud Pods](https://docs.localstack.cloud/user-guide/state-management/cloud-pods/)
    
* Additional configuration like [cold starts, concurrency, and more](https://docs.localstack.cloud/references/configuration/#lambda)!
    

In the past year, we introduced the [LocalStack VS Code Extension](https://github.com/localstack/localstack-vscode-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)](https://aws.amazon.com/serverless/sam/). Furthermore, we will delve into the process of viewing and managing locally created resources through the [LocalStack Resource Browser](https://docs.localstack.cloud/user-guide/web-application/resource-browser/).

## Prerequisites

* [`localstack` CLI](https://docs.localstack.cloud/getting-started/installation/#localstack-cli) with the [`LOCALSTACK_AUTH_TOKEN`](https://docs.localstack.cloud/getting-started/auth-token/)
    
* [LocalStack Web Application](https://app.localstack.cloud/sign-up)
    
* [Visual Studio Code](https://code.visualstudio.com/download) & `code` CLI
    
* [Serverless Application Model (SAM) CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) & [`samlocal` wrapper](https://github.com/localstack/aws-sam-cli-local?tab=readme-ov-file#installation)
    

## Recruiting Agency application with SNS, SQS, DynamoDB, Lambda and S3

This demo uses a [public sample](https://github.com/localstack-samples/sample-sam-sns-fifo-dynamodb-lambda) 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 the `SNS 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 FIFO `JobEvents.fifo` topic.
    
    * It monitors `JobCreated` and `JobDeleted` events, storing data in a DynamoDB table with SNS filter policy assistance.
        

![AWS Architecture Diagram](https://cdn.hashnode.com/res/hashnode/image/upload/v1709541731997/0bba1318-24cf-42c1-bf9b-e36a82326d6e.png align="center")

These resources are deployed using the [Serverless Application Model (SAM)](https://aws.amazon.com/serverless/sam/). The [LocalStack VS Code Extension](https://github.com/localstack/localstack-vscode-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](https://docs.localstack.cloud/user-guide/web-application/resource-browser/) 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:

```bash
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.

```bash
     __                     _______ __             __
    / /   ____  _________ _/ / ___// /_____ ______/ /__
   / /   / __ \/ ___/ __ `/ /\__ \/ __/ __ `/ ___/ //_/
  / /___/ /_/ / /__/ /_/ / /___/ / /_/ /_/ / /__/ ,<
 /_____/\____/\___/\__,_/_//____/\__/\__,_/\___/_/|_|

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

```bash
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:

```bash
code .
```

> If you encounter an issue where `code` is not recognized as a command, refer to the [VS Code documentation](https://code.visualstudio.com/docs/editor/command-line#_code-is-not-recognized-as-an-internal-or-external-command) to add it to the `PATH`. Alternatively, manually open the directory from your VS Code.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709541838093/22db1e6d-b190-48d7-b41d-e45ce2b62bbc.png align="center")

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](https://marketplace.visualstudio.com/items?itemName=LocalStack.localstack), follow these steps:

1. Open Visual Studio Code and go to the **Extensions** icon in the Activity Bar on the side.
    
2. In the **Extensions view**, you'll find the most popular extensions on the VS Code Marketplace.
    
3. Enter **LocalStack** in the search bar to filter the Marketplace offerings.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709542149131/843367ab-2524-4b36-be66-49621114881f.png align="center")
    
4. Click on the **Install** button next to the LocalStack VS Code Extension to download and install it.
    
5. Once the installation is complete, you can proceed to deploy the Lambda function using the newly installed LocalStack VS Code Extension.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709542176139/637f65c3-46d3-4341-936a-38c8427d70c7.png align="center")
    

### Deploy & invoke the Lambda function

To deploy and invoke the Lambda function, navigate to the `anti-corruption-service` directory and open the [`app.py`](http://app.py) file. Within the CodeLens, you'll find two options:

* **Deploy Lambda function**
    
* **Invoke Lambda function**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709542217434/9eb0d1a9-37a0-48dd-bb11-a74139651442.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709542228792/6f56e8a2-77f1-42a7-aa8f-cab860443a1d.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709542305045/cc2e81c7-bdc7-48aa-a182-0aefda444d72.png align="center")

A notification will indicate the start of the Lambda function deployment, followed by another notification confirming its creation.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709542386129/8959baa7-643c-45ac-8339-e8ed0e5ad2da.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709542341269/0e810029-2839-4fee-a3fa-a98c5f7f040d.png align="center")

Next, select the specific Lambda function to invoke, in this case, `recruiting-agency-AntiCorruptionFunction-*`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709542367931/65d4317e-bbbf-45fd-90d9-b8fe5b62ae81.png align="center")

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.

```bash
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](https://app.localstack.cloud/inst/default/status). 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](https://app.localstack.cloud/inst/default/resources/dynamodb).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709542449087/f3451607-560a-48b1-82ea-553e2926f275.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709542472393/d4e5af6d-c27b-4e10-a9b3-dd27f7aefe69.png align="center")

You can also access the [S3 Resource Browser](https://app.localstack.cloud/inst/default/resources/s3) and click on the **recruiting-agency-analyticsbucket-**\* resource.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709542482421/267677e4-0731-4070-a3b5-cf89f8fdc554.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1709542498999/1a17f99d-c0a4-4fe6-acb6-181ee389d4dd.png align="center")

Similarly, you can leverage the Resource Browser for other functionalities such as querying logs in [CloudWatch Logs Groups](https://app.localstack.cloud/inst/default/resources/cloudwatch/groups), verifying [SNS subscriptions](https://app.localstack.cloud/inst/default/resources/sns), 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:

1. Presently, it supports only CloudFormation and Serverless Application Model.
    
2. Invocation of the Lambda function is restricted to the `us-east-1` region.
    
3. It exclusively supports Python Lambdas with an empty payload invocation.
    

You can check out the [source code](https://github.com/localstack/localstack-vscode-extension) for the LocalStack VS Code Extension, and share feedback/bug reports on our [public issue tracker](https://github.com/localstack/localstack-vscode-extension/issues), as we continue to improve the extension experience.
