-
Notifications
You must be signed in to change notification settings - Fork 4
Creating Azure Function with Javascript in Visual Studio Code
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.
- Node (+npm) installed
- VS Code
- Subscription on MS Azure
-
Open VS Code and install the
vs-code-azurefunctions
extension andazure-functions-core-tools@core
according to the this deployment tutorial. -
Sign in to the Azure subscription using the Azure Functions extension tab.
-
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.
-
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
-
Create a new Azure Function using the Azure Functions extension.
- Provide its name.
- Define its time interval pattern.
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
-
To start the app, run the generated task in the Debug tab.
-
To deploy Azure Function:
- Select the subscription in the Azure Functions extension tab.
- Select Create Function App.
- Go through the wizard.
Azure Functions are using app settings. If you want to develop locally, it is required to set up keys:
- Copy
local.settings.json.template
and rename it tolocal.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 } ...
- Set up the keys defined in the template.
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.
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).