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#

For this walkthrough, you'll need:

Create a new project#

Create a new project from the template:

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

The pulumi new command walks you through creating and configuring the 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. Depending on the language, you may also be asked for your preferred package manager or toolchain.

This template’s settings include:

KeyDescriptionDefault
azure-native:locationThe Azure region to deploy intoWestUS
sitePathThe path to the folder containing the websitewww
appPathThe path to the folder containing the functions to be deployedapp
indexDocumentThe file to use for top-level pagesindex.html
errorDocumentThe file to use for error pageserror.html
You can change values like these 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>

Review the code#

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

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

Take a moment to open them up and have a look. Here’s what’s in index.ts:

index.ts
import * as pulumi from "@pulumi/pulumi";
import * as azure from "@pulumi/azure-native";
import * as synced from "@pulumi/synced-folder";
// Import the program's configuration settings.
const config = new pulumi.Config();
const sitePath = config.get("sitePath") || "www";
const appPath = config.get("appPath") || "app";
const indexDocument = config.get("indexDocument") || "index.html";
const errorDocument = config.get("errorDocument") || "error.html";
// Create a resource group for the website.
const resourceGroup = new azure.resources.ResourceGroup("resource-group", {});
// Create a blob storage account.
const account = new azure.storage.StorageAccount("account", {
resourceGroupName: resourceGroup.name,
kind: azure.storage.Kind.StorageV2,
sku: {
name: azure.storage.SkuName.Standard_LRS,
},
});
// Create a storage container for the pages of the website.
const website = new azure.storage.StorageAccountStaticWebsite("website", {
accountName: account.name,
resourceGroupName: resourceGroup.name,
indexDocument: indexDocument,
error404Document: errorDocument,
});
// Use a synced folder to manage the files of the website.
const syncedFolder = new synced.AzureBlobFolder("synced-folder", {
path: sitePath,
resourceGroupName: resourceGroup.name,
storageAccountName: account.name,
containerName: website.containerName,
});
// Create a storage container for the serverless app.
const appContainer = new azure.storage.BlobContainer("app-container", {
accountName: account.name,
resourceGroupName: resourceGroup.name,
publicAccess: azure.storage.PublicAccess.None,
});
// Upload the serverless app to the storage container.
const appBlob = new azure.storage.Blob("app-blob", {
accountName: account.name,
resourceGroupName: resourceGroup.name,
containerName: appContainer.name,
source: new pulumi.asset.FileArchive(appPath),
});
// Create a shared access signature to give the Function App access to the code.
const signature = azure.storage.listStorageAccountServiceSASOutput({
resourceGroupName: resourceGroup.name,
accountName: account.name,
protocols: azure.storage.HttpProtocol.Https,
sharedAccessStartTime: "2022-01-01",
sharedAccessExpiryTime: "2030-01-01",
resource: azure.storage.SignedResource.C,
permissions: azure.storage.Permissions.R,
canonicalizedResource: pulumi.interpolate`/blob/${account.name}/${appContainer.name}`,
});
// Create an App Service plan for the Function App.
const plan = new azure.web.AppServicePlan("plan", {
resourceGroupName: resourceGroup.name,
sku: {
name: "Y1",
tier: "Dynamic",
},
});
// Create the Function App.
const functionApp = new azure.web.WebApp("function-app", {
resourceGroupName: resourceGroup.name,
serverFarmId: plan.id,
kind: "FunctionApp",
siteConfig: {
appSettings: [
{
name: "FUNCTIONS_WORKER_RUNTIME",
value: "node",
},
{
name: "WEBSITE_NODE_DEFAULT_VERSION",
value: "~22",
},
{
name: "FUNCTIONS_EXTENSION_VERSION",
value: "~4",
},
{
name: "WEBSITE_RUN_FROM_PACKAGE",
value: pulumi.all([account.name, appContainer.name, appBlob.name, signature])
.apply(([accountName, containerName, blobName, signature]) => `https://${accountName}.blob.core.windows.net/${containerName}/${blobName}?${signature.serviceSasToken}`),
},
],
cors: {
allowedOrigins: [
"*"
],
},
},
});
// Create a JSON configuration file for the website.
const configFile = new azure.storage.Blob("config.json", {
source: functionApp.defaultHostName
.apply(host => new pulumi.asset.StringAsset(JSON.stringify({ api: `https://${host}/api` }))),
contentType: "application/json",
accountName: account.name,
resourceGroupName: resourceGroup.name,
containerName: website.containerName,
});
// Export the URLs of the website and serverless endpoint.
export const siteURL = account.primaryEndpoints.apply(primaryEndpoints => primaryEndpoints.web);
export const apiURL = pulumi.interpolate`https://${functionApp.defaultHostName}/api`;

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 HTTPS URL of the static website.

apiURL

The HTTPS 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
Need help?

If pulumi up asks you to run az login, or says your sign-in has expired, sign in again:

Terminal window
az login

Still stuck? Let us know.

Customize the project#

Projects created with this 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.

For example, to deploy a website from another folder on your computer, replacing ../my-existing-website/build with the path to your site’s files:

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. If it still prints the old error page, wait a few seconds and run curl again:

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

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 platform for humans and agents.