Deploying Serverless Applications with Azure Functions
A minimal Azure Native Function in Python with Pulumi program
This example lives in the pulumi/examples repository. Check out just this directory to use it:
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examplesgit -C pulumi-examples sparse-checkout set azure-py-functionscd pulumi-examples/azure-py-functionsYou will deploy a Azure Function Apps with HTTP-triggered serverless functions in python
Running the App#
-
Login to Azure CLI (you will be prompted to do this during deployment if you forget this step):
$ az login -
Create a new stack:
$ pulumi stack init dev -
Create a virtual environment and install python dependencies
Windows#
Run the following command to create a virtual environment
Terminal window python -m venv venvActivate the environment:
Terminal window venv\Scripts\activateInstall dependencies:
Terminal window pip3 install -r requirements.txtMac and Linux#
Run the following command to create a virtual environment
Terminal window python3 -m venv venvActivate the environment:
Terminal window source venv/bin/activateInstall dependencies:
Terminal window pip3 install -r requirements.txtAt 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:
Terminal window source venv/bin/activate -
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.
Terminal window pulumi config set azure-native:location eastus2pulumi config set allows us to pass in configuration values from the command line. Feel free to choose any Azure region that supports the services used in these labs (see this infographic 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,australiacentral2The command updates and persists the value to the local
Pulumi.dev.yamlfile. You can view or edit this file at any time to effect the configuration of the current stack. -
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 file for you to get started faster, you can find its code here. The code contains a single HTTP-triggered Azure Function.
-
Run
pulumi upto preview and selectyesto deploy changes:$ pulumi upPreviewing update (dev)View Live: https://app.pulumi.com/myuser/azure-py-functions/dev/previews/f3ea-2esdff2-123d-e79dType 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 createResources:+ 5 to createDo you want to perform this update? [Use arrows to move, enter to select, type to filter]> yesnodetailsUpdating (dev)View Live: https://app.pulumi.com/myuser/azure-py-functions/dev/updates/1Type 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 createdOutputs: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 createdDuration: 50s -
Check the deployed function endpoints via pulumi stack output
$ pulumi stack output endpointhttps://functionappfe054af4.azurewebsites.net/api/HelloWithPython -
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#
-
Destroy the stack via:
pulumi destroy.SelectyesPreviewing 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 deleteDo you want to perform this destroy? [Use arrows to move, enter to select, type to filter]yes> nodetails....Resources:- 5 deletedDuration: 1m1s -
Remove the stack
pulumi stack rmThis 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