Create function using Visual Studio Code

Azure Functions lets you execute your code in a serverless environment without having to first create a VM or publish a web application.

 

This blog post shows you how to use Azure Functions extension for Visual Studio Code to create function on your local computer using Microsoft Visual Studio Code.

You then publish the function code to Azure from Visual Studio Code.

Prerequisites

 

Install the Azure Function extension

The Azure Functions extension is used to create, test, and deploy functions to Azure.

In Visual Studio Code, open Extensions and search for azure functions, or open this link in Visual Studio Code.

Select Install to install the extension to Visual Studio Code.

Restart Visual Studio Code and select the Azure icon on the Activity bar. You should see an Azure Functions area in the Side Bar.

Create an Azure Functions project

The Azure Functions project template in Visual Studio Code creates a project that can be published to a function app in Azure.

A function app lets you group functions as a logical unit for management, deployment, and sharing of resources.

In Visual Studio Code, select the Azure logo to display the Azure: Functions area, and then select the Create New Project icon.

Choose a location for your project workspace and choose Select.

Select the language for your function app project. In this article, JavaScript is used.

When prompted, choose Add to workspace.

Visual Studio Code creates the function app project in a new workspace. This project contains the host.json and local.settings.json configuration files, plus any language-specific project files. You also get a new Git repository in the project folder.

Create an HTTP triggered function

From Azure: Functions, choose the Create Function icon.

Select the folder with your function app project and select the HTTP trigger function template.

Type HTTPTrigger for the function name and press Enter, then select Anonymous authentication.

A function is created in your chosen language using the template for an HTTP-triggered function.

Sign in to Azure

Before you can publish your app, you must sign in to Azure.

In the Azure: Functions area, choose Sign in to Azure.

Publish the project to Azure

Visual Studio Code lets you publish your functions project directly to Azure. In the process, you create a function app and related resources in your Azure subscription.

The function app provides an execution context for your functions. The project is packaged and deployed to the new function app in your Azure subscription.

In the Azure: Functions area, select the Deploy to Function App icon.

Choose the project folder, which is your current workspace.

If you have more than one subscription, choose the one you want to host your function app, then choose + Create New Function App.

Type a globally unique name that identifies your function app and press Enter.

  1. Choose + Create New Resource Group, type a resource group name, like myResourceGroup, and press enter. You can also use an existing resource group.

  2. Choose

  3. Choose a location in a

  4. Function app creation starts after you choose your location. A notification is displayed after your function app is created and the deployment package is applied.

Select View Output in the notifications to view the creation and deployment results, including the Azure resources that you created.

Make a note of the URL of the new function app in Azure. You use this to test your function after the project is published to Azure.

Back in the Azure: Functions area, you see the new function app displayed under your subscription. When you expand this node, you see the functions in the function app, as well as application settings and function proxies.

Test your function in Azure

Copy the URL of the HTTP trigger from the Output panel. As before, make sure to add the query string ?name=<yourname> to the end of this URL and execute the request.

Paste this new URL for the HTTP request into your browser’s address bar. The following shows the response in the browser to the remote GET request returned by the function:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s