1. Answers
  2. Hosting multiple web apps within a single App Service Plan

How do I host multiple web apps within a single App Service Plan?

To host multiple web apps within a single App Service Plan on Azure using Pulumi, we will follow these steps:

  1. Create an Azure Resource Group: Resource groups in Azure are logical containers into which Azure resources like web apps, databases, and storage accounts are deployed and managed.
  2. Create an App Service Plan: An App Service Plan defines the region, instance size, and scaling options for web apps.
  3. Create Multiple Web Apps: Each web app will be hosted within the single App Service Plan we created.

We’ll use the azure-native package, which provides native support for Azure resources. Below is a detailed explanation followed by the Pulumi program written in TypeScript.

Detailed Explanation

  1. Resource Group: This is a container that holds related resources for an Azure solution. It includes those resources that you want to manage as a group.
  2. App Service Plan: This provides the underlying compute resources for your web apps. It defines the region, instance size, and scaling options.
  3. Web Apps: These are the actual applications that you want to deploy. They will share the compute resources defined in the App Service Plan.

Pulumi Program

import * as pulumi from "@pulumi/pulumi";
import * as azureNative from "@pulumi/azure-native";

// Create an Azure Resource Group
const resourceGroup = new azureNative.resources.ResourceGroup("resourceGroup", {
    resourceGroupName: "myResourceGroup",
    location: "WestEurope",
});

// Create an App Service Plan
const appServicePlan = new azureNative.web.AppServicePlan("appServicePlan", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    sku: {
        name: "B1",
        tier: "Basic",
        capacity: 1,
    },
});

// Create the first Web App
const webApp1 = new azureNative.web.WebApp("webApp1", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    serverFarmId: appServicePlan.id,
    siteConfig: {
        appSettings: [
            {
                name: "WEBSITE_RUN_FROM_PACKAGE",
                value: "1",
            },
        ],
    },
});

// Create the second Web App
const webApp2 = new azureNative.web.WebApp("webApp2", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    serverFarmId: appServicePlan.id,
    siteConfig: {
        appSettings: [
            {
                name: "WEBSITE_RUN_FROM_PACKAGE",
                value: "1",
            },
        ],
    },
});

// Export the URLs of the web apps
export const webApp1Url = webApp1.defaultHostName.apply(hostName => `https://${hostName}`);
export const webApp2Url = webApp2.defaultHostName.apply(hostName => `https://${hostName}`);

Explanation of the Code

  1. Resource Group: We create a resource group named myResourceGroup in the WestEurope region.
  2. App Service Plan: We create an App Service Plan named appServicePlan in the same resource group and region. The plan uses the Basic pricing tier with one instance.
  3. Web Apps: We create two web apps (webApp1 and webApp2) within the same App Service Plan. Each web app is configured to run from a package.
  4. Export URLs: We export the URLs of the two web apps so you can easily access them after deployment.

This Pulumi program sets up an environment where two web apps share the same compute resources defined by a single App Service Plan, making it cost-effective and easy to manage.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up