Skip to main content
  1. Posts/

Deploy Azure Function App

·3 mins·
azure azure
Table of Contents

Azure Functions adalah layanan serverless yang memungkinkan pengguna menjalankan kode tanpa harus menyediakan atau mengelola infrastruktur secara eksplisit. Layanan ini mendukung berbagai bahasa pemrograman, termasuk C#, Java, JavaScript, Python, dan PowerShell.

Azure Functions dapat di-trigger oleh berbagai sumber, seperti:

  • HTTP requests
  • Timers (cron-like schedules)
  • Queues (misalnya Azure Storage Queue, Service Bus Queue)
  • Layanan Azure lainnya

Hal ini membuat Azure Functions ideal untuk skenario microservices, automation, dan integration.

Install Azure Functions Core Tools
#

Tambahkan Repository Microsoft

curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg

Tambahkan repository:

sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-$(lsb_release -cs)-prod $(lsb_release -cs) main" > /etc/apt/sources.list.d/dotnetdev.list'

Lalu update daftar paket:

sudo apt-get update

Install Core Tools

sudo apt-get install azure-functions-core-tools-4

Setup Azure Functions App
#

1. Buat Resource Group
#

az group create \
  --name <resourceGroupName> \
  --location <REGION>

2. Buat Storage Account
#

Sesuaikan dengan lokasi dan resource group yang sudah dibuat.

az storage account create \
  --name <STORAGE_NAME> \
  --resource-group <resourceGroupName> \
  --location <REGION> \
  --sku Standard_RAGRS \
  --kind StorageV2 \
  --min-tls-version TLS1_2 \
  --allow-shared-key-access true

3. Buat Managed Identity & Tambahkan Role
#

Buat User Assigned Managed Identity dan berikan role Storage Blob Data Owner di storage account.

output=$(az identity create \
  --name "demo-app" \
  --resource-group <resourceGroupName> \
  --location <REGION> \
  --query "{userId:id, principalId: principalId, clientId: clientId}" -o json)

userId=$(echo $output | jq -r '.userId')
principalId=$(echo $output | jq -r '.principalId')
clientId=$(echo $output | jq -r '.clientId')

storageId=$(az storage account show \
  --resource-group <resourceGroupName> \
  --name <STORAGE_NAME> \
  --query 'id' -o tsv)

az role assignment create \
  --assignee-object-id $principalId \
  --assignee-principal-type ServicePrincipal \
  --role "Storage Blob Data Owner" \
  --scope $storageId

4. Buat Functions App
#

Opsi A: Dengan VNET Integration

Gunakan Flex Consumption Plan jika ingin integrasi dengan VNET.

IDENTITY_ID=$(az identity show \
  --name demo-app \
  --resource-group <resourceGroupName> \
  --query 'id' -o tsv)

az functionapp create \
  --resource-group <resourceGroupName> \
  --name <APP_NAME> \
  --flexconsumption-location <REGION> \
  --runtime <RUNTIME> \
  --runtime-version <LANGUAGE_VERSION> \
  --vnet <vnetResourceId> \
  --subnet <subnetName> \
  --storage-account <STORAGE_NAME> \
  --deployment-storage-auth-type UserAssignedIdentity \
  --deployment-storage-auth-value $IDENTITY_ID
  --assign-identity $IDENTITY_ID

Opsi B: Tanpa VNET Integration

Gunakan Consumption Plan (lebih murah, tapi tanpa private VNET).

IDENTITY_ID=$(az identity show \
  --name demo-app \
  --resource-group <resourceGroupName> \
  --query 'id' -o tsv)

az functionapp create \
  --resource-group <resourceGroupName> \
  --consumption-plan-location <REGION> \
  --runtime <RUNTIME> \
  --runtime-version <LANGUAGE_VERSION> \
  --functions-version 4 \
  --os-type Linux \
  --name <APP_NAME> \
  --storage-account <STORAGE_NAME>
  --assign-identity $IDENTITY_ID

Create a Local Code Project and Function
#

1. Inisialisasi Proyek
#

Buat folder proyek lalu jalankan func init untuk menentukan bahasa pemrograman function app.

mkdir myapp && cd myapp
func init --worker-runtime python

2. Buat Function dengan Template
#

Gunakan template HTTP trigger dengan level otentikasi anonymous.

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

3. Jalankan Function App Secara Lokal
#

func start

Deploy the Function Project to Azure
#

1. Deploy Function App
#

Gunakan perintah berikut untuk melakukan deploy ke Azure:

func azure functionapp publish <APP_NAME>

2. Streaming Log dari Function App
#

Untuk melihat log function app di Azure secara real-time:

func azure functionapp logstream <APP_NAME>

Advanced Scenarios
#

Menambahkan Environment Variables
#

Untuk menambahkan atau memperbarui environment variables pada function app:

az functionapp config appsettings set \
  --name <APP_NAME> \
  --resource-group <resourceGroupName> \
  --settings \
    CONTAINER_APP_NAME=<APP_NAME> \
    RESOURCE_GROUP=<resourceGroupName> \
    AZURE_CLIENT_ID=$(az identity show --name demo-app --resource-group <resourceGroupName> --query clientId -o tsv) \
    AzureFunctionsJobHost__logging__console__isEnabled="true"

Related

Implementasi Autoscaling VM di Azure
·4 mins
azure azure
Azure CLI Cheat sheet
·7 mins
azure azure
Mengubah Tanggal Modifikasi File di Linux
·1 min
linux linux
HTTP Middleware Traefik
·3 mins
traefik traefik
Git Commands Cheat Sheet
·5 mins
git git
Create Simple Systemd Service in Linux
·2 mins
linux linux