Use Terraform to provision infrastructure with Azure deployment slots

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 and can be anything you want that represents your scenario.) After you configure your deployment slots, you can 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. By using the vi editor, create a file named deploy.tf. This file will contain the Terraform configuration.

    vi deploy.tf
    
  9. Enter insert mode by selecting the I key.

  10. 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}"
    }
    
  11. Select the Esc key to exit insert mode.

  12. Save the file and exit the vi editor by entering the following command:

    :wq
    
  13. Now that you've created the file, verify its contents.

    cat deploy.tf
    
  14. Initialize Terraform.

    terraform init
    
  15. Create the Terraform plan.

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

    terraform apply
    
  17. Close the Cloud Shell window.

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

    "Resource groups" selection in the portal

  19. 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 have deployed the production slot. To deploy the staging slot, perform all of the previous steps in this section with only 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.

Perform the following steps 2 times. In step 3, you select slotAppService the first time, and then select slotAppServiceSlotOne the second time.

  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

Note

It can take several minutes for Azure to build and deploy the site from GitHub.

For the slotAppService web app, you see a blue page with a page title of Slot Demo App 1. For the slotAppServiceSlotOne web app, you see a 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, perform 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. By using the vi editor, create a file named swap.tf.

    vi swap.tf
    
  6. Enter insert mode by selecting the I key.

  7. 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"
    }
    
  8. Select the Esc key to exit insert mode.

  9. Save the file and exit the vi editor by entering the following command:

    :wq
    
  10. Initialize Terraform.

    terraform init
    
  11. Create the Terraform plan.

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

    terraform apply
    
  13. After Terraform has finished swapping the slots, return to the browser that is rendering the slotAppService web app and 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.