Developing Lambda functions locally with VS Code - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Developing Lambda functions locally with VS Code

You can move your Lambda functions from the Lambda console to Visual Studio Code, which provides a full development environment and allows you to use other local development options like Amazon SAM and Amazon CDK.

Key benefits of local development

While the Lambda console provides a quick way to edit and test functions, local development offers more advanced capabilities:

  • Advanced IDE features: Debugging, code completion, and refactoring tools

  • Offline development: Work and test changes locally before cloud deployment

  • Infrastructure as code integration: Seamless use with Amazon SAM, Amazon CDK, and Infrastructure Composer

  • Dependency management: Full control over function dependencies

Prerequisites

Before developing Lambda functions locally in VS Code, you must have:

Note

If you already have an Amazon account and profile configured locally, ensure that the AdministratorAccess managed policy is added to your configured Amazon profile.

Authentication and access control

To develop Lambda functions locally, you need Amazon credentials to securely access and manage Amazon resources on your behalf, just like they would in the cloud. The Amazon Toolkit for VS Code supports the following authentication methods:

The Amazon Toolkit for VS Code supports the following authentication methods:

  • IAM user long-term credentials

  • Temporary credentials from assumed roles

  • Identity federation

  • Amazon account root user credentials (not recommended)

This section guides you through obtaining and configuring these credentials using IAM user long-term credentials.

Get IAM Credentials

If you already have an IAM user with access keys, have both the access key ID and secret access key ready for the next section. If you don't have these keys, follow these steps to create them:

Note

You must use both the access key ID and secret access key together to authenticate your requests.

To create an IAM user and access keys:

  1. Open the IAM console at https://console.aws.amazon.com/iam/

  2. In the navigation pane, choose Users.

  3. Choose Create user.

  4. For User name, enter a name and choose Next.

  5. Under Set permissions, choose Attach policies directly.

  6. Select AdministratorAccess and choose Next.

  7. Choose Create user.

  8. In the success banner, choose View user.

  9. Choose Create access key.

  10. For Use case, select Local code.

  11. Select the confirmation check box and choose Next.

  12. (Optional) Enter a description tag value.

  13. Choose Create access key.

  14. Copy your access key and secret access key immediately. You won't be able to access the secret access key again after you leave this page.

Important

Never share your secret key or commit it to source control. Store these keys securely and delete them when no longer needed.

Note

For more information, see Create an IAM user in your Amazon account and Manage access keys for IAM users in the IAM User Guide.

Configure Amazon credentials using the Amazon Toolkit

The following table summarizes the credential setup process you will complete in the following procedure.

What to Do Why?
Open Sign In panel Start authentication
Use Command Palette, search for Amazon Add a New Connection Access the sign-in UI
Choose IAM Credential Use your access keys for programmatic access
Enter profile name, access key, secret key Provide credentials for connection
See Amazon Explorer update Confirm you're connected

Complete the following steps authenticate to your Amazon account:

  1. Open the Sign In panel in VS Code:

    1. To start the authentication process, select the Amazon icon in the left navigation pane or open the Command Palette (Cmd+Shift+P on Mac or Ctrl+Shift+P on Windows/Linux) and search for and select Amazon Add a New Connection.

  2. In the sign in panel, choose IAM Credentials and select Continue.

    Note

    To proceed, you will need to allow Amazon IDE Extensions for VS Code to access your data.

  3. Enter your profile name, access key ID, and secret access key, then select Continue.

  4. Verify the connection by checking the Amazon Explorer in VS Code for your Amazon services and resources.

For information on setting up authentication with long-term credentials, see Using long-term credentials to authenticate Amazon SDKs and tools.

For information about configuring authentication, see Amazon IAM credentials in the Amazon Toolkit for Visual Studio Code User Guide.

Moving from console to local development

Note

If you've made changes in the console, make sure you don't have any undeployed changes before transitioning to local development.

To move a Lambda function from the Lambda console to VS Code, complete the following steps:

  1. Open the Lambda console.

  2. Choose the name of your function.

  3. Select the Code source tab.

  4. Choose Open in Visual Studio Code.

    Note

    The Open in Visual Studio Code button is only available in Amazon Toolkit version 3.69.0 and later. If you have an earlier version of the Amazon Toolkit installed, you may see a Cannot open the handler message in VS Code. To resolve this, update your Amazon Toolkit to the latest version.

  5. When prompted, allow your browser to open VS Code.

When you open your function in VS Code, Lambda creates a local project with your function code in a temporary location that's designed for quick testing and deployment. This includes the function code, dependencies, and a basic project structure that you can use for local development.

For details on using Amazon in VS Code, see the Amazon Toolkit for Visual Studio Code User Guide.

Working with functions locally

After opening your function in VS Code, follow these steps to access and manage your functions:

  1. Select the Amazon icon in the sidebar to open the Amazon Explorer:

    Amazon Toolkit icon in VS Code sidebar
  2. In the Amazon Explorer, select the region with your Lambda function:

    Amazon Explorer showing region selection
  3. Under your selected region, expand the Lambda section to view and manage your functions:

    Lambda functions with action icons for deploy, invoke, and more

With your function opened in VS Code, you can:

  • Edit function code with full language support and code completion.

  • Test your function locally using the Amazon Toolkit.

  • Debug your function with breakpoints and variable inspection.

  • Deploy your updated function back to Amazon using the cloud icon.

  • Install and manage dependencies for your function.

For more information, see Working with Amazon Lambda functions in the Amazon Toolkit for Visual Studio Code User Guide.

Integrating with infrastructure as code

After moving your function to local development, you can integrate it with infrastructure as code tools:

After moving your function to VS Code, you can convert it to a Amazon SAM application. Select the icon next to your Lambda function in the Amazon Explorer to generate a Amazon SAM template from your existing function.

These tools provide additional capabilities for defining, testing, and deploying your serverless applications as infrastructure as code.

Next steps

To learn more about working with Lambda functions in VS Code, see the following resources: