Azure Functions AKS DevOps
Build and deploy you Azure Function as Containers
Use Azure DevOps to build your Azure Functions as Docker Containers push them to Azure Container Registry (ACR) and deploy them to Azure Kubernetes Service (AKS).
While running our Azure Functions on Consumption Plan in Azure is a great way to host our functions, adding Docker into the mix gives us the flexibility of running our microservices either on-premise or Cloud.
The base images that Microsoft provide are available in Docker Hub, here.
In this post we’ll be creating a Node.js Azure Function and use Azure DevOps to build the function as a Docker image, push it to Azure Container Registry and finally deploy it to a Kubernetes cluster on AKS.
Note: We’ll be using the default HTTP endpoint added when creating the function
To follow along with this post you’ll need the following:
Create Azure Resources
This first thing we need to do is create the Azure Resources that we’re going to need so let’s head to the Azure Portal and launch Cloud Shell with a PowerShell Environment so we can get started.
1 – Create a new Resource Group by running New-AzResourceGroup -Name <RG Name> – Location <Azure Location for RG>. Please update the values between the <> with the name and location of your choosing.
2 – Create a Container Registry in our Resource Group by running New-AzContainerRegistry -ResourceGroupName <RG Name> -Name <Registry Name> -EnableAdminUser -Sku Basic. Update the values between the <> with the name of Resource Group previously created and a name of your choice for the Container Registry.
3 – Create a Kubernetes Cluster on AKS by running New-AzAksCluster -ResourceGroupName <RG Name> –Name <Cluster Name> -NodeCount 1 -Kubernetes $version -GenerateSshKey -location <Azure Location for Cluster>. Update the values between the <> with the name of Resource Group previously created and the name and location of your choosing. This step will take about 10-15 minutes.
Note: Refer to the naming rules and restrictions for Azure Resources here to help pick an appropriate name for the ACR and AKS resources.
Create Function App
Now that we have our Azure Resources created let’s create our Function
1 – Create a new git repository in our Azure DevOps project by navigating to Repos, expanding repos and clicking New repository from the drop down menu.
2 – Enter the name of your choice for the new git repository and click create.
3 – Click the Clone button in the new repository and then click Clone in VS Code. When prompted select a local folder on your workstation that the git repository will be cloned into.
4 – In VS Code go to the File menu and click Open folder. When prompted select the folder that you specified in the previous step.
5 – Select Terminal from the top menu and click New terminal to open a new terminal pane in VS Code.
7 – Add an HTTP Trigger to our Function project by running func new –name HttpExample –template “HTTP Trigger” in the terminal in VS Code.
8 – Click the Azure icon in VS Code and click yes when prompted to Initialize for optimal use with VS Code.
9 – Hit F5 in VS Code and click Debug anyway to our your function. Now we can open a new browser window/tab and access our function on http://localhost:7071/api/HttpExample.
10 – Press Shift + F5 to stop debugging our Function. Press Ctrl + Shift + G and enter a commit message and select commit.
11 – Select Push from Pull, Push Git menu to push our changes to our remote origin Git Repository in our Azure DevOps project.
The code for our function is now available in the Git Repository in our Azure DevOps project, so we’re ready to create our pipeline to deploy our function to the AKS Cluster we created earlier.
For this example we will be creating our pipeline in Azure DevOps, using the Deploy to Azure Kubernetes Service template.
1 – In our Azure DevOp project, go to Pipelines and select New Pipeline.
2 – In the new pipeline, Where is your code? screen select Azure Repos Git then we’ll select our repository (myFunction) in the Select a repository screen.
3 – Now let’s select the Deploy to Azure Kubernetes Service template in the Configure you pipeline screen.
4 – Select the Azure Subscription that you want to deploy to when prompted and click Continue.
5 – Specify the following details for when prompted i.e. the AKS Cluster and ACR that we created earlier as well as new Namespace within our Cluster to deploy our function to.
6 – Click Validate and Configure to generate our YAML pipeline.
7 – Review the YAML output and click Save and Run. When prompted click Save and run to commit our pipeline into the main branch of our repository.
8 – Once our new pipeline has been committed into our repository the pipeline will begin to run the stages in our pipeline. The build stage will be building a container image of our function and push it to ACR and the Deploy stage will deploy our container to our AKS Cluster.
Now our function should be available on our AKS Cluster, so let’s go ahead and validate that it’s running.
We’re going to need to get the external IP address of AKS Cluster so that we can check that our function is running, so let’s go over our Cloud Shell session and run the following commands.
1 – Authenticate to our AKS Cluster by running az aks get-credentials –resource-group tip4-dev-aes-rg –name tip4devaesaks.
2 – List the Pods running on the cluster under the Namespace we created by running kubectl get pods –namespace myfunction.
3 – Get the details of the Service by running kubectl get service myfunction –namespace myfunction.
Now let’s open up a browser window/tab enter the EXTERNAL-IP of our AKS Cluster and access our function.
In Summary – Azure Functions AKS DevOps
In this article we looked at how we can build an Azure Function as a container image push it into Azure Container Registry and deploy it to Azure Kubernetes Service via Azure DevOps, not only allowing us to automate our container workflow but give us the flexibility to run our Azure Functions on-premise or in the Cloud.
Thank you for reading. We hope that this article has provided you with some useful information.
Managing Director | DevOps & Cloud Lead