azureazure-functionsazure-storage-queues

How to create connection to create - queue item - HTTP Trigger - local - Azurite - Azure Queue Storage


I installed Azurite and started ( assuming the command is correct.)

azurite --queueHost 127.0.0.1

I created an outqueue using Azure Storage Explorer

I want to test/use this function

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

const queueOutput = output.storageQueue({
    queueName: 'outqueue',
    connection: 'MyStorageConnectionAppSetting',
});

app.http('httpTrigger1', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    extraOutputs: [queueOutput],
    handler: async (request, context) => {
        const body = await request.text();
        context.extraOutputs.set(queueOutput, body);
        return { body: 'Created queue item.' };
    },
});

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue-output?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cextensionv5&pivots=programming-language-javascript

What I dont know / understand is "MyStorageConnectionAppSetting" . (The name of an app setting or setting collection that specifies how to connect to Azure Queues. See Connections.)

Error

[2024-04-22T11:04:11.464Z] System.Private.CoreLib: Exception while executing function: Functions.httpTrigger1. Microsoft.Azure.WebJobs.Extensions.Storage.Queues: Storage account connection string 'AzureWebJobsMyStorageConnectionAppSetting' does not exist. Make sure that it is a defined App Setting.

what is the step I am missing to create that MyStorageConnectionAppSetting ? Any reference documentation that gives the steps is appreciated.

Thank you


Solution

  • To resolve the error, use the application setting "AzureWebJobsStorage": "UseDevelopmentStorage=true" in the local.settings.json to enable storage connection locally.

    local.settings.json:

    {
      "IsEncrypted": false,
      "Values": {
        "FUNCTIONS_WORKER_RUNTIME": "node",
        "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
        "AzureWebJobsStorage": "UseDevelopmentStorage=true"
      }
    }
    

    Change the Connection name in the function code.

    Code Snippet:

    const { app, output } = require('@azure/functions');
    
    const queueOutput = output.storageQueue({
        queueName: 'outqueue',
        connection: 'AzureWebJobsStorage ',
    });
    
    app.http('httpTrigger1', {
        methods: ['GET', 'POST'],
        authLevel: 'anonymous',
        extraOutputs: [queueOutput],
        handler: async (request, context) => {
            const body = await request.text();
            context.extraOutputs.set(queueOutput, body);
            return { body: 'Created queue item.' };
        },
    });
    

    Run the function locally:

    C:\Users\uname\function>func start
    
    Azure Functions Core Tools
    Core Tools Version:       4.0.5611 Commit hash: N/A +591b8aec842e333a87ea9e23ba390bb5effe0655 (64-bit)
    Function Runtime Version: 4.31.1.22191
    
    [2024-04-22T13:17:40.044Z] Worker process started and initialized.
    
    Functions:
    
            httpTrigger1: [GET,POST] http://localhost:7071/api/httpTrigger1
    
    For detailed output, run func with --verbose flag.
    [2024-04-22T13:17:53.133Z] Executing 'Functions.httpTrigger1' (Reason='This function was programmatically called via the host APIs.', Id=00ec97ec-a464-43af-acff-854c28b43711)
    [2024-04-22T13:17:56.667Z] Executed 'Functions.httpTrigger1' (Succeeded, Id=00ec97ec-a464-43af-acff-854c28b43711, Duration=3631ms)
    

    Response:

    enter image description here