Microsoft Docs

Documentation, how-to, and learning resources.

Develop Azure Functions with Visual Studio Code

Visual Studio Code (VS Code) is a lightweight, powerful, and extensible source-code editor that runs on your desktop and is available for Windows, macOS, and Linux. It's a popular choice for developing Azure Functions because of its excellent JavaScript, TypeScript, and C# support, along with its vast ecosystem of extensions.

This article will guide you through setting up your development environment, creating your first Azure Function using VS Code, debugging it locally, and deploying it to Azure.

Prerequisites

1. Install the Azure Functions Extension for VS Code

Open VS Code and navigate to the Extensions view by clicking the Extensions icon on the sidebar or pressing Ctrl+Shift+X (Windows/Linux) or Cmd+Shift+X (macOS).

  1. Search for "Azure Functions".
  2. Click "Install" on the official Microsoft extension.

This extension provides a rich set of features for developing Azure Functions, including project creation, debugging, and deployment.

2. Create a New Azure Functions Project

Once the extension is installed:

  1. Open the Azure view in VS Code (click the Azure icon on the sidebar).
  2. In the "Workspace (Local)" section, click the "Create New Project..." button.
  3. Choose a folder for your project and click "Select Folder".
  4. Select your preferred language for the function (e.g., JavaScript, C#, Python). Let's choose JavaScript for this example.
  5. Select a template for your first function (e.g., "HTTP trigger").
  6. Provide a name for your function (e.g., MyHttpTrigger).
  7. Choose an authorization level (e.g., "Anonymous" for easy testing).

VS Code will create a new project with necessary files, including host.json, local.settings.json, and your function's code file (e.g., MyHttpTrigger/index.js).

Note: local.settings.json is used for local development settings, including connection strings and app settings. It's not checked into source control by default.

3. Develop Your Function Code

Open your function's code file (e.g., MyHttpTrigger/index.js) and you'll see something like this for an HTTP trigger:

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? 'Hello, ' + name + '!'
        : 'This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.';

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: responseMessage
    };
};

4. Run and Debug Locally

To run your function locally:

  1. Press F5 or click the "Run and Debug" icon in the sidebar and select "Start Debugging".
  2. The Azure Functions Core Tools will start, and your function app will be hosted locally.
  3. You'll see output in the Debug Console, including the URL for your HTTP trigger function.

You can then open a web browser or use tools like curl or Postman to send requests to your local function.

For example, to test the HTTP trigger:

curl http://localhost:7071/api/MyHttpTrigger?name=AzureFunctions
Tip: You can set breakpoints in your code and use VS Code's debugging tools (step over, step into, inspect variables) to troubleshoot your function locally.

5. Deploy to Azure

Once you're satisfied with your function's local performance:

  1. Ensure you are logged into your Azure account in VS Code (use the Azure extension's "Sign in to Azure" option).
  2. In the Azure Functions extension sidebar, click the "Deploy to Function App..." button.
  3. Follow the prompts:
    • Select your Azure subscription.
    • Choose "Create new Function App in Azure..." (or select an existing one).
    • Provide a globally unique name for your Function App.
    • Select a runtime stack (e.g., Node.js).
    • Select a region.

VS Code will package your code, create the necessary resources in Azure, and deploy your function. The output window will show the deployment progress and provide the URL of your deployed function.

Conclusion

Visual Studio Code, combined with the Azure Functions extension and Core Tools, provides a seamless and efficient development experience for Azure Functions. You can now build, test, and deploy serverless applications with ease.

Next Steps: