Skip to main content

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:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set azure-py-functions
cd pulumi-examples/azure-py-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
  2. Create a new stack:

    $ pulumi stack init dev
  3. Create a virtual environment and install python dependencies

    Windows#

    Run the following command to create a virtual environment

    Terminal window
    python -m venv venv

    Activate the environment:

    Terminal window
    venv\Scripts\activate

    Install dependencies:

    Terminal window
    pip3 install -r requirements.txt

    Mac and Linux#

    Run the following command to create a virtual environment

    Terminal window
    python3 -m venv venv

    Activate the environment:

    Terminal window
    source venv/bin/activate

    Install dependencies:

    Terminal window
    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:

    Terminal window
    source venv/bin/activate
  4. 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 eastus2

    pulumi 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,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.

  5. 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.

  6. 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
  7. Check the deployed function endpoints via pulumi stack output

    $ pulumi stack output endpoint
    https://functionappfe054af4.azurewebsites.net/api/HelloWithPython
  8. 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
  2. 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

Related

The infrastructure as code platform for any cloud.