Search code examples
azureazure-functionsazure-resource-manager

How do I create an Azure Function App v4 using Azure Resource Manager API


Consider the following code that worked for function apps prior to v4:

var bindingsConfig = new
{
    bindings = new object[]
        {
            new
            {
                AuthLevel = "function",
                Type = "httpTrigger",
                Direction = "in",
                Name = "request",
                Methods = new[] {"post"}
            },
            new
            {
                Type = "http",
                Direction = "out",
                Name = "$return"
            }
        }
};

app = await myResourceGroup.GetWebSites().GetAsync("my-app-name", ct);
var func = await app.GetSiteFunctions().CreateOrUpdateAsync(WaitUntil.Completed, "my-func-name", new FunctionEnvelopeData
{
    Config = BinaryData.FromObjectAsJson(config)
}, ct);

Now according to this doc, function.json is no longer used in v4 programming model. My question is - does it mean I shouldn't be passing bindingsConfig into CreateOrUpdateAsync()? When I try it though I get the following error:

Azure.RequestFailedException: 'Error creating function. <my-func-name> is not a valid function name
Status: 400 (Bad Request)
ErrorCode: BadRequest

In general how should I call CreateOrUpdateAsync() to create a v4 kind of function?


Solution

  • For those interested in resolution, it turns out that the call to app.GetSiteFunctions().CreateOrUpdateAsync() should never be used for creating function v4. This call results in creation of the v3 style of function.json which gets in conflict with the v4 programming model (v3 and v4 programming models shouldn't be mixed in one app).

    It is also important to make sure you call npm install if the node_modules folder is missing (in Kudu UI).