How to do it...

Perform the following steps:

  1. Create a new function app named MyProductionApp.
  2. Create a new HTTP trigger and name it MyProd-HttpTrigger1. Replace the last line with the following:
         return name != null
? (ActionResult)new OkObjectResult("Welcome to MyProd-HttpTrigger1 of Production App")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
  1. Create another new HTTP trigger and name it MyProd-HttpTrigger2. Use the same code that you used for MyProd-HttpTrigger1—just replace the last line with the following:
return name != null
? (ActionResult)new OkObjectResult("Welcome to MyProd-HttpTrigger2 of Production App")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
  1. Assume that both the functions of the function app are live on your production environment with the URL https://<<functionappname.azurewebsites.net>>.
  2. Now, the customer has requested us to make some changes to both functions. Instead of directly making the changes to the functions of your production function app, you might need to create a slot.
  1. Hold on! Before you can create a slot, you first need to enable the feature by navigating to the Function app settings under the General Settings of the Platform features tab of the function app. Once you click on the Function app settings, a new tab will be opened where you can enable the Slots (preview), as shown in the following screenshot:
The slots feature is currently in preview. By the time you read this, it might be Generally Available (GA). It is not recommended to use this feature in production workloads until it goes GA
  1. Click the On button in the Slots (preview) section highlighted in the preceding screenshot. As soon as you turn it on, the slots section will be hidden, as it is a one-time setting. Once it's enabled, you cannot disable it.
  2. OK, let's create a new slot with all the functions that we have in our function app, named MyProductionApp.
  3. Click on the + icon, available near the Slots (preview) section, as shown in the following screenshot:
  1. It prompts you to enter a name for the new slot. Provide a meaningful name, something such as Staging, as shown in the following screenshot:
  1. Once you click on Create, a new slot will be created, as shown in the following screenshot. In case, if you see the Functions as read only, you can make them read-write in the Function App Settings.
The URL for the slot will be https://<<functionappname>>-<<Slotname>>.azurewebsites.net>>. Each slot within a function app will have a different URL.
  1. To make a staged environment complete, you need to copy all the Azure Functions from the production environment (in this case, the MyProductionApp app) to the new staged slot named Staging. Create two HTTP triggers and copy both the functions' code (MyProd-HttpTrigger1 and MyProd-HttpTrigger2) from MyProductionApp to the new Staging slot. Basically, you need to copy all the functions to the new slot manually.
  1. Change the production string to staging in the last line of both the functions in the Staging slot. This is useful for testing the output of the swap operation:
Note that, in all the slots that you create as a pre-production app, you need to make sure that you use the same function names as you have in your production environment.
  1. Click on the Swap button, available in the Deployment slots blade, as shown in the following screenshot:
  1. In the Swap blade, you need to choose the following:
    • Swap Type: Choose the Swap option.
    • Source: Choose the slot that you would like to move to production. In this case, we're swapping Staging in general, but you can even swap across non-production slots.
    • Destination: Choose the production option, as shown in the following screenshot:
  1. Once you review the settings, click on the OK button of the preceding step. It will take a few moments to swap the functions. A progress bar will appear, as shown in the following screenshot:
  1. After a minute or two, the staging and production slots get swapped. Let's review the run.csx script files of the production:
  1. If you don't see any changes, click on the refresh button of the function app, as shown in the following screenshot:
  1. Make sure that the Application settings and Database Connection Strings are marked as Slot Setting (slot-specific). Otherwise, Application settings and Database Connection Strings will also get swapped, which could cause unexpected behavior. You can mark any of these settings as such from Platform features, as shown in the following screenshot:
  1. Clicking on the Application settings will take you to the following blade, where you can mark any setting as a SLOT SETTING:
All the functions taken in the recipe are HTTP triggers; note that you can have any kind of triggers in the function app. The deployment slots are not limited to HTTP triggers.

You can have multiple slots for each of your function apps. The following are a few of the examples:
  • Alpha
  • Beta
  • Staging