Run a python Azure Function in the python program
Run a python Azure Function in the python program
MODIFY
azure-py-functions-raw/README.md
azure-py-functions-raw/README.md
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)
-# Azure Functions
+# Azure Functions in Python
-Azure Functions created from raw deployment packages in C#.
-
-C# is a precompiled language, and the deployment artifact contains compiled binaries. You will need the following tool to build this project:
-
-- [.NET Core SDK](https://dotnet.microsoft.com/download) for the .NET Function App
-
-Please remove the corresponding resources from the program in case you don't need those runtimes.
+Azure Functions developed and deployed with Python.
## Running the App
-1. Build and publish the .NET Function App project:
-
- ```bash
- $ dotnet publish dotnet
- ```
-
1. Create a new stack:
```bash
@@ -63,8 +51,8 @@ Please remove the corresponding resources from the program in case you don't nee
1. Check the deployed function endpoints:
```bash
- $ pulumi stack output dotnet_endpoint
- https://http-dotnet1a2d3e4d.azurewebsites.net/api/HelloDotnet?name=Pulumi
- $ curl "$(pulumi stack output dotnet_endpoint)"
- Hello from .NET, Pulumi
+ $ pulumi stack output endpoint
+ https://http1a2d3e4d.azurewebsites.net/api/HelloPython?name=Pulumi
+ $ curl "$(pulumi stack output endpoint)"
+ Hello from Python, Pulumi!
```

MODIFY
azure-py-functions-raw/__main__.py
azure-py-functions-raw/__main__.py
from pulumi import asset, export, Output
from pulumi_azure import core, storage, appservice
-resource_group = core.ResourceGroup("windowsrg")
+resource_group = core.ResourceGroup("linuxrg")
-httpdotnet_storage_account = storage.Account(
- "httpdotnet",
+http_storage_account = storage.Account(
+ "http",
account_kind="StorageV2",
account_tier="Standard",
account_replication_type="LRS",
resource_group_name=resource_group.name,
)
-httpdotnet_container=storage.Container(
- "http-dotnet",
- storage_account_name=httpdotnet_storage_account.name,
+http_container=storage.Container(
+ "http",
+ storage_account_name=http_storage_account.name,
container_access_type="private"
)
-httpdotnet_zib_blob=storage.Blob(
- "http-dotnet",
- storage_account_name=httpdotnet_storage_account.name,
- storage_container_name=httpdotnet_container.name,
+http_zib_blob=storage.Blob(
+ "http",
+ storage_account_name=http_storage_account.name,
+ storage_container_name=http_container.name,
type="Block",
source=asset.AssetArchive({
- ".": asset.FileArchive("./dotnet/bin/Debug/netcoreapp3.1/publish")
+ ".": asset.FileArchive("./python")
}))
def get_sas(args):
@@ -43,33 +43,35 @@ def get_sas(args):
)
return f"https://{args[0]}.blob.core.windows.net/{args[2]}/{args[3]}{blob_sas.sas}"
-httpdotnet_signed_blob_url = Output.all(
- httpdotnet_storage_account.name,
- httpdotnet_storage_account.primary_connection_string,
- httpdotnet_container.name, httpdotnet_zib_blob.name
+http_signed_blob_url = Output.all(
+ http_storage_account.name,
+ http_storage_account.primary_connection_string,
+ http_container.name, http_zib_blob.name
).apply(get_sas)
-httpdotnet_plan=appservice.Plan(
- "http-dotnet",
+http_plan=appservice.Plan(
+ "http",
resource_group_name=resource_group.name,
- kind="FunctionApp",
+ kind="Linux",
sku={
"tier": "Dynamic",
"size": "Y1"
- }
+ },
+ reserved="true"
)
-httpdotnet_function_app=appservice.FunctionApp(
- "http-dotnet",
+http_function_app=appservice.FunctionApp(
+ "http",
resource_group_name=resource_group.name,
- app_service_plan_id=httpdotnet_plan.id,
- storage_connection_string=httpdotnet_storage_account.primary_connection_string,
+ app_service_plan_id=http_plan.id,
+ storage_account_name=http_storage_account.name,
+ storage_account_access_key=http_storage_account.primary_access_key,
version="~3",
app_settings={
- "runtime": "dotnet",
- "WEBSITE_RUN_FROM_PACKAGE": httpdotnet_signed_blob_url,
+ "FUNCTIONS_WORKER_RUNTIME": "python",
+ "WEBSITE_RUN_FROM_PACKAGE": http_signed_blob_url,
},
)
-export("dotnet_endpoint", httpdotnet_function_app.default_hostname.apply(
- lambda endpoint: "https://" + endpoint + "/api/HelloDotnet?name=Pulumi"))
+export("endpoint", http_function_app.default_hostname.apply(
+ lambda endpoint: "https://" + endpoint + "/api/HelloPython?name=Pulumi"))

DELETE
azure-py-functions-raw/dotnet/HelloDotnet.cs
-using System;
-using System.IO;
-using System.Threading.Tasks;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.Azure.WebJobs;
-using Microsoft.Azure.WebJobs.Extensions.Http;
-using Microsoft.AspNetCore.Http;
-using Microsoft.Extensions.Logging;
-using Newtonsoft.Json;
-
-namespace functionapp_dotnet
-{
- public static class HelloDotnet
- {
- [FunctionName("HelloDotnet")]
- public static async Task<IActionResult> Run(
- [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] 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 from .NET, {name}")
- : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
- }
- }
-}

DELETE
azure-py-functions-raw/dotnet/functionapp-dotnet.csproj
-<Project Sdk="Microsoft.NET.Sdk">
- <PropertyGroup>
- <TargetFramework>netcoreapp3.1</TargetFramework>
- <AzureFunctionsVersion>v3</AzureFunctionsVersion>
- <RootNamespace>functionapp_dotnet</RootNamespace>
- </PropertyGroup>
- <ItemGroup>
- <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.4" />
- </ItemGroup>
- <ItemGroup>
- <None Update="host.json">
- <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
- </None>
- </ItemGroup>
-</Project>

DELETE
azure-py-functions-raw/dotnet/host.json
-{
- "version": "2.0"
-}
\ No newline at end of file

ADD
azure-py-functions-raw/python/HelloPython/__init__.py
+import logging
+
+import azure.functions as func
+
+
+def main(req: func.HttpRequest) -> func.HttpResponse:
+ logging.info('Python HTTP trigger function processed a request.')
+
+ name = req.params.get('name')
+ if not name:
+ try:
+ req_body = req.get_json()
+ except ValueError:
+ pass
+ else:
+ name = req_body.get('name')
+
+ if name:
+ return func.HttpResponse(f"Hello from Python, {name}!")
+ else:
+ return func.HttpResponse(
+ "Please pass a name on the query string or in the request body",
+ status_code=400
+ )

ADD
azure-py-functions-raw/python/HelloPython/function.json
+{
+ "scriptFile": "__init__.py",
+ "bindings": [
+ {
+ "authLevel": "anonymous",
+ "type": "httpTrigger",
+ "direction": "in",
+ "name": "req",
+ "methods": [
+ "get",
+ "post"
+ ]
+ },
+ {
+ "type": "http",
+ "direction": "out",
+ "name": "$return"
+ }
+ ]
+}

ADD
azure-py-functions-raw/python/host.json
+{
+ "version": "2.0",
+ "extensionBundle": {
+ "id": "Microsoft.Azure.Functions.ExtensionBundle",
+ "version": "[1.*, 2.0.0)"
+ }
+}

ADD
azure-py-functions-raw/python/requirements.txt
azure-py-functions-raw/python/requirements.txt
Run a python Azure Function in the python program (#741)

Run a python Azure Function in the python program