AWS Toolkit for VS Code
User Guide

Creating a Serverless Application with the AWS Toolkit for Visual Studio Code

This example shows how to use the AWS Toolkit for Visual Studio Code to create a serverless application and then run and debug it.

Prerequisites

  • Be sure your system meets the the required prerequisites specified in Installing the Toolkit for VS Code.

  • In addition, install the AWS SAM CLI and its prerequisites. See AWS SAM CLI in the setup prerequisites. If Visual Studio Code is open when you perform these installations, you might need to close and reopen the editor.

  • Identify your default AWS Region in your AWS config file. For more information, see Configuration and Credential Files in the AWS CLI User Guide. If Visual Studio Code is open when you update your config file, you might need to close and reopen the editor.

  • After installing your language SDK, be sure to configure your toolchain.

Create a Serverless Application with the Toolkit for VS Code

This example shows how to create a serverless application with the Toolkit for VS Code by using the AWS Serverless Application Model (AWS SAM).

  1. To open the Command Palette, choose View, Command Palette, and then enter AWS.

  2. Choose AWS: Create new SAM Application.

    
                        Command palette dialog box.

    Note

    If the AWS SAM CLI isn't installed, you will get an error in the lower-right corner of the VS Code editor. If this happens, see the prerequisites described earlier.

  3. Choose the runtime for your SAM application. For this example, choose nodejs10.x.

    
                        Screenshot of the runtime selection list.
  4. Choose a location for your new project. You can use an existing workspace folder if one is open, Select a different folder that already exists, or create a new folder and select it. For this example, choose There are no workspace folders open to create a folder named MY-SAM-APP.

    
                        Screenshot showing the location selection dialog box.
  5. Enter a name for your new project. For this example, use my-sam-app-nodejs. After you press Enter, the Toolkit for VS Code takes a few moments to create the project.

    
                        Screenshot showing the dialog box where you enter a name for the SAM project.

When the project is created, your application is added to your current workspace. You should see it listed in the Explorer window.


                Screenshot showing the Explorer showing your new project.

Run and Debug Your Serverless Application

After the AWS SAM application is created, you can use CodeLens indicators to run and debug the application.

  1. From the hello-world directory in the project explorer, open app.js, if it isn't already open.

    
                        app.js file location in the AWS SAM project.
  2. Select the Run Locally CodeLens indicator.

    Note

    To supply event information, select the Configure indicator from the CodeLens line in your code, as shown.

    
                        CodeLens Menu

    If CodeLens indicators aren't visible, select the AWS Explorer icon in the Activity Bar to activate CodeLens.

    
                        AWS Explorer icon
  3. After the local run is complete, the output appears on the OUTPUT tab or the TERMINAL tab.

    
                        Output of running the application locally.

    Note

    For Windows users, if you encounter a Docker mounting error during this process, you might need to refresh the credentials for your shared drives (in Docker Settings). A Docker mounting error might look something like the following.

    Fetching lambci/lambda:nodejs10.x Docker container image...... 2019-07-12 13:36:58 Mounting C:\Users\<username>\AppData\Local\Temp\ ... as /var/task:ro,delegated inside runtime container Traceback (most recent call last): ... requests.exceptions.HTTPError: 500 Server Error: Internal Server Error ...
  4. Choose the Debug Locally CodeLens indicator to debug your application. When in debug mode, you can set breakpoints, step through each line, inspect variables, and change values just as you would for any application.