Skip to content

Creating Azure Function with Javascript in Visual Studio Code

edison23 edited this page Feb 11, 2020 · 1 revision

This section describes how to set up an environment to be able to develop Azure Functions in Visual Studio Code (VS Code) using JavaScript. You can take a look at the VS Code Azure Function repository where the function is loading data from the GitHub API, transforming them and storing them in Azure Table Storage.

Prerequisites

  1. Node (+npm) installed
  2. VS Code
  3. Subscription on MS Azure

Setting up the environment

  1. Open VS Code and install the vs-code-azurefunctions extension and azure-functions-core-tools@core according to the this deployment tutorial.

  2. Sign in to the Azure subscription using the Azure Functions extension tab.

  3. Create a local function app using the Azure Functions extension, which is basically a project for creating Azure apps. Follow the deployment tutorial.

    • Select JavaScript when creating a new project.
  4. Open the project folder and you will see the following structure:

    ├── .git - you have your project initialized by a new GitHub repository - just like using 'git init' command
    │   ├── ...
    ├── .gitignore - list of files ignored by Git
    ├── .vscode
    │   ├── extensions.json - recommended extensions for the current project
    │   ├── launch.json - configuration to launch Azure Function locally
    │   ├── settings.json - project configuration settings
    │   └── tasks.json - VS Code tasks definition
    ├── host.json - Azure host definition
    ├── local.settings.json - settings definition template
    └── proxies.json - proxies definition template
    
  5. Create a new Azure Function using the Azure Functions extension.

    This creates the following function structure in the folder named by the function name.

    ├── function.json - metadata about the function (type, interval, etc..)
    ├── index.js - function implementation template
    ├── readme.md - documentation template based on its type
    └── sample.dat - empty sample data file
    
  6. To start the app, run the generated task in the Debug tab.

  7. To deploy Azure Function:

    1. Select the subscription in the Azure Functions extension tab.
    2. Select Create Function App.
    3. Go through the wizard.

Setting up keys

Azure Functions are using app settings. If you want to develop locally, it is required to set up keys:

  1. Copy local.settings.json.template and rename it to local.settings.json.
    ...
      "Values": {
        ...
        "AzureWebJobsStorage": "", // connection string to Azure Table Storage
        "GITHUB_API_KEY" : "" // access token used when loading data from the GitHub API
        "KC_WEBHOOK_SECRET" : "", // Kentico Kontent secret for the webhook
        "TRAVIS_TOKEN" : "" // token used to trigger the site rebuild
      }
    ...
    
  2. Set up the keys defined in the template.

Using Azure Table Storage

If you want to store the data in local Azure Table Storage, it is required to install the local storage emulator. Then you can use the local settings file to define the connection string to Azure Table Storage. If you want to connect to the local emulated storage, use UseDevelopmentStorage=true as a value.

ℹ️ It is really useful to also install Azure Storage Explorer to be able to browse the storage (including the local emulated one).

❗️ It is not possible to update the Azure table as an output binding. When you want to make updates to Azure Table Storage, it is recommended to use Azure Storage SDK.

Sharing business login among functions

It is possible to use npm packages reference in projects. If you want to share npm packages among the functions you have in your Azure Function app, just run npm init in the root folder and install the packages you need. Then reference them in the functions. Or run npm init right in the function folders and do not share the packages (for example, in case of conflicting npm versions among the functions).

Analytics