# Deploy Mastra to Azure App Services

Deploy your Mastra applications to Azure App Services.

> **Info:** This guide covers deploying the [Mastra server](https://mastra.ai/docs/server/mastra-server). If you're using a [server adapter](https://mastra.ai/docs/server/server-adapters) or [web framework](https://mastra.ai/docs/deployment/web-framework), deploy the way you normally would for that framework.

## Before you begin

You'll need:

- A [Mastra application](https://mastra.ai/guides/getting-started/quickstart)
- An [Azure account](https://azure.microsoft.com/) with an active subscription
- A [GitHub repository](https://github.com/) containing your Mastra application

> **Warning:** On Azure App Services, the filesystem is ephemeral for some pricing tiers, so any local database file will be lost between deployments. If you're using [LibSQLStore](https://mastra.ai/reference/storage/libsql) with a local file, configure it to use a remote LibSQL-compatible database (for example, [Turso](https://turso.tech/)) instead.

## Create a new app service

In these steps you'll learn how to create a new Azure App Service for hosting your Mastra application. This will set up the necessary infrastructure and environment for your application to run.

1. Log in to the [Azure Portal](https://portal.azure.com) and navigate to **[App Services](https://docs.microsoft.com/en-us/azure/app-service/)** or search for it in the top search bar. Select **Create** to create a new App Service, and in the drop-down, select **Web App**.

2. Configure the app service settings:

   - **Subscription**: Select your Azure subscription
   - **Resource Group**: Create a new resource group or select an existing one
   - **Instance name**: Enter a unique name for your app (this will be part of your URL)
   - **Publish**: Select **Code**
   - **Runtime stack**: Select **Node 22 LTS**
   - **Operating System**: Select **Linux**
   - **Region**: Choose a region close to your users
   - **Linux Plan**: You may have the option of choosing a plan depending on the region you chose, pick an appropriate one for your needs.
   - Select **Review + Create** and wait for validation to complete, then select **Create**.

3. A new deployment has been triggered. Wait for the deployment to complete, then select **Go to resource** under the next steps section.

4. Before setting up GitHub deployment, configure your environment variables. Navigate to **Settings** > **Environment variables** in the left sidebar. Add your required environment variables, such as:

   - Model provider API keys (e.g., `OPENAI_API_KEY`)
   - Database connection strings
   - Any other configuration values your Mastra application requires

   Select **Apply** to save the changes.

## GitHub deployment

In these steps, you'll connect your Azure App Service to your GitHub repository to enable continuous deployment. This means that every time you push changes to your repository, Azure will automatically build and deploy your application.

1. Navigate to **Deployment Center** in the left sidebar and select **GitHub** as your source. Sign in to GitHub if you're not already authenticated with Azure.

   In this example, you'll keep [GitHub Actions](https://docs.github.com/en/actions) as your provider. Select your organization, repository, and branch.

   Azure will generate a GitHub workflow file and you can preview it before proceeding. Select **Save** (the save button is located at the top of the page).

2. After Azure creates the workflow, it will trigger a GitHub Actions run and merge the workflow file into your branch. **Cancel this initial run** as it will fail without the necessary modifications.

   > **Warning:** The default workflow generated by Azure will fail for Mastra applications and needs to be modified.

   Pull the latest changes to your local repository and modify the generated workflow file (`.github/workflows/main_<your-app-name>.yml`):

3. Find the step named "npm install, build, and test" and:

   - Change the step name to "npm install and build"
   - If you haven't set up proper tests in your Mastra application, remove the `npm test` command from the run section as the default test script will fail and disrupt deployment. If you have working tests, you can keep the test command.

4. Find the "Zip artifact for deployment" step and replace the zip command with:

   ```yaml
   run: (cd .mastra/output && zip ../../release.zip -r .)
   ```

   This ensures only the build outputs from `.mastra/output` are included in the deployment package.

5. Commit and push your workflow modifications. The build will be automatically triggered in the **Deployment Center** in your Azure dashboard. Monitor the deployment progress until it completes successfully.

## Verify your deployment

Once the build is successful, wait a few moments for the application to start. You can access your deployed application using the default URL provided in the **Overview** tab in the Azure portal. Your application will be available at `https://<your-app-name>.azurewebsites.net`.

Copy the URL from the Azure portal and visit `https://<your-app-name>.azurewebsites.net/api/agents` in your browser. You should see a JSON list of your agents.

You can now call your Mastra endpoints over HTTP.

> **Warning:** Set up [authentication](https://mastra.ai/docs/server/auth) before exposing your endpoints publicly.

## Next steps

- [Mastra Client SDK](https://mastra.ai/reference/client-js/mastra-client)
- [Configure custom domains](https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-custom-domain)
- [Enable HTTPS](https://docs.microsoft.com/en-us/azure/app-service/configure-ssl-bindings)
- [Azure App Service documentation](https://docs.microsoft.com/en-us/azure/app-service/)