Posted on • Originally published atblog.cloudbuff.in on
Deploying our first function to Azure
In theprevious article, 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 toAzure website 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 typeresource groups
. Click onResource groups
in the Services section of the results.
Our subscription should automatically be selected on the creation form. Let's specifyblog-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 withEast US
.
Click onReview + create
to proceed. If everything is good, a "Validation Passed" message will be displayed.
Clicking onCreate
will create the Resource Group.
Create a Function App
Now, let's create a Function App. TypeFunction App
in the search field at the top and click onFunction App
in the Services section of the results.
Again, our subscription will be automatically selected. We will use theblog-functions-rg
Resource Group we just created and name our Function Appblog-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 chooseCode
as the Publish option. We will pick.NET
as the Runtime stack and6
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.
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 theConsumption
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.
Click onReview + create
to proceed. A confirmation screen will be displayed. ClickCreate
to create the Function App.
It may take some time for the deployment to be completed. Once it is complete, we will see a confirmation. Click onGo to resource
to view details.
Click on theFunctions
menu on the left. Currently, there are no functions under this Function App.
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, typestorage
in the search bar and click onStorage accounts
in the Service section of the search results.
We will choose the sameblog-functions-rg
Resource Group. We will useblogfunctionstorage
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 withStandard
option for Performance, since it will work for most scenarios and low latency is not really a requirement for us. ForRedundancy
we will opt forLocally-redundant storage (LRS)
as it is the lowest cost option and recommended for non-critical scenarios. For production environments, it is advisable to useZone-redundant
,Geo-redundant
orGeo-zone-redundant
option depending on your requirements and budget. ClickReview
button to proceed.
ClickCreate
to create the Storage Account.
Once the Storage account is created, we should see the details in the Portal.
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 onAccess keys
item in the left menu. That will display Access keys for the account. Click on the copy icon at the end ofConnection string
field.
Go to the Function App by searching forFunction App
in the menu. Click on the name of the Function App.
Select theConfiguration
item from left menu. Under theApplication Settings
tab, click on+ New application setting
link.
OnAdd/Edit application setting pop-up form enterAzureWebJobsStorage
as the name and paste the Storage account connection string that we copied earlier. Click OK.
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.
Deploy function
Now, we will switch over to Visual Studio Code, open the project that we createdpreviously and deploy our function. Click on the Azure icon in the left menu.
Click onSign in to Azure...
option. This will open a browser window. Login with your Azure credentials.
Upon successful login, we should seeSelect Subscriptions...
option in VS Code. Let's select our subscription from the drop list.
Open the Command Palette using keyboard shortcutCtrl + Shift + P
(useCmd + Shift + P
on Mac) orView > Command Palette...
menu option. TypeAzure Functions: Deploy
into text box and selectAzure Functions: Deploy to Function App...
option.
A list of Function Apps defined in Azure will be displayed. As we have created only one Function Appblog-function-app
, we should see just that in the list.
When we selectblog-function-app
for deployment, there will be a warning saying the previous deployment will be overwritten. We will clickDeploy
to proceed.
A Terminal window should open in VS Code and we should soon seeDeployment to "blog-function-app" completed.
message pop-up.
Our function code has been deployed successfully. If we go back to Function App in Azure Portal and click onFunctions
menu item, we should seeSimpleHttpFunction
in the list.
Now that our function is deployed, let make sure it works. Open a new browser or tab, put URLhttps://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.
Now let's modify the URL. Add aname
query parameter withJack
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.
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.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse