Create your first Azure Function with Visual Studio Code
This guide walks you through creating a serverless HTTP-triggered function in Azure using Visual Studio Code (VS Code) and the Azure Functions extension.
Note: You can also create Azure Functions using the Azure portal, Azure CLI, or other IDEs like Visual Studio.
Prerequisites
- Visual Studio Code: Install the latest version from code.visualstudio.com.
- Azure Functions Core Tools: Install the latest version. You can install it globally using npm:
$ npm install -g azure-functions-core-tools@4 --unsafe-perm true
- Azure Account: If you don't have an account, you can create a free account.
Step 1: Install the Azure Functions Extension for VS Code
Open VS Code and install the extension:
In VS Code, open the Extensions view (Ctrl+Shift+X or Cmd+Shift+X) and search for "Azure Functions". Click "Install" on the extension published by Microsoft.
After installation, you'll see the Azure icon in the Activity Bar. Click it to access the Azure Functions explorer.
Step 2: Create a New Project
Initialize a new Azure Functions project:
- In the Azure Functions explorer, click the "Create New Project..." button.
- Select a folder for your project.
- Choose a language for your project. For this guide, select JavaScript or TypeScript.
- Select the template for your first function. Choose HTTP trigger.
- Provide a name for your function (e.g.,
HttpExample
). - Choose an authorization level. For development, select Anonymous.
VS Code will create a new folder with the project files, including host.json
, local.settings.json
, and your function's folder (e.g., HttpExample/index.js
).
Step 3: Write your Function Code
Open and edit the function code:
Navigate to the HttpExample/index.js
(or index.ts
) file. You'll see boilerplate code that handles an HTTP request.
Here's an example of a simple HTTP-triggered function:
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
};
};
This function retrieves a name
from the query string or request body and returns a personalized greeting.
Step 4: Run the Function Locally
Start the Functions host:
Press F5 to start the Azure Functions host. VS Code will build and run your project locally.
Once the host is running, you'll see output in the Terminal window, including the URL for your HTTP-triggered function. It typically looks like this:
You can test this endpoint by opening your browser and navigating to the URL, adding a query parameter like ?name=Azure
, or by sending a POST request with a JSON body.
Example URL: http://localhost:7071/api/HttpExample?name=World
Step 5: Deploy to Azure
Deploy your function to Azure:
In the Azure Functions explorer, click the "Deploy to Function App..." button.
Follow the prompts to select your Azure subscription, create a new Function App (or select an existing one), and deploy your code.
Once deployed, you'll get a new URL for your function in Azure, which you can use to access it from anywhere.
Tip: For a production deployment, consider setting up Continuous Integration/Continuous Deployment (CI/CD) with services like Azure DevOps or GitHub Actions.
Next Steps
- Learn about HTTP trigger bindings.
- Explore other trigger types like Timer triggers and Queue Storage triggers.
- Discover how to monitor your functions using Application Insights.