AgentSkillsCN

azure-functions

借助 Azure 函数实现无服务器事件驱动计算——按执行次数付费、自动弹性伸缩、支持多种触发器类型,并提供灵活的部署流程。

SKILL.md
--- frontmatter
name: azure-functions
description: Serverless event-driven compute with Azure Functions - pay-per-execution, auto-scaling, multiple trigger types, and deployment workflows

Azure Functions

Azure Functions is a serverless compute service for event-driven applications. Pay only for execution time with automatic scaling.

Skill Activation Triggers

Use this skill immediately when the user asks to:

  • "Deploy my function to Azure"
  • "Create a serverless API on Azure"
  • "Deploy Azure Functions"
  • "Set up a timer-triggered function in Azure"
  • "Create webhooks in Azure"
  • Any request involving serverless functions, event-driven processing, or Azure Functions

Key Indicators:

  • Project uses Azure Functions (host.json, local.settings.json present)
  • User mentions serverless, functions, triggers, or bindings
  • User wants to deploy lightweight APIs without container management
  • User needs timer jobs, queue processors, or event handlers

Quick Reference

PropertyValue
CLI prefixaz functionapp, func
MCP toolsazure__functionapp (command: functionapp_list)
Best forEvent-driven, pay-per-execution, serverless

Hosting Plans

PlanScalingTimeoutUse Case
ConsumptionAuto, to 05-10 minEvent-driven, variable load
PremiumAuto, warm30+ minConsistent load, VNet
DedicatedManualUnlimitedPredictable load

Trigger Types

TriggerUse Case
HTTPREST APIs, webhooks
TimerScheduled jobs (CRON)
BlobFile processing
QueueMessage processing
Event GridEvent-driven
Cosmos DBChange feed processing
Service BusEnterprise messaging

Prerequisites Validation

Validate all prerequisites before starting development or deployment.

javascript
async function validatePrerequisites() {
  const checks = [];

  // Check Azure CLI authentication
  try {
    await exec('az account show');
    checks.push({ name: 'Azure CLI', status: 'authenticated' });
  } catch (error) {
    throw new Error('Not authenticated with Azure CLI. Run: az login');
  }

  // Check Azure Functions Core Tools - install if not present
  try {
    await exec('func --version');
    checks.push({ name: 'Azure Functions Core Tools', status: 'installed' });
  } catch (error) {
    console.log('Azure Functions Core Tools not found. Installing...');
    try {
      await exec('npm install -g azure-functions-core-tools@4 --unsafe-perm true');
      checks.push({ name: 'Azure Functions Core Tools', status: 'installed (just now)' });
    } catch (installError) {
      throw new Error('Failed to install Azure Functions Core Tools. Please install manually: npm install -g azure-functions-core-tools@4');
    }
  }

  return checks;
}

Platform-Specific Installation

If npm installation fails, use platform-specific installers:

bash
# Windows (winget)
winget install Microsoft.AzureFunctionsCoreTools

# Windows (Chocolatey)
choco install azure-functions-core-tools

# macOS (Homebrew)
brew tap azure/functions
brew install azure-functions-core-tools@4

# Linux (Ubuntu/Debian)
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
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'
sudo apt-get update
sudo apt-get install azure-functions-core-tools-4

Local Development

Initialize Function Project

Create a new Azure Functions project with the desired runtime.

bash
# Create new function project
func init MyFunctionApp --worker-runtime node --model V4

# Create a new HTTP-triggered function
cd MyFunctionApp
func new --name HttpTrigger --template "HTTP trigger"

# For TypeScript
func init MyFunctionApp --worker-runtime node --language typescript --model V4

Supported runtimes: node, python, dotnet, dotnet-isolated, java, powershell, custom

Project Structure

code
MyFunctionApp/
├── host.json              # Function app configuration
├── local.settings.json    # Local development settings
├── package.json           # Node.js dependencies
└── src/
    └── functions/
        └── HttpTrigger.js # Function code

Run Locally

bash
# Start local development server
func start

# Start with specific port
func start --port 7072

# Start with debugging enabled
func start --verbose

Local endpoints:

  • HTTP triggers: http://localhost:7071/api/{functionName}
  • Admin API: http://localhost:7071/admin/functions

Example HTTP Function (Node.js v4)

javascript
const { app } = require('@azure/functions');

app.http('HttpTrigger', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    handler: async (request, context) => {
        context.log('HTTP function processed a request.');
        const name = request.query.get('name') || await request.text() || 'World';
        return { body: `Hello, ${name}!` };
    }
});

Example Timer Function

javascript
const { app } = require('@azure/functions');

app.timer('TimerTrigger', {
    schedule: '0 */5 * * * *', // Every 5 minutes
    handler: async (myTimer, context) => {
        context.log('Timer trigger executed at:', new Date().toISOString());
    }
});

local.settings.json

json
{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "MY_API_KEY": "local-dev-key"
  },
  "Host": {
    "LocalHttpPort": 7071,
    "CORS": "*"
  }
}

Create Azure Resources

Create the required Azure resources for deployment.

bash
# Set variables
RESOURCE_GROUP="myResourceGroup"
LOCATION="eastus"
STORAGE_ACCOUNT="mystorageaccount$(date +%s)"
FUNCTION_APP="myFunctionApp$(date +%s)"

# Create resource group
az group create --name $RESOURCE_GROUP --location $LOCATION

# Create storage account (required for Function Apps)
az storage account create \
    --name $STORAGE_ACCOUNT \
    --resource-group $RESOURCE_GROUP \
    --location $LOCATION \
    --sku Standard_LRS

# Create Function App (Consumption Plan - pay per execution)
az functionapp create \
    --name $FUNCTION_APP \
    --resource-group $RESOURCE_GROUP \
    --storage-account $STORAGE_ACCOUNT \
    --consumption-plan-location $LOCATION \
    --runtime node \
    --runtime-version 20 \
    --functions-version 4

Create with Premium Plan

bash
# Create Premium Plan
az functionapp plan create \
    --name myPremiumPlan \
    --resource-group $RESOURCE_GROUP \
    --location $LOCATION \
    --sku EP1 \
    --is-linux

az functionapp create \
    --name $FUNCTION_APP \
    --resource-group $RESOURCE_GROUP \
    --storage-account $STORAGE_ACCOUNT \
    --plan myPremiumPlan \
    --os-type Linux \
    --runtime node \
    --runtime-version 20 \
    --functions-version 4

Deploy Functions

Deploy functions to Azure using Azure Functions Core Tools.

bash
# Deploy to Azure (from project root)
func azure functionapp publish $FUNCTION_APP

# Deploy with build (for TypeScript/compiled projects)
func azure functionapp publish $FUNCTION_APP --build remote

# Deploy with verbose output
func azure functionapp publish $FUNCTION_APP --verbose

# Deploy specific slot
func azure functionapp publish $FUNCTION_APP --slot staging

# Force update function app settings
func azure functionapp publish $FUNCTION_APP --publish-settings-only

# Upload local.settings.json to Azure
func azure functionapp publish $FUNCTION_APP --publish-local-settings

Configuration Management

Manage application settings and connection strings.

bash
# Set application setting
az functionapp config appsettings set \
    --name $FUNCTION_APP \
    --resource-group $RESOURCE_GROUP \
    --settings "MySetting=MyValue"

# Set connection string
az functionapp config connection-string set \
    --name $FUNCTION_APP \
    --resource-group $RESOURCE_GROUP \
    --connection-string-type SQLAzure \
    --settings "MyConnection=Server=..."

# List settings
az functionapp config appsettings list \
    --name $FUNCTION_APP \
    --resource-group $RESOURCE_GROUP

# Get function keys
az functionapp keys list -n $FUNCTION_APP -g $RESOURCE_GROUP

Monitoring and Logs

View function execution logs and diagnostics.

bash
# Stream live logs
func azure functionapp logstream $FUNCTION_APP

# View deployment logs
az functionapp log deployment list \
    --name $FUNCTION_APP \
    --resource-group $RESOURCE_GROUP

# Enable Application Insights (recommended)
az monitor app-insights component create \
    --app $FUNCTION_APP-insights \
    --location $LOCATION \
    --resource-group $RESOURCE_GROUP

# Link App Insights to Function App (use connection string - instrumentationKey is deprecated)
APPINSIGHTS_CONNECTION_STRING=$(az monitor app-insights component show \
    --app $FUNCTION_APP-insights \
    --resource-group $RESOURCE_GROUP \
    --query connectionString -o tsv)

az functionapp config appsettings set \
    --name $FUNCTION_APP \
    --resource-group $RESOURCE_GROUP \
    --settings "APPLICATIONINSIGHTS_CONNECTION_STRING=$APPINSIGHTS_CONNECTION_STRING"

Deployment Slots (Premium/Dedicated Plans)

Use deployment slots for zero-downtime deployments.

bash
# Create staging slot
az functionapp deployment slot create \
    --name $FUNCTION_APP \
    --resource-group $RESOURCE_GROUP \
    --slot staging

# Deploy to staging
func azure functionapp publish $FUNCTION_APP --slot staging

# Swap slots
az functionapp deployment slot swap \
    --name $FUNCTION_APP \
    --resource-group $RESOURCE_GROUP \
    --slot staging \
    --target-slot production

CI/CD with GitHub Actions

Automate deployments with GitHub Actions.

Important: Before creating CI/CD pipelines, get CI/CD guidance with deploy_pipeline_guidance_get.

.github/workflows/azure-functions.yml:

yaml
name: Deploy Azure Functions

on:
  push:
    branches: [main]

env:
  AZURE_FUNCTIONAPP_NAME: 'myFunctionApp'
  AZURE_FUNCTIONAPP_PACKAGE_PATH: '.'
  NODE_VERSION: '20.x'

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: Install dependencies
        run: npm ci
        working-directory: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}

      - name: Build (if TypeScript)
        run: npm run build --if-present
        working-directory: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}

      - name: Azure Login
        uses: azure/login@v2
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: Deploy to Azure Functions
        uses: Azure/functions-action@v1
        with:
          app-name: ${{ env.AZURE_FUNCTIONAPP_NAME }}
          package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}

Create Azure credentials secret:

bash
az ad sp create-for-rbac --name "github-actions-sp" \
    --role contributor \
    --scopes /subscriptions/{subscription-id}/resourceGroups/{resource-group} \
    --sdk-auth

Durable Functions

For long-running orchestrations and stateful workflows:

javascript
// Orchestrator
const df = require('durable-functions');

module.exports = df.orchestrator(function* (context) {
    const result1 = yield context.df.callActivity('Step1', input);
    const result2 = yield context.df.callActivity('Step2', result1);
    return result2;
});

Patterns:

  • Function chaining
  • Fan-out/fan-in
  • Async HTTP APIs
  • Human interaction
  • Aggregator

Best Practices

PracticeDescription
Keep functions smallSingle-purpose functions are easier to test and maintain
Implement idempotencyAt-least-once triggers may execute multiple times
Use managed identityPrefer managed identity over connection strings for secure resource access
Configure timeoutSet functionTimeout in host.json (default 5 min for Consumption)
Use Application InsightsEnable for monitoring, tracing, and diagnostics
Secure HTTP functionsUse authLevel: 'function' or 'admin' for non-public endpoints
Environment variablesStore secrets in App Settings, not in code
Cold start optimizationUse Premium plan or keep-alive pings for latency-sensitive apps
Use Key VaultStore secrets securely with Key Vault references
Configure retry policiesSet appropriate retry behavior for triggers

Quick Start Checklist

Setup

  • Azure subscription created
  • Azure CLI installed (az --version)
  • Azure CLI authenticated (az login)
  • Azure Functions Core Tools installed (func --version)
  • Node.js/Python/dotnet installed (based on runtime)

Development

  • Initialize project with func init
  • Create functions with func new
  • Configure host.json settings
  • Test locally with func start

Deployment

  • Create resource group
  • Create storage account
  • Create Function App
  • Deploy with func azure functionapp publish
  • Configure app settings
  • Verify function URLs

Monitoring

  • Enable Application Insights
  • Stream logs with func azure functionapp logstream
  • Set up alerts for failures

Troubleshooting

IssueSymptomSolution
func not foundCommand not recognizedInstall Azure Functions Core Tools: npm install -g azure-functions-core-tools@4
Storage errorFunction app won't startVerify AzureWebJobsStorage connection string is valid
404 on functionFunction not foundCheck function is exported correctly and route is configured
Cold start delaysFirst request slowUse Premium plan or implement warm-up triggers
TimeoutFunction exceeds limitIncrease functionTimeout in host.json or use Durable Functions
Binding errorsExtension not loadedRun func extensions install to install required extensions
Deploy failsPublish errorEnsure function app exists and CLI is authenticated
Runtime mismatchVersion conflictVerify FUNCTIONS_EXTENSION_VERSION matches project
Execution limitsConsumption has 5-10 min timeoutUse Premium or Dedicated plan for longer executions
Scaling delaysCold starts on ConsumptionConsider Premium plan with pre-warmed instances

Debug commands:

bash
func start --verbose                     # Local debugging
func azure functionapp logstream $APP    # Live logs
az functionapp show --name $APP          # App details
az functionapp config show --name $APP   # Configuration
az functionapp list --output table       # List all function apps

Azure Resources

Resource TypePurposeAPI Version
Microsoft.Web/sitesFunction App2023-12-01
Microsoft.Storage/storageAccountsRequired storage2023-01-01
Microsoft.Web/serverfarmsApp Service Plan2023-12-01
Microsoft.Insights/componentsApplication Insights2020-02-02

MCP Server Tools

Use MCP tools to query existing resources:

  • azure__functionapp with command functionapp_list - List function apps

If Azure MCP is not enabled: Run /azure:setup or enable via /mcp.


Additional Resources