Azure / azure-functions-core-tools

Command line tools for Azure Functions
MIT License
1.29k stars 426 forks source link

Generated function.json missing direction out #3157

Open AllyssaLBatten opened 1 year ago

AllyssaLBatten commented 1 year ago

Problem

I am attempting to deploy an Azure Function via Rider. The function is triggered by an IoT Hub message, parses the data into json, and writes it to a Cosmos database.

When deploying this function, the generated function.json is missing the out direction on the generated Cosmos bindings. Everything else is correct.

Below is the C# code and bindings section of function.json with sensitive data replaced with placeholder values.

C# Code:

using Newtonsoft.Json.Linq;
using IoTHubTrigger = Microsoft.Azure.WebJobs.EventHubTriggerAttribute;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class IoTHubToCosmosDB
{
        [FunctionName("IoTHubToCosmosDB")]
        public static void Run(
            [IoTHubTrigger("eventHubName", Connection = "connectionString", ConsumerGroup = "consumerGroup")] string myIoTHubMessage,
            [CosmosDB("databaseName", "collectionName", ConnectionStringSetting = "connectionString", CreateIfNotExists = true, PartitionKey = "/deviceId")] out object dataDocument,
            ILogger log)
        {
            log.LogInformation($"C# trigger Azure function processed a IoT Hub message: {myIoTHubMessage}");
            var parsedMessage = JObject.Parse(myIoTHubMessage);
            dataDocument = parsedMessage;
        }
}

Bindings from generated function.json:

"bindings": [
    {
      "type": "eventHubTrigger",
      "connection": "connectionString",
      "consumerGroup": "consumerGroup",
      "eventHubName": "eventHubName",
      "name": "myIoTHubMessage"
    },
    {
      "type": "cosmosDB",
      "connectionStringSetting": "connectionString",
      "createIfNotExists": true,
      "partitionKey": "/deviceId",
      "databaseName": "databaseName",
      "collectionName": "collectionName",
      "useMultipleWriteLocations": false,
      "useDefaultJsonSerialization": false,
      "name": "dataDocument"
    }
  ],

Environment Azure Toolkit for Rider 3.50.0.1314-2021.3 Azure Functions Core Tools 4.0.4785 JetBrains Rider 2021.3.4

kshyju commented 1 year ago

Just to confirm, Is the write to the cosmos db failing due to the missing "direction" or any other issues you are seeing with the function app?

ghost commented 1 year ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

AllyssaLBatten commented 1 year ago

Correct, the write fails because it is missing the direction. On the Azure portal, when looking at the Integration, there is a warning that says:

The following bindings are missing the required direction property and may have been placed incorrectly: dataDocument. Please update the bindings in your functions.json file.

Otherwise, there are no issues.

MysteryAchievement commented 1 year ago

Hello all - is there any update on this issue? I am seeing the same problem. The manual fix is to add: "direction": "out",

to the function.json file in the portal's Code + Test screen but that's untenable for every deploy.

sandokanfirst commented 1 year ago

This is still an issue. I get

The following bindings are missing the required direction property and may have been placed incorrectly: dataDocument. Please update the bindings in your functions.json file.

Also, the object is unknown.

yashints commented 1 year ago

I am having a similar issue where function core tools generates the wrong binding information where it's emitting the direction property and instead it puts in access: 1

error

Function core tools version: 4.0.5198

[FunctionName("ProcessImage")]
        public static async Task Run([EventGridTrigger]CloudEvent eventGridEvent,
            [Blob(blobPath: "{data.url}", access: FileAccess.Read,
                Connection = "dataLakeConnection")] Stream incomingPlate,
            ILogger log)

Generates:

"bindings": [
    {
      "type": "eventGridTrigger",
      "name": "eventGridEvent"
    },
    {
      "type": "blob",
      "connection": "dataLakeConnection",
      "blobPath": "{data.url}",
      "access": 1,
      "name": "incomingPlate"
    }
  ],
gmap3570 commented 10 months ago

Anyone got the resolution for this? I manually added direction but when I tried to deploy its automatically reverting it to original. "direction":"in"

kshyju commented 10 months ago

The direction attribute was removed on purpose as part of https://github.com/Azure/azure-functions-vs-build-sdk/pull/542. You should not be manually adding this attribute to the generated functions.json file.

@gmap3570 Can you be more specific about what exactly is not working due to this change?

We will look into removing the portal warning. But if you are experiencing any other functional issues executing your functions, please open a new issue with the relevant details.

gmap3570 commented 10 months ago

image

Its not showing proper binding at output. on azure portal it show : 'The following bindings are missing the required direction property and may have been placed incorrectly: outputEvents. Please update the bindings in your functions.json file'

nauticalcoder commented 9 months ago

Are there any plans to address this issue. It continues to plague any deployment of an Azure Function using Microsoft.NET.Sdk.Functions.Generator-4.1.1 or Microsoft.NET.Sdk.Functions.Generator-4.2 and that uses a CosmoDB output binding. As it stands, we cannot have a true automated deployment of the Azure function as we have to go to the portal and fix the function.json by adding the out parameter manually after every deployment.

kshyju commented 9 months ago

No. There are no plans to fix this as the change was made on purpose, as I mentioned in the previous comment.

@nauticalcoder Can you be more specific about what issue are you facing due to this?

MysteryAchievement commented 9 months ago

@kshyju - I recently (still) encountered the same situation as described by the OP (@AllyssaLBatten), @nauticalcoder and by @gmap3570 in the screenshot provided. When you deploy, the output is "not defined" and you have to manually go in and fix the function.json. As @AllyssaLBatten stated, you must manually go in and add the "direction":"out" key pair.

So if the deployment produced this file:

{
    "name": "dataDocument",
    "databaseName": "myDatabase",
    "collectionName": "myCollection",
    "createIfNotExists": true,
    "connectionStringSetting": "cosmosdb_DOCUMENTDB",
    "partitionKey": "/deviceId",
    "type": "cosmosDB"
},

you must modify the JSON by adding:

{
    "name": "dataDocument",
    "databaseName": "myDatabase",
    "collectionName": "myCollection",
    "createIfNotExists": true,
    "connectionStringSetting": "cosmosdb_DOCUMENTDB",
    "partitionKey": "/deviceId",
    "type": "cosmosDB",
    "direction": "out" <=============
},

(note: the '<=============' arrow above does not actually go in the JSON)

kshyju commented 9 months ago

@MysteryAchievement Is your function app working fine for function executions as expected? Are you only seeing the UI issue (as shown in the screenshot above) >

MysteryAchievement commented 9 months ago

It works after manually making that change. The point I think that everyone is trying to make is this: while you can deploy the Azure Function code via Visual Studio or Rider, function.json needs manual modification in order to work. Therefore, automated deployment will not work with a CI/CD tool because you have to manually change the function.json file. The screenshot provided by @gmap3570 is the visual evidence that deployment is broken for output side (in my case, Cosmos DB).

kshyju commented 9 months ago

@MysteryAchievement You should not be editing the json file manually to add any attributes. If your function app is not working (to execute function triggers), but the portal UI is not showing the correct thing, we may need to investigate the portal side and fix that.

Does your function app functionally work (functions are executed properly - you could confirm this with your logs) without manually editing the functions.json of the deployed payload?

TFDeveloper commented 9 months ago

For what it's worth, on our end it appears the lack of the out attribute is not actually breaking the function. We thought it was. It does break the Portal UI though which shows the Integration and how the bindings are configured on the function.

kshyju commented 9 months ago

Tagging @mattchenderson to help direct this issue for a potential UX fix.

marcosgm commented 8 months ago

the workaround is to manually edit the function JSON to include a "direction":"in" statement, then do a ZIP file of the build folder (like bin\Release\net6.0\publish* , so the host.json is on the root), then execute az functionapp deployment source config-zip -g resource-group -n app-name --src publish.zip

More info on the ZIP file structure in https://learn.microsoft.com/en-us/azure/azure-functions/deployment-zip-push#deployment-zip-file-requirements

Remember .NET will produce a function.json outside the publish folder. The JSON that must be edited is INSIDE the publish folder, i.e. \net6.0\publish\FunctionName\function.json

  "bindings": [
    {
      "type": "eventGridTrigger",
      "name": "eventGridEvent"
    },
    {
      "type": "blob",
      "connection": "blobStorageConnection",
      "blobPath": "{data.url}",
      "access": 1,
      "name": "incomingPlate",
      "direction": "in"
    }
  ],