How to deploy an Azure Function app in AKS using the Azure CLI

Peter Dol
3 min readMay 29, 2021

--

In a series of blog posts I will show how you can deploy your Azure Function App to AKS using Terraform. We will take small steps, so first we will deploy an Azure Function with the Azure CLI to validate that everything runs as expected. Second step is to deploy everything using Terraform.

Create the Function app project

In a terminal or command prompt, run the following command to create a new Azure Function app project. Use the docker flag to generate a Dockerfile that will be used to build the Function App docker image.

# create a dummy function that is triggered by a HTTP GET
func init aksexample --worker-runtime dotnet --docker
cd aksexample
func new --name HttpExample --template "HTTP trigger"
# to check if everything works
dotnet build

Create the Azure infrastructure

Next, create the Azure infrastructure:

# global variables
resourceGroup=k8s-example-rg
location=westeurope
acrName=exampleacr01
storageAccount=examplesa01
functionAppName=example01
fullAcrName=$acrName.azurecr.io
imageName=$fullAcrName/example:v1.0.0
aksName=exampleaks01
# create a resource group
az group create --name $resourceGroup --location $location
# create a container registry for hosting your docker image
az acr create --resource-group $resourceGroup --name $acrName --sku Basic
# grab the login credentials, username defaults to acr name
az acr login --name $acrName
az acr update -n $acrName --admin-enabled true
password=$(az acr credential show -n $acrName --query 'passwords[0].value' -o tsv)
# create the infra for the function app
az storage account create --name $storageAccount --location $location --resource-group $resourceGroup --sku Standard_LRS

Create the AKS

Now we can create the azure kubernetes service in Azure:

# create the aks and install the cli so we can use kubectl
az aks create --resource-group $resourceGroup --name $aksName --node-count 1 --enable-addons monitoring --generate-ssh-keys
az aks get-credentials --resource-group $resourceGroup --name $aksName
az aks install-cli
# create a secret for the storage account
storageConnectionString=$(az storage account show-connection-string --resource-group $resourceGroup --name $storageAccount --query connectionString --output tsv)
kubectl create secret generic storageaccountconnectionstring --from-literal=storageAccountConnectionString=$storageConnectionString
# create a secret for the acr credentials
kubectl create secret docker-registry containerregistrysecret --docker-server=$fullAcrName --docker-username=$acrName --docker-password=$password

Deploy the Azure Function app to AKS

Finally we can deploy our Azure Function app to the AKS:

# attach the aks to the acr so it can grab the image from the acr
az aks update --name $aksName --resource-group $resourceGroup --attach-acr $acrName
# func kubernetes deploy runs a docker build, pushes the image to the container registry and deploys it to AKSfunc kubernetes deploy --name $functionAppName --registry $fullAcrName

Test if everything works

If everything went successful you should see logging like:

Getting loadbalancer ip for the service: example-http
Waiting for the service to be ready: example01-http
HttpExample - [httpTrigger]
Invoke url:
http://20.56.225.47/api/httpexample

You can then invoke the url with:

curl http://20.56.225.47/api/httpexample?name=bla

Which should give you the expected result.

Wrapping up

Now we have deployed our first Function app to AKS we would like to automate the whole process in an Azure DevOps pipeline. Let's use Terraform for that.

Don't forget to remove your azure infrastructure because the AKS will munch away your MSDN credits at no time otherwise.

--

--

Peter Dol
Peter Dol

Written by Peter Dol

Freelance software engineer with an interest in .NET, Azure, React and DevOps.

No responses yet