Azure function working in local but not after uploading to azure - azure

I'm new with azure functions and azure as whole.
I'm using azure functions for interacting with a SignalR service for usage in a xamarin forms app. I borrowed the azure function code from docs.MSDocs on serverless signalR service
Worked well in local (tested the web client provided in the docs, And also a simple console app).
But when the function was moved to azure. Initially I faced CORS error fixed those and then faced 502. Could not debug or find the root cause. After few hours of browsing found that azure itself provides a template for signalR serverless connection.
Used the template, configured the app settings with signalR endpoints (I have set the app setting for AzureSignalRConnectionString).
Still facing 502 error, . How can I get it to work? Or How do I find out the root cause for the failure.
Negotiate function code:
index.js
module.exports = async function (context, req, connectionInfo) {
context.res.body = connectionInfo;
};
Function.json
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"methods": [
"post"
],
"name": "req"
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "signalRConnectionInfo",
"name": "connectionInfo",
"hubName": "messaage",
"connectionStringSetting": "AzureSignalRConnectionString",
"direction": "in"
}
]
}

SignalR Service needs a URL to access Function App when you're using SignalR Service trigger binding.
The URL format: <Function_App_URL>/runtime/webhooks/signalr?code=<API_KEY>. Explanation: The Function_App_URL can be found on Function App's Overview page and The API_KEY is generated by Azure Function. You can get the API_KEY from signalr_extension in the App keys blade of Function App.
And if you are very new to this, here is a step by step article to follow: https://github.com/aspnet/AzureSignalR-samples/tree/master/samples/BidirectionChat

Related

Azure Function in ASE not triggred when receiving a message in an event hub

We are trying to deploy an Azure Function in an App service Environment in an Isolated App Service Plan.
This Azure Function should be triggered each time we receive a message in an event hub.
Unfortunately it doesn't seem to work even though we've tried different network configurations :
Authorised ASE subnet in the networking section in Event Hubs Namespace
White listed both subnet IP ranges and ASE Outbound IP Addresses.
And Allowed trusted Microsoft services.
Could you please help us debug this situation in order to understand why our Azure Function is not triggered.
Here are the steps to create function app that triggers when there is an event generated in EventHub try to check these steps and make required changes in your code to execute your requirement.
Add the below code in your local.settings.json file z
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsStorage": [Paste the Storage account connection string which is linked with your Azure function that you have Created on Azure Portal],
"FUNCTIONS_EXTENSION_VERSION": "~2",
"receiverConnectionString": [Paste the Endpoint Connection String of the EventHubNamespace here in double quotes and remove these brackets.],
"MyStorageConnectionString": [Paste the Endpoint Connection String of the blob storage account here in double quotes and remove these brackets.]
},
"ConnectionStrings": {}
}
Below is the code that will bind azure function with EventHub so that it will result in triggering message automatically when there is any event generated in EventHub.
{
"scriptFile": "__init__.py",
"bindings": [{
"type": "eventHubTrigger",
"name": "events",
"direction": "in",
"eventHubName": [Enter the name of your event hub in double quotes and remove these brackets.],
"connection": "receiverConnectionString",
"cardinality": "many","consumerGroup": "$Default",
"dataType": "binary"
},
{
"name": "outputblob",
"type": "blob",
"path": [Enter the path of the folder where you want to store the data in double quotes and remove these brackets.],
"connection": "MyStorageConnectionString",
"direction": "out"
}]
}
For Complete information check Azure Functions and EventHub.
Also, Check these SO threads for related information. SO1, SO2 and these Microsoft Q&A

Azure Function Trigger not working for a different resource id of cosmosdb

I am trying to create an azure function cosmosdbtrigger .My cosmosdb is in a different resource id as compared to my azure function. However My function is not getting triggered.
Is there any restriction that the azure function and cosmosdb should be in the same resource id. If not is there any additional setting to be done for a different resource id.
My azure function is on python running on a linux app service. From the azure documentation i came to know, i cannot mix app services from windows and linux as the current limitation.
Azure Documentation on Current Limitation
I need to use an azure function Python to check azure cosomos db change feed.
Here is my function.json used for connecting to a cosmosdb collection trigger..
{
"scriptFile": "__init__.py",
"bindings": [
{
"type": "cosmosDBTrigger",
"name": "documents",
"direction": "in",
"leaseCollectionName": "leases1",
"connectionStringSetting": "devcosmosdb_DOCUMENTDB",
"databaseName": "devcosmosdb",
"collectionName": "testCollection",
"createLeaseCollectionIfNotExists": "true"
}
]
}
There is no such limitation.
Please check the databaseName, collectionName and connectionStringSetting again.
If you have already deployed the function to Azure portal. You need to add the connectionStringSetting to Application Settings. In your scenario, you should add the connectionString like this
You can find the connectionString under Keys part of your cosmosdb account.
Also, please check the FireWall settings.
I would like to add on some more useful information related to this topic. You might be the old me looking at the Monitor logs waiting for something to appear when you use the default template of the __init__.py to get update on the inserted document to your CosmosDB.
May I refer you to this link - https://learn.microsoft.com/en-us/azure/cosmos-db/how-to-configure-cosmos-db-trigger#enabling-logging
You need to edit the host.json file to enable logging for CosmosDB before anything useful will appear in your logs.
{
"version": "2.0",
"logging": {
"fileLoggingMode": "always",
"logLevel": {
"Host.Triggers.CosmosDB": "Trace"
}
}
}
I had for some reason changed the name in the bindings section of the function.json. If this doesn't match with the parameter name in your function definition, it will not fire.
For example, name in here:
{
"scriptFile": "__init__.py",
"bindings": [
{
"type": "cosmosDBTrigger",
"name": "documents", <<<<<<<<<<<<<
"direction": "in",
"leaseCollectionName": "leases",
"connectionStringSetting": "CosmosDBConnectionString",
"databaseName": "dbname",
"collectionName": "collname",
"createLeaseCollectionIfNotExists": true
}
]
}
Must match the variable name in the main method of __init__.py when using Python as runtime (this should be similar for other languages):
import azure.functions as func
"""
Must match with this
|
|
V
"""
def main(documents: func.DocumentList):
# do something smart with the list

ARM Deployment: Get Azure Function API Key

As part of a Stream Analytics deployment solution I want to retrieve the API key for a Azure Function App in an ARM template via e.g. the listkeys() function. Is there a way to retrieve this key via an ARM template respectively during an ARM deployment and if yes, how?
Thanks
The new Key Management API of Azure Functions has gone live. Its possible via the following ARM Script. Also check this Github issue
"variables": {
"functionAppId": "[concat(parameters('functionAppResourceGroup'),'/providers/Microsoft.Web/sites/', parameters('functionAppName'))]"
},
"resources": [
{
"type": "Microsoft.KeyVault/vaults/secrets",
"name": "[concat(parameters('keyVaultName'),'/', parameters('functionAppName'))]",
"apiVersion": "2015-06-01",
"properties": {
"contentType": "text/plain",
"value": "[listkeys(concat(variables('functionAppId'), '/host/default/'),'2016-08-01').functionKeys.default]"
},
"dependsOn": []
}
]
This question has already been answered here:
listKeys for Azure function app
Get Function & Host Keys of Azure Function In Powershell
What is important in this context is to set the 'Minimum TLS Version' to '1.0' before deploying the job. Otherwise you will get failures when testing the connection health.

API to add properties to Azure Webapp Application settings

I have one web app running on a Azure appservice plan. The web app has a lot of settings defined in Application settings of the Web App. Now I want to replicate that web app with all its Application settings. I got the REST API to list down all the settings available for any web app (/api/settings). Although there is a POST call to add/update the settings , But it is not updating Application settings.
Is there any REST API to add/update the Application settings of Azure web app ?
Thanks,
Abhiram
Is there any REST API to add/update the Application settings of Azure web app ?
Yes, we could update the application setting with the following Update Application Settings REST API
Put https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resource group}/providers/Microsoft.Web/sites/{WebAppName}/config/appsettings?api-version=2016-08-01
Body
{
"id": "subscriptions/{subscriptionId}/resourceGroups/{resource group}/providers/Microsoft.Web/sites/{WebAppName}/config/appsettings",
"name": "appsettings",
"type": "Microsoft.Web/sites/config",
"location": "South Central US",
"tags": {
"hidden-related:/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.Web/serverfarms/tomfreePlan": "empty"
},
"properties": {
"WEBSITE_NODE_DEFAULT_VERSION": "6.9.1",
"Test1": "testValue1" //Added value
}
}
Note: we could use the following List Application Settings REST API post way to list the appsetting body.
Post https://management.azure.com/subscriptions/{subscription}/resourceGroups/CXP-{resourceGroup}/providers/Microsoft.Web/sites/{WebAppName}/config/appsettings/list?api-version=2016-08-01
To my knowledge, there is not. But have you considered scripting your Web App settings with an ARM template? This is exactly the kind of thing that ARM templates are intended for.
An example of the properties section of a Web App's ARM template that lets you script appSettings and connectionStrings is listed below:
"properties": {
"name": "YourWebAppsName",
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', YourAppServicePlanName)]",
"siteConfig": {
"appSettings": [
{
"name": "someAppSettingKey",
"value": "someAppSettingValue"
},
{
"name": "someOtherAppSettingKey",
"value": "someOtherAppSettingValue"
}
],
"connectionStrings": [
{
"name": "defautlConnection",
"connectionString": "YourConnectionString",
"type": "2"
},
]
}
When you deploy an ARM template, Azure will ensure that the target resource's settings match what's specified in your template.
Visual Studio has a project type for developing and deploying these. It's the Azure Resource Group project type located under the Cloud node in the project templates.
As an added bonus, you can check these ARM templates into source control alongside your code.

I can't see proxies for Azure Function App

I have created an azure function app in VS 2017.
i've included a proxies.json file and published the app.
However I can't see the proxies in the Azure portal.
All it says is:
Proxies (preview) (Read Only)
I have gone into function settings, and there are no settings to enable proxies. I think that was the old method, (not for developing the function in visual studio)
proxies.json looks like this:
{
"$schema": "http://json.schemastore.org/proxies",
"proxies": {
"user": {
"matchCondition": {
"methods": [ "GET" ],
"route": "/user/{user}"
},
"backendUri": "https://<mycontainer>.blob.core.windows.net/html/test.html/{user}"
}
}
}
Ok I figured it out....
the proxies.json file should have copy to output directory as "copy always" in the properties panel in VS.

Resources