Creating Azure Function Apps with VS Code Extension and Azure CLI

Learn how to create Azure Function Apps using VS Code and CLI with this detailed, step-by-step guide.
parveensingh-headshot
Parveen
9 minutes read
July 13, 2024

Table of Content

Share this

Twitter
LinkedIn
Reddit

If you are looking to build a scalable, serverless application or service, then Azure Functions is a great option to consider. Azure Functions is a serverless compute service that enables you to run event-driven code on a highly scalable and available platform. In this post, you will walk through the process of creating your first Azure Function using both the VS Code extension and the Azure CLI.

What is Azure Functions?

Azure Functions is a serverless computing service provided by Microsoft as part of the Azure cloud platform. It allows developers to build event-driven applications that can be hosted on the Azure platform at scale. Azure Functions is built on top of the Azure App Service, which provides a platform for building, deploying, and managing web applications.

Benefits of using Azure Functions

Azure Functions offers several benefits to developers, including:

  • Scalability: Azure Functions is designed to scale automatically to meet the demands of your application.
  • Cost-Effectiveness: With Azure Functions, you only pay for the resources you use, making it a cost-effective solution for building serverless applications.
  • Ease of Use: Azure Functions provides a simple and intuitive interface for building and deploying serverless applications.

Creating Your First Azure Function using VS Code and Function Core CLI Tools

In this guide, you will learn how to create and deploy Azure Function Apps using two methods: the VS Code extension and the Azure CLI. We’ll start with the VS Code extension, covering the creation of a new Azure Function App, developing a simple HTTP trigger function, testing it locally, and deploying it to Azure.

Prerequisites

Before you begin, ensure you have the following prerequisites:

  • Azure Account: You need an active Azure account. If you don’t have one, you can create a free Azure account.
  • Visual Studio Code: Ensure that Visual Studio Code (VS Code) is installed on your machine. You can download and install it from the official VS Code website.
  • Azure Functions Extension for VS Code: Install the Azure Functions extension in VS Code from the VS Code Marketplace.
  • Azure CLI: Ensure that the Azure CLI is installed on your machine. Follow our comprehensive guide on setting up the Azure CLI, refer to our post on Getting Started with Azure CLI.
  • Function Core Tools: Install the Azure Functions Core Tools. Instructions can be found here.
  • Node.js Installed: Ensure you have Node.js installed on your machine. You can download and install it from the official Node.js website. Check the version by running node -v in your terminal.

Creating an Azure Function App using VS Code

Installing the Azure Functions Extension

  • Open VS Code and go to the Extensions view by clicking the Extensions icon in the Activity Bar on the side of the window or by pressing Ctrl+Shift+X :
  • Search for Azure Functions and click Install:

Creating a New Azure Function App

  • Open the VS Code Command Palette (Ctrl+Shift+P), search Azure Functions: Create New Project select Azure Functions: Create New Project:
  • Choose a directory for your project and select a language. Select JavaScript in this case:
  • Select Model v4 as the JavaScript programming model:
  • Select HTTP trigger for the template:

    HTTP triggers are a great way to start with Azure Functions, allowing you to create functions that respond to HTTP requests.
  • Provide and function name (e.g., HttpTriggerFunction):
  • In the newly created project, open the HttpTriggerFunction.js file:
    This file holds the example code for a HTTP trigger function. You can develop your function here. Next, you will learn how to test your functions locally before deploying them to Azure Function Apps.

Testing the Function Locally

  • Open the Terminal in VS Code by going to View > Terminal:
  • While in the root directory of your function app project, run the following command to start the function app locally:
    func start

    You will see the list of all the registered functions in the terminal.
  • Once the function app is running, open your browser and navigate to http://localhost:7071/api/HttpTriggerFunction?name=Azure to test the function. You should see a response message saying Hello, Azure!:

    After successfully testing your function locally, you will now deploy the function to Azure.

Deploying the Function to Azure

  • Open the VS Code Command Palette (Ctrl+Shift+P), search Azure Functions: Deploy to Function App select Azure Functions: Deploy to Function App:
  • Select your Azure subscription you would like to deploy the function app in.
  • On the next step, click on (+)Create new function app:
  • On the next step, enter a globally unique name for the function app and press Enter:

    Make sure the function app name is unique or your deployment will fail. Enter a string of random numbers and letters to make sure the function name is unique.
  • Select the runtime stack(e.g., Node.js 20 LTS) and region(e.g., East US) for your function app:
  • You will see the following output in the terminal under the AZURE tab upon successful deployment:
  • Navigate to the OUTPUT tab in the terminal to find the URL for your new function:
  • Ctrl+click on the URL to open it in the browser, you will see the following output:

By following these steps, you have created and deployed an Azure Function App using Visual Studio Code. You started by installing the Azure Functions extension and then created a new Azure Function App with an HTTP trigger. After developing a simple HTTP trigger function, you tested it locally to ensure it worked as expected. Finally, you deployed the function to Azure, making it accessible via a cloud-hosted URL.

Next, you will learn how to achieve the same results using the Azure CLI, providing an alternative method for creating and managing Azure Function Apps. This approach will help you understand the flexibility and power of Azure’s command-line tools for serverless development and deployment.

Creating an Azure Function App using Azure CLI

  • Open your terminal and run the following command to log in to your Azure account:
    az login
    This command will open a web browser window where you can sign in with your Azure account credentials. Once logged in, you can close the browser window.
  • Create a resource group by running the following command:
    az group create --name HttpFuncRg --location eastus
    This command creates a new resource group named myResourceGroup in the specified location. Resource groups help you manage and organize related Azure resources
  • Azure Functions require a storage account. Create on with the following command:
    az storage account create --name mystorageacc98d3e --location eastus --resource-group myResourceGroup --sku Standard_LRS
    Storage account name has to be globally unique. Make sure you replace mystorageacc98d3e with a unique storage account name.
  • Next, create the Function App in the resource group with the following command:
    az functionapp create --resource-group myResourceGroup --consumption-plan-location eastus --runtime node --functions-version 4 --name HttpFunc89d4 --storage-account mystorageacc98d3e
    Function App name has to be globally unique. Make sure you replace HttpFunc89d4 with a unique Function App name.
  • Next, navigate to the directory where you want to create the project and run:
    func init myFunctionProject --javascript

    This command initializes a new Azure Functions project in a directory named myFunctionProject with JavaScript as the programming language.
  • Navigate into your project directory using the following command:
    cd myFunctionProject
  • Create a new function with an HTTP trigger using the following command:
    func new
    This command will prompt you to select a template for the new function. Select HTTP trigger and provide a name for the function (e.g., HttpTriggerFunction).
  • While is your myFunctionProject directory, open the newly created HttpTriggerFunction.js file in VS Code by running the following command:
    code src/functions/HttpTriggerFunction.js
    This file holds the example code for a HTTP Trigger function. You can develop your function here. Next, you will learn how you can test your functions locally before deploying them to Azure Function Apps using the CLI.
  • While is your myFunctionProject directory, run the following command:
    func start

    This will start the function locally and display a list of all the registered functions.
  • Ctrl+click on the URL to open it in the browser, you will see the following output:
  • Next, deploy your function to Azure using the following command:
    func azure functionapp publish HttpFunc89d4
    Replace HttpFunc89d4 with the globally unique name you used to create your function app.
  • Ctrl+click on the URL to open it in the browser, you will see the following output:

You have now created and deployed an Azure Function App using the Azure CLI.

Conclusion

You have learned how to create and deploy Azure Function Apps using both the VS Code extension and the Azure CLI. These two methods provide flexibility and convenience, allowing you to choose the approach that best fits your workflow.

While this guide focused on creating a simple HTTP trigger function, Azure Functions supports many other triggers and bindings. These include triggers for events such as timer-based schedules, changes in storage accounts, messages in queues, and more. Each type of trigger and binding allows you to integrate Azure Functions with different services and create more complex, event-driven workflows.

If you want to delve deeper into the world of Azure Function triggers and bindings, check out this blogpost on Mastering Azure Function triggers and bindings. In that post, you’ll learn more about various triggers and bindings and see a practical example of using the timer trigger and Twilio binding to send SMS notifications.

Additional Resources and Useful Links

To further enhance your understanding and capabilities with Azure Functions, explore the following resources and links:

  • Azure Functions Documentation: Comprehensive documentation covering all aspects of Azure Functions, including triggers, bindings, and advanced configurations. Azure Functions Documentation
  • Azure Functions Core Tools: Learn more about the Azure Functions Core Tools, which allow you to run and test your functions locally. Azure Functions Core Tools

Stay wired with our newsletter!

Recommended Articles

Stay Up To Date with
Cloud News and Tutorials!

Subscribe to our weekly newsletter!

By entering your email address, you agree to our privacy policy.