diff --git a/azure-py-functions/.gitignore b/azure-py-functions/.gitignore new file mode 100644 index 000000000..a3807e5bd --- /dev/null +++ b/azure-py-functions/.gitignore @@ -0,0 +1,2 @@ +*.pyc +venv/ diff --git a/azure-py-functions/.vscode/settings.json b/azure-py-functions/.vscode/settings.json new file mode 100644 index 000000000..5b80df368 --- /dev/null +++ b/azure-py-functions/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "venv/bin/python" +} \ No newline at end of file diff --git a/azure-py-functions/Pulumi.yaml b/azure-py-functions/Pulumi.yaml new file mode 100644 index 000000000..bcb077e1d --- /dev/null +++ b/azure-py-functions/Pulumi.yaml @@ -0,0 +1,6 @@ +name: azure-py-functions +runtime: + name: python + options: + virtualenv: venv +description: A minimal Azure Native Function in Python with Pulumi program diff --git a/azure-py-functions/README.md b/azure-py-functions/README.md new file mode 100644 index 000000000..03d264e13 --- /dev/null +++ b/azure-py-functions/README.md @@ -0,0 +1,182 @@ +# Deploying Serverless Applications with Azure Functions + +You will deploy a Azure Function Apps with HTTP-triggered serverless functions in python + +## Running the App + +1. Login to Azure CLI (you will be prompted to do this during deployment if you forget this step): + + ``` + $ az login + ``` + +1. Create a new stack: + + ``` + $ pulumi stack init dev + ``` + + +1. Create a virtual environment and install python dependencies + + ### Windows + Run the following command to create a virtual environment + ```bash + python -m venv venv + ``` + + Activate the environment: + ```bash + venv\Scripts\activate + ``` + + Install dependencies: + ```bash + pip3 install -r requirements.txt + ``` + ### Mac and Linux + Run the following command to create a virtual environment + ```bash + python3 -m venv venv + ``` + + Activate the environment: + ```bash + source venv/bin/activate + ``` + + Install dependencies: + ```bash + pip3 install -r requirements.txt + ``` + + At this point, dependencies will be installed into your virtual environment. **If you close your terminal at any time**, you may need to re-activate the environment: + ```bash + source venv/bin/activate + ``` + +1. Configure the location to deploy the resources to. The Azure region to deploy to is pre-set to **WestUS** - but you can modify the region you would like to deploy to. + + ```bash + pulumi config set azure-native:location eastus2 + ``` + + [pulumi config set](https://www.pulumi.com/docs/reference/cli/pulumi_config_set/) allows us to pass in [configuration values](https://www.pulumi.com/docs/intro/concepts/config/#setting-and-getting-configuration-values) from the command line. + Feel free to choose any Azure region that supports the services used in these labs ([see this infographic](https://azure.microsoft.com/en-us/global-infrastructure/regions/) for current list of available regions). A list of some of the regions: + + ``` + centralus,eastasia,southeastasia,eastus,eastus2,westus,westus2,northcentralus,southcentralus, + westcentralus,northeurope,westeurope,japaneast,japanwest,brazilsouth,australiasoutheast,australiaeast, + westindia,southindia,centralindia,canadacentral,canadaeast,uksouth,ukwest,koreacentral,koreasouth, + francecentral,southafricanorth,uaenorth,australiacentral,switzerlandnorth,germanywestcentral, + norwayeast,jioindiawest,australiacentral2 + ``` + + The command updates and persists the value to the local `Pulumi.dev.yaml` file. You can view or edit this file at any time to effect the configuration of the current stack. + +1. Azure Python Function Zip file + The applications settings configure the app to run on Python3 deploy the specified zip file to the Function App. The app will download the specified file, extract the code from it, discover the functions, and run them. We’ve prepared this [zip](https://github.com/tusharshahrs/demo/blob/main/content/lab/pulumi/azure-native/python/app/HelloWithPython.zip) file for you to get started faster, you can find its code [here](https://github.com/tusharshahrs/demo/tree/main/content/lab/pulumi/azure-native/python/app). The code contains a single HTTP-triggered Azure Function. + +1. Run `pulumi up` to preview and select `yes` to deploy changes: + + ``` + $ pulumi up + Previewing update (dev) + + View Live: https://app.pulumi.com/myuser/azure-py-functions/dev/previews/f3ea-2esdff2-123d-e79d + + Type Name Plan + + pulumi:pulumi:Stack azure-py-functions-dev create + + ├─ azure-native:resources:ResourceGroup resourcegroup_functions_py create + + ├─ azure-native:web:AppServicePlan consumption-plan create + + ├─ azure-native:storage:StorageAccount storageaccount create + + └─ azure-native:web:WebApp functionapp create + + Resources: + + 5 to create + + Do you want to perform this update? [Use arrows to move, enter to select, type to filter] + > yes + no + details + + Updating (dev) + + View Live: https://app.pulumi.com/myuser/azure-py-functions/dev/updates/1 + + Type Name Status + + pulumi:pulumi:Stack azure-py-functions-dev created + + ├─ azure-native:resources:ResourceGroup resourcegroup_functions_py created + + ├─ azure-native:web:AppServicePlan consumption-plan created + + ├─ azure-native:storage:StorageAccount storageaccount created + + └─ azure-native:web:WebApp functionapp created + + Outputs: + consumptionplan : "consumption-plan7b9df5ed" + endpoint : "https://functionappfe054af4.azurewebsites.net/api/HelloWithPython" + function_app : "functionappfe054af4" + primarystoragekey : "[secret]" + resourcegroup : "resourcegroup_functions_py4eba2bf2" + storageaccount : "storageaccounta6b2e431" + storageaccountkeys : "[secret]" + storageconnectionstring: "[secret]" + + Resources: + + 5 created + + Duration: 50s + ``` + +1. Check the deployed function endpoints via [pulumi stack output](https://www.pulumi.com/docs/reference/cli/pulumi_stack_output/) + + ``` + $ pulumi stack output endpoint + https://functionappfe054af4.azurewebsites.net/api/HelloWithPython + +1. You can now open the resulting endpoint in the browser or curl it: + ``` + $ curl "$(pulumi stack output endpoint)" + Hello from Python in Pulumi! You have stood up a serverless function in Azure! + ``` + +## Cleanup and destroy everything + +1. Destroy the stack via: `pulumi destroy` .Select `yes` + ``` + Previewing destroy (dev) + + Type Name Plan + - pulumi:pulumi:Stack azure-py-functions-dev delete + - ├─ azure-native:web:WebApp functionapp delete + .. + .. + + Outputs: + - endpoint : "https://functionappfe054af4.azurewebsites.net/api/HelloWithPython" + .. + .. + + Resources: + - 5 to delete + + Do you want to perform this destroy? [Use arrows to move, enter to select, type to filter] + yes + > no + details + .. + .. + Resources: + - 5 deleted + + Duration: 1m1s + ``` + +1. Remove the stack + + ``` + pulumi stack rm + This will permanently remove the 'dev' stack! + Please confirm that this is what you'd like to do by typing ("dev"): + ``` + + Type in the name of your stack: **dev** \ No newline at end of file diff --git a/azure-py-functions/__main__.py b/azure-py-functions/__main__.py new file mode 100644 index 000000000..8bbf5646b --- /dev/null +++ b/azure-py-functions/__main__.py @@ -0,0 +1,90 @@ +import pulumi +from pulumi_azure_native import resources +from pulumi_azure_native import storage +from pulumi_azure_native import web +from pulumi import Output + +# Create an Azure Resource Group +resource_group = resources.ResourceGroup('resourcegroup_functions_py') + +# Create a Storage Account +account = storage.StorageAccount( + 'storageaccount', + resource_group_name=resource_group.name, + sku=storage.SkuArgs( + name=storage.SkuName.STANDARD_LRS, + ), + kind=storage.Kind.STORAGE_V2, +) + +# Create a consumption plan +# Consumption plan must be linux for python: https://docs.microsoft.com/en-us/azure/azure-functions/functions-scale#operating-systemruntime +plan = web.AppServicePlan( + "consumption-plan", + resource_group_name=resource_group.name, + location=resource_group.location, + kind="functionapp", + reserved=True, # This is an Azure Requirement for PYTHON. The function can only run on Linux. + sku=web.SkuDescriptionArgs(name="Y1", tier="Dynamic", size="Y1", family="Y", capacity=0), +) + +# Export the Azure Resource Group +pulumi.export('resourcegroup', resource_group.name) + +# Export the Storage Account +pulumi.export('storageaccount', account.name) + +# Export the Consumption Plan +pulumi.export('consumptionplan', plan.name) + +# List of storage account keys +storageAccountKeys = pulumi.Output.all(resource_group.name, account.name).apply( + lambda args: storage.list_storage_account_keys( + resource_group_name=args[0], account_name=args[1] + ) +) +# Primary storage account key +primaryStorageKey = storageAccountKeys.apply(lambda accountKeys: accountKeys.keys[0].value) +# Build a storage connection string out of it: +storageConnectionString = Output.concat( + "DefaultEndpointsProtocol=https;AccountName=", account.name, ";AccountKey=", primaryStorageKey +) + + +# Export the storageacountkey as a secret +pulumi.export("storageaccountkeys", pulumi.Output.secret(storageAccountKeys)) +# Export the primarystoragekey as a secret +pulumi.export('primarystoragekey', pulumi.Output.secret(primaryStorageKey)) +# Export the storageconnectionstring as a secret +pulumi.export('storageconnectionstring', pulumi.Output.secret(storageConnectionString)) + +# Create the functionapp +app = web.WebApp( + "functionapp", + resource_group_name=resource_group.name, + location=resource_group.location, + kind="functionapp", + reserved=True, + server_farm_id=plan.id, + site_config=web.SiteConfigArgs( + app_settings=[ + web.NameValuePairArgs(name="runtime", value="python"), + web.NameValuePairArgs(name="FUNCTIONS_WORKER_RUNTIME", value="python"), + web.NameValuePairArgs(name="FUNCTIONS_EXTENSION_VERSION", value="~3"), + web.NameValuePairArgs(name="AzureWebJobsStorage", value=storageConnectionString), + web.NameValuePairArgs( + name="WEBSITE_RUN_FROM_PACKAGE", + value="https://github.com/pulumi/examples/raw/master/azure-py-functions/app/HelloWithPython.zip", + ), + ], + ), +) + +# Export the function +pulumi.export('function_app', app.name) + +# Full endpoint of your Function App +function_endpoint = app.default_host_name.apply( + lambda default_host_name: f"https://{default_host_name}/api/HelloWithPython" +) +pulumi.export('endpoint', function_endpoint) diff --git a/azure-py-functions/app/HelloWithPython.zip b/azure-py-functions/app/HelloWithPython.zip new file mode 100644 index 000000000..4cedbee77 Binary files /dev/null and b/azure-py-functions/app/HelloWithPython.zip differ diff --git a/azure-py-functions/app/HelloWithPython/__init__.py b/azure-py-functions/app/HelloWithPython/__init__.py new file mode 100644 index 000000000..f937f618b --- /dev/null +++ b/azure-py-functions/app/HelloWithPython/__init__.py @@ -0,0 +1,10 @@ +import logging + +import azure.functions as func + + +def main(req: func.HttpRequest) -> func.HttpResponse: + logging.info('Python HTTP trigger function processed a request.') + return func.HttpResponse( + f"Hello from Python in Pulumi! You have stood up a serverless function in Azure!" + ) diff --git a/azure-py-functions/app/HelloWithPython/function.json b/azure-py-functions/app/HelloWithPython/function.json new file mode 100644 index 000000000..4667f0aca --- /dev/null +++ b/azure-py-functions/app/HelloWithPython/function.json @@ -0,0 +1,20 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": [ + "get", + "post" + ] + }, + { + "type": "http", + "direction": "out", + "name": "$return" + } + ] +} \ No newline at end of file diff --git a/azure-py-functions/app/README.md b/azure-py-functions/app/README.md new file mode 100644 index 000000000..4ed3cd22c --- /dev/null +++ b/azure-py-functions/app/README.md @@ -0,0 +1,202 @@ + +# Azure Function App Python + +Use command-line tools to create a Python function that responds to HTTP requests. +After testing the code locally, you deploy it to the serverless environment of Azure Functions. +This is informational. You can skip this entire thing if you already have working `python code`. + +# Why do this step? +To make sure that you build a function that works + +# Based on the following guide from MS +[Create a Python function in Azure from the command line](https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-python?tabs=azure-cli%2Cbash%2Cbrowser) + +## Installation Prereq + +[Install the Azure Functions Core Tools](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=macos%2Ccsharp%2Cbash#install-the-azure-functions-core-tools) + +Example steps for a mac: + +``` +brew tap azure/functions +brew install azure-functions-core-tools@3 +# if upgrading on a machine that has 2.x installed +brew link --overwrite azure-functions-core-tools@3 +``` + +# Create a Function Project +[Create a local Functions project](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=macos%2Ccsharp%2Cbash#create-a-local-functions-project) + +1. Create this in subdirectory of where your pulumi project is. For Example. + ``` + cd azure-function-workshop (where the Pulumi.yaml, **`__main__.py`**, etc are located. ) + mkdir app + cd app + + ``` + +1. Create an Azure Functions project + In the terminal window or from a command prompt, navigate to an empty folder for your project, and run the following command: + ``` + func init + ``` + You will also be prompted to choose a runtime for the project. Select **python**. + +1. Select a worker runtime + ``` + Select a number for worker runtime: + 1. dotnet + 2. dotnet (isolated process) + 3. node + 4. python + 5. java + 6. powershell + 7. custom + Choose option: 4 + ``` + Choose option: **4. python** + + Output: + + ``` + python + Found Python version 3.7.3 (python3). + Writing requirements.txt + Writing .gitignore + Writing host.json + Writing local.settings.json + ``` + +1. [Create a function](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=macos%2Ccsharp%2Cbash#create-func). + To create a function, run the following command: + + ``` + func new + ``` + + You will also be prompted to choose a runtime for the project. Select **9. HTTP trigger**. + + ``` + Select a number for template: + 1. Azure Blob Storage trigger + 2. Azure Cosmos DB trigger + 3. Durable Functions activity + 4. Durable Functions entity + 5. Durable Functions HTTP starter + 6. Durable Functions orchestrator + 7. Azure Event Grid trigger + 8. Azure Event Hub trigger + 9. HTTP trigger + 10. Azure Queue Storage trigger + 11. RabbitMQ trigger + 12. Azure Service Bus Queue trigger + 13. Azure Service Bus Topic trigger + 14. Timer trigger + + ``` + **Choose option: 9** + + Next you will have to name your function. The default name is: `HttpTrigger`. + Here we entered **HelloWorld** + + + `HTTP trigger` + + `Function name: [HttpTrigger]` **HelloWorld** + + Output: + + ``` + The function "HelloWorld" was created successfully from the "HTTP trigger" template. + ``` + +1. Verify that the function and all the files were created. + + `azure-function-workshop/app#> ls` + + ``` + HelloWorld(This is a directory) host.json local.settings.json requirements.txt + ``` + + `cd HelloWorld` + + `azure-function-workshop/app/HelloWorld#>ls -a` + + ``` + __init__.py function.json + ``` + +1. Change the `anonymous` user in the **`function.json`** + + This is because `Unless the HTTP access level on an HTTP triggered function is set to anonymous, requests must include an API access key in the request` as per + [Function access keys](https://docs.microsoft.com/en-us/azure/azure-functions/security-concepts#function-access-keys). + + Before: + ``` + "authLevel": "function", + + ``` + + After + ``` + "authLevel": "anonymous", + + ``` + +1. Update the **`__init__.py__`** (Optional) + This the file where your python code that you want. + You can keep what you have. We changed ours to simplify the output to + ``` + import logging + import azure.functions as func + + def main(req: func.HttpRequest) -> func.HttpResponse: + logging.info('Python HTTP trigger function processed a request.') + return func.HttpResponse(f"Hello from Python in Pulumi! You have stood up a serverless function in Azure!") + ``` + + +## Deploy your code to Azure via azure cli + +1. You need the function name. The name doesn't exist yet + because you haven't created it. + +1. comment out the following line in `__main__.py__` from your code from the section where the **web.WebApp** is: + + ``` + app = web.WebApp("functionapp", + ... + ... + #web.NameValuePairArgs(name= "WEBSITE_RUN_FROM_PACKAGE", value="https://github.com/..." <- THIS IS THE LINE TO COMMENT OUT + .. + .. + ) + ``` + +1. Deploy the Pulumi stack via `pulumi up -y` + +1. Find the function name via `pulumi stack output` or via checking the azure console + +1. To publish your Functions project into Azure, enter the following command: + + `azure-function-workshop/app#> func azure functionapp publish` **functionappe7f5313d** + +1. Check the azure console and view that the python code shows up inside he. + + Add + +## Clean Up +1. Azure does not allow you edit/delete python functions via the console. + Destroy the stack: `pulumi destroy -y` + +1. Update the ```__main__.py``` to external URL. + ``` + app = web.WebApp("functionapp", + ... + ... + web.NameValuePairArgs(name= "WEBSITE_RUN_FROM_PACKAGE", value="https://github.com/..." <- PUT THIS BACK IN + .. + .. + ) + ``` +1. Return back to your original steps to continue. \ No newline at end of file diff --git a/azure-py-functions/app/host.json b/azure-py-functions/app/host.json new file mode 100644 index 000000000..506376e88 --- /dev/null +++ b/azure-py-functions/app/host.json @@ -0,0 +1,7 @@ +{ + "version": "2.0", + "extensionBundle": { + "id": "Microsoft.Azure.Functions.ExtensionBundle", + "version": "[2.*, 3.0.0)" + } +} \ No newline at end of file diff --git a/azure-py-functions/app/images/python_function_uploaded_to_azure_function_via_cli.png b/azure-py-functions/app/images/python_function_uploaded_to_azure_function_via_cli.png new file mode 100644 index 000000000..d19027dad Binary files /dev/null and b/azure-py-functions/app/images/python_function_uploaded_to_azure_function_via_cli.png differ diff --git a/azure-py-functions/app/local.settings.json b/azure-py-functions/app/local.settings.json new file mode 100644 index 000000000..f5f7783b5 --- /dev/null +++ b/azure-py-functions/app/local.settings.json @@ -0,0 +1,7 @@ +{ + "IsEncrypted": false, + "Values": { + "FUNCTIONS_WORKER_RUNTIME": "python", + "AzureWebJobsStorage": "" + } +} \ No newline at end of file diff --git a/azure-py-functions/app/requirements.txt b/azure-py-functions/app/requirements.txt new file mode 100644 index 000000000..6bb1e59d0 --- /dev/null +++ b/azure-py-functions/app/requirements.txt @@ -0,0 +1,3 @@ +# Do not include azure-functions-worker as it may conflict with the Azure Functions platform + +azure-functions \ No newline at end of file diff --git a/azure-py-functions/requirements.txt b/azure-py-functions/requirements.txt new file mode 100644 index 000000000..d1109fefc --- /dev/null +++ b/azure-py-functions/requirements.txt @@ -0,0 +1,2 @@ +pulumi>=3.2.0,<4.0.0 +pulumi-azure-native>=1.5.0,<2.0.0 \ No newline at end of file