How to create a function app in Azure Portal without VS - JackyChiou/jackychiou.github.io GitHub Wiki
Here is the steps that create a function app in Azure Portal without VS.
-
New function
-
HTTP Trigger
-
Enter a Name.
-
View files -> Add -> function.proj file -> copy my sample into it -> Save
Please copy below xml doc into function.proj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.ResourceManager.Fluent" Version="1.27.2" />
<PackageReference Include="Microsoft.Azure.Management.Websites" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
</ItemGroup>
</Project>
- Click “run.csx” file -> copy my code and paste it -> Save -> Test it.
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;
using System.Linq;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
int capacity;
if (!string.IsNullOrEmpty(req.Query["capacity"]) && int.TryParse(req.Query["capacity"], out capacity))
{
log.LogInformation("You want to chagne the capacity to "+ capacity.ToString());
// Step1. How to: Use the portal to create an Azure AD application and service principal that can access resources
// https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal
// Step2. How to: Add app roles in your application and receive them in the token
// https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-add-app-roles-in-azure-ad-apps
// For security concerns, I only give the resource group => contributor
// and the service plan read permission
var subscriptionId = "Your Subscription Id";
var appId = "e6f6d231-Your-App-Id";
var secretKey = "2z]pj0vy2JRapjo:R@TJSoW1AmOCC=o8";
var tenantId = "72f988bf-Your Tenant-id";
var resourceGroup = "jackywebl1rg";
var servicePlanName = "ASP-jackywebl1rg-81ee";
// Step 3. change capacity
var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
ClientCredential clientCredential = new ClientCredential(appId, secretKey);
var tokenResponse = context.AcquireTokenAsync("https://management.azure.com/", clientCredential).Result;
var accessToken = tokenResponse.AccessToken;
TokenCredentials credential = new TokenCredentials(accessToken);
var webSiteManagementClient = new Microsoft.Azure.Management.WebSites.WebSiteManagementClient(credential);
webSiteManagementClient.SubscriptionId = subscriptionId;
var servicePlan = webSiteManagementClient.AppServicePlans.ListByResourceGroupWithHttpMessagesAsync(resourceGroup).Result.Body.Where(x => x.Name.Equals(servicePlanName)).FirstOrDefault();
var appServicePlanRequest = await webSiteManagementClient.AppServicePlans.ListByResourceGroupWithHttpMessagesAsync(resourceGroup);
appServicePlanRequest.Body.ToList().ForEach(x => log.LogInformation($">>>{x.Name}"));
var appServicePlan = appServicePlanRequest.Body.Where(x => x.Name.Equals(servicePlanName)).FirstOrDefault();
if (appServicePlan == null)
{
log.LogError("Could not find app service plan.");
}
//scale up/down
//servicePlan.Sku.Family = "P";
//servicePlan.Sku.Name = "P1v2";
//servicePlan.Sku.Size = "P1v2";
//servicePlan.Sku.Tier = "PremiumV2";
servicePlan.Sku.Capacity = capacity; // scale out: number of instances
var updateResult = webSiteManagementClient.AppServicePlans.CreateOrUpdateWithHttpMessagesAsync(resourceGroup, servicePlanName, servicePlan).Result;
log.LogInformation("Completed!!");
return (ActionResult)new OkObjectResult($"Hello, {capacity} {updateResult.Response.StatusCode}");
}
else
{
return new BadRequestObjectResult("Please pass a capacity on the query string or in the request body");
}
}
HTH. By Jacky 2019-11-18