Friday, July 25, 2025

Azure Functions - Labs


I. Functions

Architecture diagram



Exercises 

Exercise 1: Create Azure Resources

  • Task 1: Open the Azure portal

  • Task 2: Create a Storage Account

  • Task 3: Create a Function App

  • Task 4: Review created resources

Exercise 2: Configure the Local Azure Functions Project

  • Task 1: Initialize a new project with Azure Functions Core Tools

  • Task 2: Configure connection strings in local.settings.json

  • Task 3: Build the project

Exercise 3: Create an HTTP Trigger Function (Echo)

  • Task 1: Create a new HTTP-triggered function

  • Task 2: Implement logic to echo back the request body

  • Task 3: Test the function locally using curl

Exercise 4: Create a Timer Trigger Function (Recurring)

  • Task 1: Create a new Timer-triggered function

  • Task 2: Configure it to run every minute

  • Task 3: Observe periodic log output

Exercise 5: Integrate with Azure Blob Storage

  • Task 1: Upload a file to a blob container

  • Task 2: Create a new HTTP function GetSettingInfo

  • Task 3: Add the Storage extension package

  • Task 4: Implement logic to read from the blob

  • Task 5: Test the function and verify returned content

Exercise 6: Deploy to Azure

  • Task 1: Sign in using Azure CLI

  • Task 2: Deploy the project using func azure functionapp publish

  • Task 3: Test the deployed functions from the Azure portal


Clase02.mp4
Inicio: 18m
Fin: 3h20m

Checkpoint 1: 2h25m
Checkpoint 2: 3h


Errores

PS C:\Allfiles\Labs\02\Starter\func> func start --build

[2025-08-05T01:33:46.409Z] A host error has occurred during startup operation '7ecd3452-ab28-4680-85ec-5e57ca3c0389'.
[2025-08-05T01:33:46.410Z] Microsoft.Azure.WebJobs.Extensions.Http: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The syste
m cannot find the file specified.
Value cannot be null. (Parameter 'provider')
[2025-08-05T01:33:46.421Z] Host startup operation has been canceled

Solución: Reinstalar "Azure Functions Core Tools"


Alternativas para desplegar un proyecto local a Azure Functions

  • Azure Functions Core Tools (CLI)
    • Cuando estás desarrollando localmente y quieres subir cambios rápidamente.
    • func azure functionapp publish <NombreFunctionApp>
  • Visual Studio / Visual Studio Code
    • Ideal para desarrolladores individuales o pruebas manuales.
    • En Visual Studio, clic derecho en el proyecto > Publish > selecciona tu Function App y publica.
  • GitHub Actions (CI/CD automatizado)
    • Para proyectos en equipo. Necesidad de despliegue continuo.
    • Puedes configurar un flujo de trabajo (.yml) para desplegar automáticamente cuando haces push a una rama.
  • Azure DevOps Pipelines
    • Proyectos corporativos con requisitos de seguridad, testing y control.
    • También puedes configurar pipelines para compilar y desplegar desde repos Azure DevOps o GitHub.
  • Zip Deploy (Manual o Scripted)
    • En entornos sin CI/CD. Cuando haces despliegue desde un artefacto ya compilado.
  • Terraform / Bicep / ARM Templates
    • Cuando necesitas versionar y controlar tu infraestructura también
    • Para infraestructura como código que incluye despliegue de código (por ejemplo, desde un paquete en Azure Storage).

II. Durable Functions

📝 Descripción general del ejercicio

Este ejercicio simula un escenario real en el que una empresa llamada Acme procesa archivos de datos sensibles enviados en formato .csv y encriptados, a través de un flujo completamente automatizado usando Durable Functions en Azure.

El objetivo es implementar una solución serverless, escalable y segura que:

  • Detecta automáticamente cuando un archivo encriptado es cargado en Blob Storage,

  • Desencripta y procesa los datos en fragmentos manejables,

  • Y reconsolida, encripta y entrega el resultado final nuevamente en el almacenamiento, notificando a un sistema externo.


🎯 Objetivo principal

Construir una aplicación basada en Azure Durable Functions que permita orquestar múltiples tareas relacionadas con la carga, desencriptado, procesamiento por lotes y publicación de archivos CSV encriptados.

Este flujo demuestra cómo combinar múltiples servicios de Azure, como:

  • Event Grid para eventos automatizados,

  • Blob Storage para almacenamiento de archivos,

  • Key Vault para la gestión de claves seguras,

  • Y Durable Functions para la orquestación del flujo de trabajo completo.


🛠️ Tecnologías y servicios involucrados

  • Azure Blob Storage

  • Azure Event Grid

  • Azure Key Vault

  • Azure Durable Functions (.NET Isolated / VS Code)

  • Azure Functions Core Tools (para pruebas locales)

  • C# / .NET 8


🧩 ¿Qué aprenderás?

  • Diseñar e implementar funciones serverless orquestadas

  • Integrar múltiples servicios de Azure de forma segura

  • Procesar archivos en lotes para escalar eficientemente

  • Usar claves del Key Vault para desencriptado/encriptado

  • Automatizar flujos basados en eventos con Event Grid

  • Desarrollar y probar en local usando VS Code



🧩 Solution Breakdown – Exercises

Exercise 1: Create and configure the local environment

Goal: Set up your local development environment using VS Code.
Steps:

  • Install Azure Functions Core Tools and the Azure Functions extension in VS Code.

  • Create a new isolated worker Azure Functions project (.NET 8).

  • Set up local.settings.json with mock or real connections to:

    • Azure Storage

    • Azure Key Vault (optional in local dev)

  • Test the project locally using a basic function like Echo.

Exercise 2: React to blob uploads using Event Grid

Goal: Trigger the process when a file is uploaded to Blob Storage.
Steps:

  • Create a container in Azure Blob Storage to receive encrypted .csv files.

  • Configure an Event Grid topic and subscription to monitor blob uploads.

  • Implement a starter function StartOrchestration that triggers the Durable Orchestrator with the blob file path.

  • Upload a test file and verify that the orchestration is started.

Exercise 3: Orchestrate the workflow with Durable Functions

Goal: Create the Durable Function orchestrator that coordinates the entire file processing pipeline.
Steps:

  • Implement the orchestrator function ProcessEncryptedFileOrchestrator.

  • Chain activity functions using CallActivityAsync for:

    • Decryption

    • Batch processing

    • Consolidation

    • Notification

  • Log the execution flow to monitor progress.

Exercise 4: Decrypt the file using Azure Key Vault

Goal: Access a secure key and use it to decrypt the file.
Steps:

  • Set up Key Vault and store the encryption key.

  • Grant access to the Function App using a managed identity.

  • Create an activity function DecryptFileActivity.

  • Read the blob file and decrypt it using the retrieved key (or simulate decryption for testing).

Exercise 5: Process file in batches of 100 records

Goal: Divide the file into chunks and process each batch separately.
Steps:

  • Split the CSV into batches of 100 lines.

  • For each batch, call ProcessBatchActivity.

  • Inside each batch, apply logic based on a flag to assign message templates.

  • Store intermediate results (in-memory or temporary blobs).

Exercise 6: Consolidate and re-encrypt the processed file

Goal: Merge processed data and save the output securely.
Steps:

  • Create an activity ConsolidateRecordsActivity.

  • Merge all processed batches into a single CSV.

  • Encrypt the new file with the same or a new key.

  • Upload the result to a target Blob Storage container.

Exercise 7: Notify external systems

Goal: Notify another team or system that the file is ready.
Steps:

  • Implement NotifyFileReadyActivity.

  • Send a JSON message to another Event Grid Topic or a custom webhook.

  • Include metadata like filename, timestamp, and status.



🎓 By the end of this exercise series, you will have:

  • A full event-driven, serverless, secure, and scalable file processing pipeline.

  • Hands-on experience using Durable Functions, Blob Storage, Key Vault, and Event Grid.

  • A modular solution you can extend and reuse in real-world scenarios.


No comments:

Post a Comment