Exploring Azure - Azure Functions Coding

In this article we will be continuing our journey in exploring Azure. On this episode we will explore and dig deep into the coding and implementations Azure Functions this is the second part of Azure functions explorations.

This is part 2 of the Azure functions series you can check part on by clicking on the the following link
https://dev.to/moe23/exploring-azure-azure-functions-45cg

You watch the full video on Youtube

So what we will cover today:

  • Ingredients
  • How to build Azure Functions
  • Code

As always please comment your questions, clarifications and suggestions in the comments down below. Please like, share and subscribe if you like the video. It will really help the channel

Ingredients

  • Azure account
  • VS Code
  • Dotnet SDK

How to create Azure functions

  • Azure Portal
  • Azure CLI
  • Terraform

Azure CLI:

We will need to do some configuration and installation before we can deploy

  • We need to have an Azure Account
  • We need to install function tools
brew tap azure/functions
brew install azure-functions-core-tools@3
  • We need to have the Azure CLI installed
brew update && brew install azure-cli
az login

Now we need to do some checks before we can proceed

  • run func --version to check that the Azure Functions Core Tools are version 3.x.
  • Run az --version to check that the Azure CLI version is 2.4 or later.
  • Run az login to sign in to Azure and verify an active subscription.
  • Run dotnet --list-sdks to check that .NET Core SDK version 3.1.x is installed

Now let us create a new Azure function

func new --name HttpExample --template "HTTP trigger" --authlevel "anonymous"

Now let us start this function

func start

We can see in the terminal a link has been generated which will allow us to connect to the function. We can execute the function by using that link in the browser

http://localhost:7071/api/HttpExample?name=Mohamad

Now we need to publish our application to Azure

  • We need to make sure that we are signed in to Azure through our CLI
az login
  • As we know everything in Azure needs to belong to a resource group so we will start by creating a resource group
az group create --name AzFunctionsDeepDive-rg --location uksouth
  • Azure Functions require us to have a storage account so we need to create it as well
az storage account create --name azfuncstoragemldemo --location uksouth --resource-group AzFunctionsDeepDive-rg --sku Standard_LRS
  • Once that is created now we can create our azure function
az functionapp create --resource-group AzFunctionsDeepDive-rg --consumption-plan-location uksouth --runtime dotnet --functions-version 3 --name azfuncmldemo1 --storage-account azfuncstoragemldemo

Now that we have created our resources on Azure, its time for us to deploy our function

func azure functionapp publish azfuncmldemo1

Once its deployed we can get the url for our function and we can test it out in our browser

Terraform:

we will need to install some items:

We need to download terraform (https://www.terraform.io/downloads.html)

brew tap hashicorp/tap
brew install hashicorp/tap/terraform
brew update
brew update && brew install azure-cli
az login

VS code plugin for Terrafrom

Now we need to create a new file called setup.tf which will represent our code for Azure function infrastructure

# we need to specify the provider that we are going to use
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=2.67.0"
    }
  }
}

provider "azurerm" {
   features {

  }
}

resource "azurerm_resource_group" "az_func_rg" {
    name = "azure-functions-demo-rg"
    location = "uksouth"
}

resource "azurerm_storage_account" "azfuncstorage" {
    name = "azurefuncstoragesyt"
    resource_group_name = azurerm_resource_group.az_func_rg.name
    location = azurerm_resource_group.az_func_rg.location
    account_tier = "Standard"
    account_replication_type = "LRS"
}

resource "azurerm_app_service_plan" "az-func-sp" {
    name = "azure-functions-service-plan"
    location = azurerm_resource_group.az_func_rg.location
    resource_group_name = azurerm_resource_group.az_func_rg.name
    kind = "Linux"
    reserved = true

    sku {
        tier = "Dynamic"
        size = "Y1"
    }
}

resource "azurerm_function_app" "supper-az-func" {
  name = "super-azure-functions"
  location = azurerm_resource_group.az_func_rg.location
  resource_group_name = azurerm_resource_group.az_func_rg.name
  app_service_plan_id = azurerm_app_service_plan.az-func-sp.id
  storage_account_name = azurerm_storage_account.azfuncstorage.name
  storage_account_access_key = azurerm_storage_account.azfuncstorage.primary_access_key
  os_type = "linux"
}

We need to make sure we are logged into Azure through CLI

az login # to login

Once we have logged in we need now to run terraform code

terraform init
terraform plan
terraform apply

To remove all of the infrastructure created

terraform destroy

30