Deploying our first function to Azure
Tags: azure, serverless, azurefunctions
Published: 2022-10-01 13:14:08

---
title: Deploying our first function to Azure
published: true
date: 2022-10-01 08:17:40 UTC
tags: #azure #serverless #azurefunctions
canonical_url: https://blog.cloudbuff.in/azure-functions-part-3
---

In the [previous article](https://dev.to/sjejurkar/up-and-running-with-azure-functions-part-2-150d), we created a simple function with HTTP trigger and tested it locally. Now it's time to deploy it to Azure cloud platform.

## Create an Azure account

Before we can deploy our first function to Azure, we will need an Azure account. For this, we will go the to [Azure website](https://azure.microsoft.com) and sign up for a free account. Note that you will have to provide credit card information and some additional information depending on your location. Usually, Microsoft provides up to US$ 200 worth of Azure credit when you sign up.

## Create a Resource Group

Once you are logged into Azure, the first step is to create a Resource Group. This is simply a logical container for keeping related cloud resources together. Go to the search bar at the top and type `resource groups`. Click on `Resource groups` in the Services section of the results.

![01_portal_search_rg.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664457891019/ZDLqLex7-.png)

Our subscription should automatically be selected on the creation form. Let's specify `blog-functions-rg` as the name of the resource group and select a region. You can select any region you like. For e.g., you can choose the region your located in or a region close to you. We will go with `East US`. ![02_portal_create_rg.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664458012025/z75k03_UG.png)

Click on `Review + create` to proceed. If everything is good, a "Validation Passed" message will be displayed. ![03_portal_create_rg_valid.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664458227929/6AdM782ux.png)

Clicking on `Create` will create the Resource Group.

![04_portal_rg_details.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664458357136/JYJhc49A3.png)

## Create a Function App

Now, let's create a Function App. Type `Function App` in the search field at the top and click on`Function App` in the Services section of the results.

![05_portal_create_rg_fn.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664458417200/yJ5aItmAG.png)

Again, our subscription will be automatically selected. We will use the `blog-functions-rg` Resource Group we just created and name our Function App `blog-function-app`. Note that this name must be globally unique because Azure uses `<function-name>.azurewebsites.net` as the base URL for the Function App. So, you will have to choose a different name. We are going to deploy the function we created using Visual Studio Code, so we will choose `Code` as the Publish option. We will pick `.NET` as the Runtime stack and `6` as the version since that is what we used to create our function. We will select the same region as the Resource Group, `East US`, to deploy the Function App. ![06_portal_create_fn_app.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664458507544/FtMYoBZZo.png)

We will use Linux as the Operating System (Windows will work too, but we will go with Linux as it appears to be the more popular choice for deploying code in the cloud). There are several pricing plans available and we will look at all the available plans in more detail in a future article. For now, we will go with the `Consumption` plan as this is a true serverless plan. You are charged based on how many times the function is executed. Another nice thing with this plan is that first 1,000,000 executions are free. ![07_portal_create_fn_app_2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664458673068/B61ax7KDL.png)

Click on `Review + create` to proceed. A confirmation screen will be displayed. Click `Create` to create the Function App.

![09_portal_final_create_fn_app.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664472692166/OJyPm1UWN.png)

It may take some time for the deployment to be completed. Once it is complete, we will see a confirmation. Click on `Go to resource` to view details.

![10_portal_fn_app_confirm.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664459542902/soZphDpvi.png)

Click on the `Functions` menu on the left. Currently, there are no functions under this Function App.

![12_portal_fn_app_functions.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664472951386/imNX5X3fe.png)

## Create a Storage account

We must link a general purpose Azure Storage account to the Function App we just created because it relies on Azure Storage for operations like managing triggers and logging function executions. Since we don't have a Storage account, we will create one. In Azure Portal, type `storage` in the search bar and click on `Storage accounts` in the Service section of the search results.

![20_portal_storage_1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664555536698/QlIJd01l2.png)

We will choose the same `blog-functions-rg` Resource Group. We will use `blogfunctionstorage` as the name (note that we cannot use hyphen, underscore or any other special characters in the name). We will select the same region as the Resource Group, `East US`. We will go with `Standard` option for Performance, since it will work for most scenarios and low latency is not really a requirement for us. For `Redundancy` we will opt for `Locally-redundant storage (LRS)` as it is the lowest cost option and recommended for non-critical scenarios. For production environments, it is advisable to use `Zone-redundant`, `Geo-redundant` or `Geo-zone-redundant` option depending on your requirements and budget. Click `Review` button to proceed. ![20_portal_storage_2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664555545863/vJvh64Xha.png)

Click `Create` to create the Storage Account. ![20_portal_storage_3.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664557403531/HwKJE8S1k.png)

Once the Storage account is created, we should see the details in the Portal. ![20_portal_storage_4.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664557414475/w0K6u3EAF.png)

## Link the Storage account to Function App

Now let's link this Storage account to our Function App. For this, we first need to get the connection string for our Storage account. Click on `Access keys` item in the left menu. That will display Access keys for the account. Click on the copy icon at the end of `Connection string` field. ![20_portal_storage_5_keys.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664557659461/q51HOG72F.png)

Go to the Function App by searching for `Function App` in the menu. Click on the name of the Function App. ![21_portal_fn_navigate_00.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664558240592/m-E5JFM3T.png)

Select the `Configuration` item from left menu. Under the `Application Settings` tab, click on `+ New application setting` link. ![21_portal_fn_config.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664557712960/7bwYKD4e8.png)

On **Add/Edit application setting** pop-up form enter `AzureWebJobsStorage` as the name and paste the Storage account connection string that we copied earlier. Click OK.

![21_portal_fn_config_WebJobsStorage.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664558591540/2mtCCC054.png)

After the pop-up form closes, click on Save icon to save the new setting. We have now linked our Storage account with our Function App. ![21_portal_fn_config_save.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664558332681/vRq7809iF.png)

## Deploy function

Now, we will switch over to Visual Studio Code, open the project that we created [previously](azure-functions-part-2) and deploy our function. Click on the Azure icon in the left menu.

![13_VSCode_Azure_login.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664473050323/0loogf32l.png)

Click on `Sign in to Azure...` option. This will open a browser window. Login with your Azure credentials.  
 ![14_MS_sign_in.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664552904456/XAwp_sCDB.png)

Upon successful login, we should see `Select Subscriptions...` option in VS Code. Let's select our subscription from the drop list. ![15_VSCode_Azure_subsription.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664552912904/HXxq6il6P.png)

Open the Command Palette using keyboard shortcut `Ctrl + Shift + P` (use `Cmd + Shift + P` on Mac) or `View > Command Palette...` menu option. Type `Azure Functions: Deploy` into text box and select `Azure Functions: Deploy to Function App...` option. ![16_VSCode_deploy_function.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664553140554/lJQV6NWYB.png)

A list of Function Apps defined in Azure will be displayed. As we have created only one Function App `blog-function-app`, we should see just that in the list. ![17_VSCode_deploy_select_fapp.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664553148649/AgsvabezS.png)

When we select `blog-function-app` for deployment, there will be a warning saying the previous deployment will be overwritten. We will click `Deploy` to proceed.

![18_VSCode_deploy_fapp_warn.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664554113584/jgudFOm7c.png)

A Terminal window should open in VS Code and we should soon see `Deployment to "blog-function-app" completed.` message pop-up.  
 ![18-2_VSCode_deploy_fapp_done.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664558933331/2a8ae6RGv.png)

Our function code has been deployed successfully. If we go back to Function App in Azure Portal and click on `Functions` menu item, we should see `SimpleHttpFunction` in the list.

![21_portal_fn_view_deployed.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664559086570/8Mv8etaze.png)

Now that our function is deployed, let make sure it works. Open a new browser or tab, put URL `https://blog-function-app.azurewebsites.net/api/SimpleHttpFunction` into the address bar and press enter. If a message like the one below is displayed, it means our function was executed successfully. ![22_browser_invoke_1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664559094122/YqTYxf3Rr.png)

Now let's modify the URL. Add a `name` query parameter with `Jack` as the value:`https://blog-function-app.azurewebsites.net/api/SimpleHttpFunction?name=Jack`We will get a different response now. As you can see that both responses match what we saw when we tested the function locally. ![22_browser_invoke_2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664559102015/0YR6_AVHG.png)

And that's it!! We have successfully deployed our function to Azure.

## Conclusion

In this article, we deployed a simple function with HTTP trigger to Azure cloud. In the next article, we will create a function that gets triggered when a message arrives in a Storage Queue.