Last active
November 20, 2018 18:41
-
-
Save mouadcherkaoui/586f054ed08ebb43786c875b0752be75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", | |
"contentVersion": "1.0.0.0", | |
"parameters": { | |
"functionAppName": { | |
"type": "string", | |
"metadata": { | |
"description": "The name of the function app that you wish to create." | |
} | |
}, | |
"sku": { | |
"type": "string", | |
"allowedValues": [ | |
"Free", | |
"Shared", | |
"Basic", | |
"Standard" | |
], | |
"defaultValue": "Standard", | |
"metadata": { | |
"description": "The pricing tier for the hosting plan." | |
} | |
}, | |
"workerSize": { | |
"type": "string", | |
"allowedValues": [ | |
"0", | |
"1", | |
"2" | |
], | |
"defaultValue": "0", | |
"metadata": { | |
"description": "The instance size of the hosting plan (small, medium, or large)." | |
} | |
}, | |
"storageAccountType": { | |
"type": "string", | |
"defaultValue": "Standard_LRS", | |
"allowedValues": [ | |
"Standard_LRS", | |
"Standard_GRS", | |
"Standard_RAGRS" | |
], | |
"metadata": { | |
"description": "Storage Account type" | |
} | |
}, | |
"location": { | |
"type": "string", | |
"defaultValue": "[resourceGroup().location]", | |
"metadata": { | |
"description": "Location for all resources." | |
} | |
}, | |
"functionName":{ | |
"type":"string" | |
}, | |
"functionFile": { | |
"type": "string", | |
"metadata": { | |
"description": "The name of the function app that you wish to create." | |
} | |
}, | |
"proxiesFile": { | |
"type": "string", | |
"metadata": { | |
"description": "The name of the function app that you wish to create." | |
} | |
}, | |
"functionSchedule": { | |
"type":"string" , | |
"defaultValue": "0 15 0 0 0" | |
} | |
}, | |
"variables": { | |
"functionName":"[parameters('functionName')]", | |
"functionsAppName": "[parameters('functionAppName')]", | |
"functionSchedule":"[parameters('functionSchedule')]", | |
"hostingPlanName": "[parameters('functionAppName')]", | |
"functionFile": "[parameters('functionFile')]", | |
"proxiesFile": "[parameters('proxiesFile')]", | |
"storageAccountName": "[concat(uniquestring(resourceGroup().id), 'functions')]" | |
}, | |
"resources": [ | |
{ | |
"type": "Microsoft.Storage/storageAccounts", | |
"name": "[variables('storageAccountName')]", | |
"apiVersion": "2016-12-01", | |
"location": "[parameters('location')]", | |
"kind": "Storage", | |
"sku": { | |
"name": "[parameters('storageAccountType')]" | |
} | |
}, | |
{ | |
"type": "Microsoft.Web/serverfarms", | |
"apiVersion": "2015-04-01", | |
"name": "[variables('hostingPlanName')]", | |
"location": "[parameters('location')]", | |
"properties": { | |
"name": "[variables('hostingPlanName')]", | |
"sku": "[parameters('sku')]", | |
"workerSize": "[parameters('workerSize')]", | |
"hostingEnvironment": "", | |
"numberOfWorkers": 1 | |
} | |
}, | |
{ | |
"name": "[concat(variables('functionsAppName'), '/', variables('functionName'))]", | |
"type": "Microsoft.Web/sites/functions", | |
"apiVersion": "2015-08-01", | |
"properties": { | |
"config": { | |
"bindings": [ | |
{ | |
"name": "myTimer", | |
"type": "timerTrigger", | |
"direction": "in", | |
"schedule": "[variables('functionSchedule')]" | |
} | |
], | |
"disabled": false | |
}, | |
"files": { | |
"run.csx": "[variables('functionFile')]", | |
"proxies.json": "[variables('proxiesFile')]" | |
} | |
}, | |
"dependsOn": [ | |
"[resourceId('Microsoft.Web/sites', variables('functionsAppName'))]" | |
] | |
}, | |
{ | |
"apiVersion": "2015-04-01", | |
"type": "Microsoft.Web/sites", | |
"name": "[variables('functionsAppName')]", | |
"location": "[parameters('location')]", | |
"kind": "functionapp", | |
"properties": { | |
"name": "[variables('functionsAppName')]", | |
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]", | |
"hostingEnvironment": "", | |
"clientAffinityEnabled": false, | |
"siteConfig": { | |
"alwaysOn": true | |
} | |
}, | |
"dependsOn": [ | |
"[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]", | |
"[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]" | |
], | |
"resources": [ | |
{ | |
"apiVersion": "2016-03-01", | |
"name": "appsettings", | |
"type": "config", | |
"dependsOn": [ | |
"[resourceId('Microsoft.Web/sites', variables('functionsAppName'))]", | |
"[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]" | |
], | |
"properties": { | |
"AzureWebJobsStorage": "[concat('DefaultEndpointsProtocol=https;AccountName=',variables('storageAccountName'),';AccountKey=',listkeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2015-05-01-preview').key1,';')]", | |
"AzureWebJobsDashboard": "[concat('DefaultEndpointsProtocol=https;AccountName=',variables('storageAccountName'),';AccountKey=',listkeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2015-05-01-preview').key1,';')]", | |
"FUNCTIONS_EXTENSION_VERSION": "~1" | |
} | |
} | |
] | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
there is no way to add proxies to a function app other than deploying the proxies.json file, here is the trick, first, you prepare a template for the function, a simple one, and add the proxies.json file to the list of files, let's take a look at the parameters and variables portion of the deploy.json file: | |
"parameters": { | |
"functionAppName": { | |
"type": "string", | |
"metadata": { | |
"description": "The name of the function app that you wish to create." | |
}, | |
"functionName":{ | |
"type":"string" | |
}, | |
"functionFile": { | |
"type": "string" | |
}, | |
"proxiesFile": { | |
"type": "string" | |
} | |
}, | |
"variables": { | |
"functionName":"[parameters('functionName')]", | |
"functionsAppName": "[parameters('functionAppName')]", | |
"functionFile": "[parameters('functionFile')]", | |
"proxiesFile": "[parameters('proxiesFile')]", | |
"storageAccountName": "[concat(uniquestring(resourceGroup().id), 'functions')]" | |
}, | |
we declare input parameters that we can use in our template as variables, and the in the function resource declaration we use our variables: | |
{ | |
"name": "[concat(variables('functionsAppName'), '/', variables('functionName'))]", | |
"type": "Microsoft.Web/sites/functions", | |
"apiVersion": "2015-08-01", | |
"properties": { | |
"config": { | |
"bindings": [ | |
{ | |
"name": "myTimer", | |
"type": "timerTrigger", | |
"direction": "in", | |
"schedule": "[variables('functionSchedule')]" | |
} | |
], | |
"disabled": false | |
}, | |
"files": { | |
"run.csx": "[variables('functionFile')]", | |
"proxies.json": "[variables('proxiesFile')]" | |
} | |
}, | |
"dependsOn": [ | |
"[resourceId('Microsoft.Web/sites', variables('functionsAppName'))]" | |
] | |
}, | |
to get the content of our files and pass it as parameters we can use for example Powershell to read each file's contents: | |
# Read the contents of the function file and assemble deployment parameters | |
$ProxiesFilePath="./proxies.json" | |
$proxiesFileContents = [System.IO.File]::ReadAllText($ProxiesFilePath) | |
$FunctionFilePath="./run.csx" | |
$functionFileContents = [System.IO.File]::ReadAllText($FunctionFilePath) | |
$templateParameters = @{} | |
$templateParameters.Add("functionFile", $functionFileContents) | |
$templateParameters.Add("proxiesFile", $proxiesFileContents) | |
and then we use Azure CLI or PowerShell to deploy our function: | |
az group deployment create \ | |
--name testFunctionDeployment \ | |
--resource-group msdn-demos-resources \ | |
--template-file deploy.json \ | |
--parameters functionFile=$templateParameters.functionFile proxiesFile=$templateParameters.proxiesFile functionName="myFunc" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "http://json.schemastore.org/proxies", | |
"proxies": { | |
"proxyHomePage": { | |
"matchCondition": { | |
"methods": [ "GET" ], | |
"route": "/" | |
}, | |
"backendUri": "%WEB_HOST%/index.html" | |
}, | |
"proxyImages": { | |
"matchCondition": { | |
"methods": [ "GET" ], | |
"route": "/images/{*restOfPath}" | |
}, | |
"backendUri": "%WEB_HOST%/images/{restOfPath}" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "Newtonsoft.Json" | |
using System.Net; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Primitives; | |
using Newtonsoft.Json; | |
public static async Task<IActionResult> Run(HttpRequest req, ILogger log) | |
{ | |
log.LogInformation("C# HTTP trigger function processed a request."); | |
string name = req.Query["name"]; | |
string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); | |
dynamic data = JsonConvert.DeserializeObject(requestBody); | |
name = name ?? data?.name; | |
return name != null | |
? (ActionResult)new OkObjectResult($"Hello, {name}") | |
: new BadRequestObjectResult("Please pass a name on the query string or in the request body"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read the contents of the function file and assemble deployment parameters | |
$ProxiesFilePath="./proxies.json" | |
$proxiesFileContents = [System.IO.File]::ReadAllText($ProxiesFilePath) | |
$FunctionFilePath="./run.csx" | |
$functionFileContents = [System.IO.File]::ReadAllText($FunctionFilePath) | |
$templateParameters = @{} | |
$templateParameters.Add("functionFile", $functionFileContents) | |
$templateParameters.Add("proxiesFile", $proxiesFileContents) | |
az group deployment create ` | |
--name testFunctionDeployment ` | |
--resource-group msdn-demos-resources ` | |
--template-file deploy.json ` | |
--parameters functionFile=$templateParameters.functionFile proxiesFile=$templateParameters.proxiesFile functionName="myFunc" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment