Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 57 ยท Compute

App Service and Web Apps

Host web apps and APIs on Azure App Service: plans and tiers, deploying code or containers, app settings and connection strings, custom domains, TLS and scaling.

Most teams do not want to patch operating systems, they want to run a web application. Azure App Service is the platform-as-a-service (PaaS) for exactly that. You hand it your code (Node.js, .NET, Java, Python, PHP) or a container image, and Azure provides the servers, the operating system updates, the load balancing, the HTTPS endpoint and the scaling controls. You manage the app and its configuration; Microsoft manages everything underneath.

This lesson explains the two resources you always create (the plan and the app), how to deploy, how settings reach your code, how to attach a custom domain with TLS, and the difference between scaling up and scaling out. Creating real apps needs a subscription, so the commands are read-only. The runnable examples are stand-ins that model how settings become environment variables and how capacity grows.

Plans and apps

Two resources work together:

  • The App Service plan is the set of virtual machines that run your apps. It fixes the region, the operating system (Linux or Windows), the tier and size (CPU, memory, features) and the number of instances. The plan is what you pay for.
  • The web app is your application. Many apps can share one plan and share its capacity, which is a good way to save money for small workloads.
flowchart TB
    PLAN["App Service plan<br/>region, OS, tier, instance count"]
    PLAN --> A["Web app: shop-web"]
    PLAN --> B["Web app: shop-api"]
    PLAN --> C["Web app: admin"]
    PLAN --> I1["Instance 1"]
    PLAN --> I2["Instance 2"]

Apps in a plan run on the plan's instances; every instance runs a copy of every app.

Tiers

Tier Typical use Notable features
Free and Shared Learning, experiments Shared hardware, strict quotas, no SLA, limited features
Basic Simple dev/test and light production Dedicated instances, custom domains with TLS, manual scaling
Standard Production Autoscale, deployment slots, backups
Premium (v3 and newer) Larger production More memory and performance, more instances and slots
Isolated Strict network or compliance needs Runs in a private, dedicated environment inside your virtual network

The details change, so verify features against the current pricing page before relying on a specific tier. A practical rule: start with a small paid tier for anything real, because the free tier sleeps apps and has no production guarantees.

Creating and deploying

All commands here are read-only. App names become part of a public hostname (<name>.azurewebsites.net), so they must be globally unique.

Terminal
az group create --name web --location westeurope

# The plan: Linux, Basic B1
az appservice plan create --name shop-plan --resource-group web \
  --is-linux --sku B1

# The app, with a Node.js runtime
az webapp create --name shop-web-4821 --resource-group web \
  --plan shop-plan --runtime "NODE:20-lts"

# Push a ZIP of your built app
az webapp deploy --name shop-web-4821 --resource-group web \
  --src-path app.zip --type zip

There are several ways to get code into the app: a ZIP or WAR package as above, a Git or GitHub connection, or a pipeline that deploys on every merge (see CI/CD with GitHub Actions and Azure DevOps). For containers, point the app at an image in a registry, such as Azure Container Registry, and App Service pulls and runs it. A quick one-liner, az webapp up, creates the missing pieces from the current folder, which is handy for experiments but less repeatable than infrastructure as code (see Bicep and ARM Templates).

Useful switches to turn on early:

Terminal
az webapp update --name shop-web-4821 --resource-group web --https-only true
az webapp config set --name shop-web-4821 --resource-group web --always-on true

HTTPS only redirects plain HTTP. Always on keeps the app loaded instead of unloading it after idle time, which avoids slow first requests (it needs a tier above Free and Shared).

App settings and connection strings

Code should never contain environment-specific values or secrets. App Service stores application settings as name-value pairs outside your code. At startup the platform exposes each one to your process as an environment variable, so ordinary code like process.env.API_URL or os.environ["API_URL"] just works.

Terminal
az webapp config appsettings set --name shop-web-4821 --resource-group web \
  --settings API_URL=https://api.example.com FEATURE_BANNER=on

az webapp config connection-string set --name shop-web-4821 --resource-group web \
  --connection-string-type SQLAzure \
  --settings Orders="Server=tcp:...;Database=orders;..."

Connection strings are a second list with a type. The platform adds a prefix to the environment variable name depending on the type: SQLAZURECONNSTR_, SQLCONNSTR_, MYSQLCONNSTR_, POSTGRESQLCONNSTR_ or CUSTOMCONNSTR_. So the Orders string above reaches your code as SQLAZURECONNSTR_Orders. Some frameworks (such as ASP.NET Core) read the prefixes natively; in other languages you read the prefixed variable yourself.

Settings are encrypted at rest and only shown to people with access to the app, but a plain setting is still a stored secret. Better options exist for real secrets: reference a Key Vault secret from the setting (see Key Vault) and give the app a managed identity (see Managed Identities and Service Principals) so passwords disappear altogether. Changing a setting restarts the app.

Settings as environment variables: a stand-in

The real platform injects the variables for you. The stand-in below does the same job in a few lines: it takes a settings list and a connection string list, produces the environment the process would see, and runs a small loadConfig function that reads only from that environment. The names and values are invented.

Node.js Turn app settings into environment variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice that the database name never appears in the code, only in configuration. That separation is what lets the same build run in test and production with different settings. Mark a setting as a slot setting (sticky) when it must stay with a deployment slot instead of moving with the code during a swap; slots are covered in Deployment Slots, Blue-Green and Canary.

Custom domains and TLS

Every app gets https://<name>.azurewebsites.net with a valid certificate. For your own domain, such as shop.example.com:

  1. Create a CNAME record at your DNS provider pointing shop to <name>.azurewebsites.net (an apex domain such as example.com uses an A record instead). App Service also asks for a TXT record that proves you own the domain. DNS itself is explained in DNS and Traffic Manager.
  2. Add the custom domain to the app.
  3. Add a TLS binding with a certificate. App Service can issue a free managed certificate for many cases and renews it automatically, or you can upload your own or pull one from Key Vault. TLS bindings need at least the Basic tier.
Terminal
az webapp config hostname add --webapp-name shop-web-4821 \
  --resource-group web --hostname shop.example.com

Scaling up and scaling out

Two directions, both changed on the plan:

  • Scale up means a bigger machine: move to a size or tier with more CPU, memory and features. It is simple and effective for a single heavy process, but there is a ceiling and a restart-like change.
  • Scale out means more instances of the same size. The built-in load balancer spreads requests across them, so your app must be stateless: keep sessions and uploads in a shared service, not in memory or on a local disk. Scale out manually, or with autoscale rules (Standard and above), as described in Scaling and Autoscale.
Terminal
az appservice plan update --name shop-plan --resource-group web --sku P1V3
az appservice plan update --name shop-plan --resource-group web --number-of-workers 3

The second stand-in compares the two on an invented per-instance capacity and price. It shows the trade-off in numbers: scaling up buys capacity at a higher unit price, scaling out multiplies instances and also gives redundancy, because one instance can fail while others serve traffic.

JS Scale up or scale out?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

One huge instance can meet the demand, but it is a single point of failure. Four mid-size instances cost the same here and still meet the demand after one fails, while thirteen tiny ones cost more and, with this little spare capacity, do not survive a failure. Real choices also depend on how well your app uses extra cores, which only load testing shows.

Managed certificates, backups, cloning and testing in production

A few App Service features turn a working app into a well-run one.

Free managed certificates

For a custom domain, a managed certificate is free: App Service issues it, binds it and renews it automatically. It has limits, so know them before you rely on it: it cannot be exported, it does not cover wildcards (*.example.com), and the domain must already point at the app so ownership can be validated. If you need a wildcard, an extended-validation certificate, or one you must use elsewhere, buy or import a certificate and keep it in Key Vault, and let the app reference it. Set HTTPS Only on so plain HTTP requests are redirected, and set a minimum TLS version.

Terminal
az webapp config ssl create --resource-group web --name shop-web-4821 --hostname shop.example.com
az webapp update --resource-group web --name shop-web-4821 --https-only true

Files, local cache and persistent storage

An app's own files live under /home (Linux) or D:\home (Windows) on persistent, shared storage, so every instance sees the same files and they survive restarts. Anything outside /home, including /tmp, is local to one instance and disappears when the app moves. Persistent storage is network-backed, so heavy small-file reads can be slow. For Windows apps, Local Cache copies the content to the instance's local disk at startup (set WEBSITE_LOCAL_CACHE_OPTION=Always), which gives faster reads and avoids brief storage hiccups, with the caveat that writes there are lost on restart and are not shared. Never keep uploads or user data in an instance's local folder; use Blob Storage.

Backup and clone

On Standard tier and above, backups save the app's files, configuration and optionally a linked database to a storage account, either on a schedule or on demand. Remember that a backup stored in the same region and account as the app helps with mistakes, not disasters (see Reliability and Disaster Recovery).

Terminal
az webapp config backup create --resource-group web --webapp-name shop-web-4821 \
  --backup-name pre-release --container-url "<storage-container-sas-url>"

Clone app (in the portal, on higher tiers) copies an app and its settings into a new app and plan, for example to make a staging environment or move to another region. A clone does not copy everything (managed identities, some networking and certificates need redoing), so check the list after cloning; infrastructure as code (Bicep and ARM Templates) is the repeatable alternative.

Testing in production with traffic routing

With deployment slots (see Deployment Slots, Blue-Green and Canary) you can send a percentage of live traffic to a slot to test a new version on real users before swapping:

Terminal
az webapp traffic-routing set --resource-group web --name shop-web-4821 --distribution staging=10
az webapp traffic-routing show --resource-group web --name shop-web-4821
az webapp traffic-routing clear --resource-group web --name shop-web-4821

Visitors get a cookie (x-ms-routing-name) that pins them to the same slot, so one user does not flip between versions mid-session. If the staging slot misbehaves, clear the routing; if it is healthy, swap it into production.

Splitting traffic with sticky sessions: a stand-in

JS Testing in production: a 10% slot with a sticky cookie

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Azure Static Web Apps and local cache

Azure Static Web Apps hosts a front end (plain HTML, or a React, Vue, Angular or similar build) plus an optional serverless API, with global content delivery, free managed TLS and per-pull-request preview environments. It is built around a GitHub repository (or Azure DevOps): creating the resource adds a workflow that builds and deploys on every push. To get started in the portal: choose Create a resource, then Static Web App, pick the subscription and resource group, give the app a name (for example static-web-app-01), choose the plan (Free is fine for learning), sign in to GitHub and select the repository and branch, and describe the build with the app location (the front-end folder), the optional API location (an Azure Functions folder) and the output location (the build folder such as dist). After the first workflow run the site is live at a generated azurestaticapps.net address, and you can add a custom domain later.

Terminal
az staticwebapp create --name static-web-app-01 --resource-group web \
  --source https://github.com/<you>/<repo> --branch main \
  --app-location "/" --output-location "dist" --login-with-github

Choose Static Web Apps when the site is mostly static files and a small API; choose App Service when you need a long-running server process, background threads or full control of the runtime.

Local cache is an App Service option for apps that read many files from the content share. It gives each instance a read-only copy of /site/wwwroot on local disk, which is faster and survives brief storage hiccups, but writes made to the folder at run time are discarded when the app restarts. Turn it on only for apps that do not write into their own folder, using the app setting WEBSITE_LOCAL_CACHE_OPTION=Always.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the App Service plan define?

    The plan is the compute you pay for. Several apps can share one plan.
  2. 2. A connection string named Orders of type SQLAzure is added to an app. Which environment variable will your code see?

    App Service prefixes connection string variables by type; SQLAzure becomes SQLAZURECONNSTR_.
  3. 3. What must be true of your app before you scale it out to several instances?

    Requests are spread across instances, so anything stored in one instance's memory or disk may not be there next time.
  4. 4. Which statement about scaling up is correct?

    Scale up changes the size or tier. Scale out changes the instance count.
  5. 5. What is a better home for a database password than a plain app setting?

    Key Vault and managed identities keep secrets out of settings and out of source control.

Summary

  • App Service is a PaaS for web apps and APIs; the plan provides compute, the app is your code or container.
  • Pick a tier by features and size; start with a small paid tier for real workloads.
  • Deploy with ZIP, Git, a pipeline or a container image.
  • App settings and connection strings arrive as environment variables, and connection strings get a type prefix such as SQLAZURECONNSTR_.
  • Custom domains use a CNAME (or A record) plus a TLS binding, which needs at least Basic.
  • Scale up with a bigger size, scale out with more stateless instances; scaling out also adds redundancy.

Next: Azure Functions.