Tutorial: Provision infrastructure with Azure deployment slots using Terraform

You can use Azure deployment slots to swap between different versions of your app. That ability helps you minimize the impact of broken deployments.

This article illustrates an example use of deployment slots by walking you through the deployment of two apps via GitHub and Azure. One app is hosted in a production slot. The second app is hosted in a staging slot. (The names "production" and "staging" are arbitrary. They can be whatever is appropriate for your scenario.) After you configure your deployment slots, you use Terraform to swap between the two slots as needed.

Prerequisites

  • Azure subscription: If you don't have an Azure subscription, create a free account before you begin.

  • GitHub account: You need a GitHub account to fork and use the test GitHub repo.

Create and apply the Terraform plan

  1. Browse to the Azure portal.

  2. Open Azure Cloud Shell. If you didn't select an environment previously, select Bash as your environment.

    Cloud Shell prompt

  3. Change directories to the clouddrive directory.

    cd clouddrive
    
  4. Create a directory named deploy.

    mkdir deploy
    
  5. Create a directory named swap.

    mkdir swap
    
  6. Use the ls bash command to verify that you successfully created both directories.

    Cloud shell after creating directories

  7. Change directories to the deploy directory.

    cd deploy
    
  8. In Cloud Shell, create a file named deploy.tf.

    code deploy.tf
    
  9. Paste the following code into the editor:

    # Configure the Azure provider
    provider "azurerm" { }
    
    resource "azurerm_resource_group" "slotDemo" {
        name = "slotDemoResourceGroup"
        location = "westus2"
    }
    
    resource "azurerm_app_service_plan" "slotDemo" {
        name                = "slotAppServicePlan"
        location            = azurerm_resource_group.slotDemo.location
        resource_group_name = azurerm_resource_group.slotDemo.name
        sku {
            tier = "Standard"
            size = "S1"
        }
    }
    
    resource "azurerm_app_service" "slotDemo" {
        name                = "slotAppService"
        location            = azurerm_resource_group.slotDemo.location
        resource_group_name = azurerm_resource_group.slotDemo.name
        app_service_plan_id = azurerm_app_service_plan.slotDemo.id
    }
    
    resource "azurerm_app_service_slot" "slotDemo" {
        name                = "slotAppServiceSlotOne"
        location            = azurerm_resource_group.slotDemo.location
        resource_group_name = azurerm_resource_group.slotDemo.name
        app_service_plan_id = azurerm_app_service_plan.slotDemo.id
        app_service_name    = azurerm_app_service.slotDemo.name
    }
    
  10. Save the file (<Ctrl>S) and exit the editor (<Ctrl>Q).

  11. Now that you've created the file, verify its contents.

    cat deploy.tf
    
  12. Initialize Terraform.

    terraform init
    
  13. Create the Terraform plan.

    terraform plan
    
  14. Provision the resources that are defined in the deploy.tf configuration file. (Confirm the action by entering yes at the prompt.)

    terraform apply
    
  15. Close the Cloud Shell window.

  16. On the main menu of the Azure portal, select Resource groups.

    "Resource groups" selection in the portal

  17. On the Resource groups tab, select slotDemoResourceGroup.

    Resource group created by Terraform

You now see all the resources that Terraform has created.

Resources created by Terraform

Fork the test project

Before you can test the creation and swapping in and out of the deployment slots, you need to fork the test project from GitHub.

  1. Browse to the awesome-terraform repo on GitHub.

  2. Fork the awesome-terraform repo.

    Fork the GitHub awesome-terraform repo

  3. Follow any prompts to fork to your environment.

Deploy from GitHub to your deployment slots

After you fork the test project repo, configure the deployment slots via the following steps:

  1. On the main menu of the Azure portal, select Resource groups.

  2. Select slotDemoResourceGroup.

  3. Select slotAppService.

  4. Select Deployment options.

    Deployment options for an App Service resource

  5. On the Deployment option tab, select Choose Source, and then select GitHub.

    Select deployment source

  6. After Azure makes the connection and displays all the options, select Authorization.

  7. On the Authorization tab, select Authorize, and supply the credentials that Azure needs to access your GitHub account.

  8. After Azure validates your GitHub credentials, a message appears and says that the authorization process has finished. Select OK to close the Authorization tab.

  9. Select Choose your organization and select your organization.

  10. Select Choose project.

  11. On the Choose project tab, select the awesome-terraform project.

    Choose the awesome-terraform project

  12. Select Choose branch.

  13. On the Choose branch tab, select master.

    Choose the master branch

  14. On the Deployment option tab, select OK.

At this point, you've deployed the production slot. To deploy the staging slot, do the previous steps with the following modifications:

  • In step 3, select the slotAppServiceSlotOne resource.

  • In step 13, select the working branch instead of the master branch.

    Choose the working branch

Test the app deployments

In the previous sections, you set up two slots--slotAppService and slotAppServiceSlotOne--to deploy from different branches in GitHub. Let's preview the web apps to validate that they were successfully deployed.

  1. On the main menu of the Azure portal, select Resource groups.

  2. Select slotDemoResourceGroup.

  3. Select either slotAppService or slotAppServiceSlotOne.

  4. On the overview page, select URL.

    Select the URL on the overview tab to render the app

  5. Depending on the selected app, you see the following results:

    • slotAppService web app - Blue page with a page title of Slot Demo App 1.
    • slotAppServiceSlotOne web app - Green page with a page title of Slot Demo App 2.

    Preview the apps to test that they were deployed correctly

Swap the two deployment slots

To test swapping the two deployment slots, do the following steps:

  1. Switch to the browser tab that's running slotAppService (the app with the blue page).

  2. Return to the Azure portal on a separate tab.

  3. Open Cloud Shell.

  4. Change directories to the clouddrive/swap directory.

    cd clouddrive/swap
    
  5. In Cloud Shell, create a file named swap.tf.

    code swap.tf
    
  6. Paste the following code into the editor:

    # Configure the Azure provider
    provider "azurerm" { }
    
    # Swap the production slot and the staging slot
    resource "azurerm_app_service_active_slot" "slotDemoActiveSlot" {
        resource_group_name   = "slotDemoResourceGroup"
        app_service_name      = "slotAppService"
        app_service_slot_name = "slotappServiceSlotOne"
    }
    
  7. Save the file (<Ctrl>S) and exit the editor (<Ctrl>Q).

  8. Initialize Terraform.

    terraform init
    
  9. Create the Terraform plan.

    terraform plan
    
  10. Provision the resources that are defined in the swap.tf configuration file. (Confirm the action by entering yes at the prompt.)

    terraform apply
    
  11. After Terraform has swapped the slots, return to the browser. Refresh the page.

The web app in your slotAppServiceSlotOne staging slot has been swapped with the production slot and is now rendered in green.

The deployment slots have been swapped

To return to the original production version of the app, reapply the Terraform plan that you created from the swap.tf configuration file.

terraform apply

After the app is swapped, you see the original configuration.

Next steps