Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Azure Serverless Application

The Azure Serverless Application template scaffolds a Pulumi project that deploys an Azure Blob Storage account configured for static website hosting and an Azure Function for the application backend. The template ships with placeholder web and function content so the project deploys end to end out of the box.

Architecture diagram of the Azure Serverless Application template Azure Storage Container Function App

Before you begin#

Make sure you’ve installed Pulumi and configured your Azure credentials.

Create a new project#

Create a new project using the template in the language of your choice:

Terminal window
mkdir my-serverless-app && cd my-serverless-app
pulumi new serverless-azure-typescript

pulumi new walks you through creating and configuring a new project and stack. A stack is an instance of a project and you can have many of them — dev, staging, prod — each with its own configuration settings. The template also includes some additional settings:

KeyDescriptionDefault
azure-native:locationThe Azure region to deploy intoWestUS
sitePathThe path to the folder containing the website./www
appPathThe path to the folder containing the functions to be deployed./app
indexDocumentThe file to use for top-level pagesindex.html
errorDocumentThe file to use for error pageserror.html
You can change these settings later with pulumi config set:
Terminal window
pulumi config set azure-native:location <value>
pulumi config set sitePath <value>
pulumi config set appPath <value>
pulumi config set indexDocument <value>
pulumi config set errorDocument <value>

When setup completes, you’ll see a few key files:

  • Pulumi.yaml names the project and the language it’s written in.
  • Pulumi.dev.yaml holds the dev stack’s settings, including the values you just entered.
  • index.ts is the program: the code that declares your cloud resources.

Deploy the project#

Deploy with pulumi up. It starts with a preview of what it’s about to create and asks before it changes anything. Choose yes to deploy.

Terminal window
pulumi up

Pulumi adds a random suffix to most resource names, so the resource group resource-group becomes something like resource-group8f3c2a1b. This is autonaming. It lets you deploy the same program to more than one stack, and lets Pulumi create a replacement before it deletes the old resource, without names colliding.

When it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist:

siteURL

The HTTP URL of the static website.

apiURL

The HTTP URL of the serverless function endpoint.

Read one any time with pulumi stack output <name>, or pass it to another command. This opens the website in your browser:

Terminal window
open $(pulumi stack output siteURL)

The website’s button calls the function at the /data path. Call it from the terminal with curl; it prints the current time in milliseconds, like {"now": 1792591509000}. The first call can take a few seconds while the function starts.

Terminal window
curl -s $(pulumi stack output apiURL)/data

Customize the project#

Projects created with the Serverless Application template expose the following configuration settings:

sitePath

The path to the folder containing the files of the website. Defaults to www, which is the folder included with the template.

appPath

The path to the folder containing the serverless functions to be deployed. Defaults to app, which is also included with the template.

indexDocument

The file to use for top-level pages. Defaults to index.html.

errorDocument

The file to use for error pages. Defaults to error.html.

All of these settings are optional and may be adjusted either by editing the stack configuration file directly (by default, Pulumi.dev.yaml) or by changing their values with pulumi config set.

Terminal window
pulumi config set sitePath ../my-existing-website/build
pulumi up

Changing the error page#

The error page is what the website returns for a path it doesn’t have. Point it at the home page instead, then ask for a page that doesn’t exist. curl prints the home page, with What time is it?, in place of the template’s Oops! That page wasn't found.:

Terminal window
pulumi config set errorDocument index.html
pulumi up
curl -s $(pulumi stack output siteURL)no-such-page

To see what else you can add, browse the Azure Native provider in the Pulumi Registry.

Cleaning up#

You can cleanly destroy the stack and all of its infrastructure with pulumi destroy:

Terminal window
pulumi destroy

Learn more#

Related

The infrastructure as code platform for any cloud.