below in the docs<\/a>, outside of the example given, they do use “connection” instead of “connectionStringSetting”).<\/p>\n{\r\n \"bindings\": [\r\n {\r\n \"authLevel\": \"FUNCTION\",\r\n \"type\": \"httpTrigger\",\r\n \"direction\": \"in\",\r\n \"name\": \"Request\",\r\n \"methods\": [\r\n \"get\",\r\n \"post\"\r\n ]\r\n },\r\n {\r\n \"type\": \"http\",\r\n \"direction\": \"out\",\r\n \"name\": \"Response\"\r\n },\r\n {\r\n \"name\": \"outputDocument\",\r\n \"direction\": \"out\",\r\n \"type\": \"cosmosDB\",\r\n \"connection\": \"rakcosmosdb01224_DOCUMENTDB\",\r\n \"databaseName\": \"rakcosmosdb\",\r\n \"containerName\": \"appRegistrations-Prod\"\r\n }\r\n ]\r\n}<\/pre>\nScreenshot of the v4.x settings:<\/p>\n
<\/p>\n
And… test!<\/p>\n
Still I get the error.<\/p>\n
Here’s a thought, maybe it’s not happy with the connection string name? Let me change it from “rakcosmosdb01224_DOCUMENTDB” to “rakcosmosdb01224” both in function.json<\/code> and the two configuration settings.<\/p>\n<\/p>\n
Nope, no luck.<\/p>\n
And then I noticed a weird thing. Even though I had updated function.json<\/code>, when I went to a different page and came back the changes had reverted. Huh. It wasn’t a one-time – I tried it couple of times with the same result.<\/p>\nSo I removed the following from the file (this is in the incorrect bit):<\/p>\n
{\r\n \"name\": \"outputDocument\",\r\n \"direction\": \"out\",\r\n \"type\": \"cosmosDB\",\r\n \"connectionStringSetting\": \"rakcosmosdb01224\",\r\n \"databaseName\": \"rakcosmosdb\",\r\n \"collectionName\": \"appRegistrations-Prod\"\r\n }<\/pre>\nSaved it. Went to the Integration section. Ensured it’s removed, and then I added it back correctly.<\/p>\n
{\r\n \"name\": \"outputDocument\",\r\n \"direction\": \"out\",\r\n \"type\": \"cosmosDB\",\r\n \"connection\": \"rakcosmosdb01224\",\r\n \"databaseName\": \"rakcosmosdb\",\r\n \"containerName\": \"appRegistrations-Prod\"\r\n }<\/pre>\nOddly, this time when I ran the Function it didn’t complain… but looks like everything I added to function.json<\/code> disappeared. What the heck. When I ran the Function again it complained that [Error] ERROR: The specified name ‘outputDocument’ cannot be resolved to a valid output binding of this function.<\/em><\/p>\nI don’t know what’s going on here. I repeated the steps, and again it kept disappearing.<\/p>\n
At this point I am just going to delete the Function and recreate it. Not the Function App, just the Function. Here’s the code as a backup:<\/p>\n
using namespace System.Net\r\n\r\n# Input bindings are passed in via param block.\r\nparam($Request, $TriggerMetadata)\r\n\r\n# Write to the Azure Functions log stream.\r\nWrite-Host \"PowerShell HTTP trigger function processed a request.\"\r\n\r\n# Interact with query parameters or the body of the request.\r\n$name = $Request.Query.Name\r\nif (-not $name) {\r\n $name = $Request.Body.Name\r\n}\r\n\r\n$body = \"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.\"\r\n\r\nif ($name) {\r\n $body = \"Hello, $name. This HTTP triggered function executed successfully.\"\r\n}\r\n\r\n$objectId = Get-Random -Minimum 60 -Maximum 300\r\nWrite-Host \"Writing $objectId to the DB\"\r\nPush-OutputBinding -Name outputDocument -ErrorAction Stop -Value\u202f@{ \r\n objectId\u202f= $objectId\r\n name\u202f=\u202f\"Rakhesh\" \r\n} \r\n\r\n# Associate values to output bindings by calling 'Push-OutputBinding'.\r\nPush-OutputBinding -Name Response -Value ([HttpResponseContext]@{\r\n StatusCode = [HttpStatusCode]::OK\r\n Body = $body\r\n})<\/pre>\nOk, new Function created. Copy pasted the stuff. But nope, now it complains it cannot find the output binding! Coz it has disappeared.<\/p>\n
If I re-add it, restart the Function App, the part I added stays. Yet, doing a Test\/Run seems to nuke it.<\/p>\n
At this point I went into a stupid cycle of copy pasting it again, trying to restart the Function App a few times etc… until I think I finally made it stick. I have no idea what I did. It was just stupid persistence and frustration I think.<\/p>\n
On the plus side, now that it sticks, while the console continues to give an error the message different.<\/p>\n
Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: fb0616b6-9c09-4be9-bb3f-0b6c034c4ae9; Reason: (Message: {“Errors”:[“One of the specified inputs is invalid”]} ActivityId: fb0616b6-9c09-4be9-bb3f-0b6c034c4ae9, Request URI: \/apps\/0b12014b-f854-44a4-8f6f-c5cedc3481fe\/services\/4919abae-b1f5-4b75-9186-772877134ddd\/partitions\/78009af3-c91e-4793-b1f9-77ad07aa126b\/replicas\/133345776092212266p\/, RequestStats: RequestStartTime: 2023-07-23T19:08:18.6420113Z, RequestEndTime: 2023-07-23T19:08:18.6435872Z, Number of regions attempted…<\/p><\/blockquote>\n
A quick Google pointed me to the answer<\/a>: I should be adding an Id property, of type string.<\/p>\nPush-OutputBinding -Name outputDocument -ErrorAction Stop -Value\u202f@{\r\n id = \"$objectId\"\r\n objectId\u202f= $objectId\r\n name\u202f=\u202f\"Rakhesh\" \r\n}<\/pre>\nDid that, and finally finally finally it works!<\/p>\n
<\/p>\n
Phew.<\/p>\n
To Summarize?<\/h3>\n
I’ve lost the plot of things I did here. \ud83d\ude03 So here’s a summary for next time:<\/p>\n
\n- Create the Cosmos DB as usual (detailed above).<\/li>\n
- On the Function App side, if you are using the v4.x extension, then add the following to
host.json<\/code>.<\/li>\n<\/ul>\n{\r\n \"version\": \"2.0\",\r\n \"managedDependency\": {\r\n \"Enabled\": true\r\n },\r\n \"extensionBundle\": {\r\n \"id\": \"Microsoft.Azure.Functions.ExtensionBundle.Preview\",\r\n \"version\": \"[4.0.0, 5.0.0)\"\r\n },\r\n \"extensions\": {\r\n \"cosmosDB\": {\r\n \"connectionMode\": \"Gateway\",\r\n \"userAgentSuffix\": \"rakfunction12334\"\r\n }\r\n }\r\n}<\/pre>\n
\n- Since we’ll be using a Managed Identity, enable that (Azure Function).<\/li>\n
- Modify
requirements.psd1<\/code> to install the Az module.<\/li>\n<\/ul>\n# This file enables modules to be automatically managed by the Functions service.\r\n# See https:\/\/aka.ms\/functionsmanageddependency for additional information.\r\n#\r\n@{\r\n # For latest supported version, go to 'https:\/\/www.powershellgallery.com\/packages\/Az'. \r\n # To use the Az module in your function app, please uncomment the line below.\r\n 'Az' = '10.*'\r\n}\r\n<\/pre>\n
\n- Run the following in Cloud Shell to grant the Function App Managed Identity Write access to the Cosmos DB account.<\/li>\n<\/ul>\n
# Change these to match the environment and role requirement\r\n$functionAppName = \"rakcosmostest12334\"\r\n$cosmosdbAcctName = \"rakcosmosdb01224\"\r\n$cosmosdbRg = \"cosmosdbtest\"\r\n$roleName = \"Cosmos DB Built-in Data Contributor\" # If using a custom role, be sure to create it before hand; the only other built-in role is 'Cosmos DB Built-in Data Reader'\r\n\r\n# Nothing to change past this... \r\n$principalId = (Get-AzADServicePrincipal -DisplayName $functionAppName).Id\r\n\r\n$roleId = (Get-AzCosmosDBSqlRoleDefinition -AccountName $cosmosdbAcctName -ResourceGroupName $cosmosdbRg | Where-Object { $_.RoleName -eq $roleName }).Id\r\n\r\nNew-AzCosmosDBSqlRoleAssignment -AccountName $cosmosdbAcctName `\r\n -ResourceGroupName $cosmosdbRg `\r\n -RoleDefinitionId $roleId `\r\n -Scope \"\/\" `\r\n -PrincipalId $principalId<\/pre>\n
\n- Add two app settings under the Configuration section of the Function App.<\/li>\n<\/ul>\n
\n\n\nrakcosmosdb01224__accountEndpoint<\/strong><\/td>\nhttps:\/\/rakcosmosdb01224.documents.azure.com:443\/<\/strong><\/td>\n<\/tr>\n\nrakcosmosdb01224__credential<\/strong><\/td>\nmanagedidentity<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n All the higlighted ones need changing with the correct URL and replacement of the “rakcosmosdb01224” bit.<\/p>\n \n- Create a new Function. Do not add the Cosmos DB output binding via the Portal<\/strong>. Instead, edit
function.json<\/code> and add it.<\/li>\n<\/ul>\n{\r\n \"bindings\": [\r\n {\r\n \"authLevel\": \"FUNCTION\",\r\n \"type\": \"httpTrigger\",\r\n \"direction\": \"in\",\r\n \"name\": \"Request\",\r\n \"methods\": [\r\n \"get\",\r\n \"post\"\r\n ]\r\n },\r\n {\r\n \"type\": \"http\",\r\n \"direction\": \"out\",\r\n \"name\": \"Response\"\r\n },\r\n {\r\n \"name\": \"outputDocument\",\r\n \"direction\": \"out\",\r\n \"type\": \"cosmosDB\",\r\n \"connection\": \"rakcosmosdb01224\",\r\n \"databaseName\": \"rakcosmosdb\",\r\n \"containerName\": \"appRegistrations-Prod\"\r\n }\r\n ]\r\n}<\/pre>\nI added the highlighted lines, including a comma above it in the JSON. Replace the “connection”, “databaseName”, and “containerName” as needed.<\/p>\n | | | |