Azure Deployment Slot Connection String

Azure Deployment Slot Connection String Average ratng: 3,8/5 5042 votes

To configure an app setting or connection string to stick to a specific slot (not swapped), go to the Configuration page for that slot. Add or edit a setting, and then select deployment slot setting. Selecting this check box tells App Service that the setting is not swappable. An App Service-specific connection string always follows the App Service during a swap, while a Slot-specific one stays tied to the slot. More about this feature can be found at Set up staging environments in Azure App Service and Azure Web Sites: How Application Strings and Connection Strings Work. Connection strings for Azure SQL Database. Connect using Microsoft.Data.SqlClient, SqlConnection, MSOLEDBSQL, SQLNCLI11 OLEDB, SQLNCLI10 OLEDB. When you create a new web application, Azure creates a deployment slot for you, typically called production. However, it's possible to add additional deployment slots. Put simply, a deployment slot is another web application. It has it's own URL, it could have its own database, connection strings, etc. Then, we configure it per deployment slot in the panel; which works great except for when we need a custom provider. I assume it works for all of you because the development info is deployed to the production server, then the connection string is overriden but not the provider name. Is this correct? – tne Apr 14 '15 at 10:41.

  1. Azure Deployment Slot Connection String Example
  2. Azure Deployment Slot Connection String Functions
  3. Azure Connection String Deployment Slot Setting

If you’ve deployed a website to Azure using App Services, you know you can override app settings and connection strings in the web.config using environment variables. This is a very handy feature since you can scrap web.config transforms all together if these are the only areas you need to touch.

Since ASP.NET Core does not use web.configs, at least not in a traditional way, how do we override these settings now? Well, pretty much in the exact same way, just with a different format. First let’s look at the new appsettings.json file that will be in your Core app:

{
“Logging”: {
“IncludeScopes”: false,
“LogLevel”: {
“Default”: “Debug”,
“System”: “Information”,
“Microsoft”: “Information”
}
},
“ConnectionStrings”: {
“ConnectionString1”: “localhost-connection-string”
},
“AppSettings”: {
“Environment”: “localhost”,
“NonSlot1”: “localhost1”,
“NonSlot2”: “localhost2”
}
}

Azure Deployment Slot Connection String Example

In this example, I simple have a Logging section (in there by default), a ConnectionStrings section (this must be named ‘ConnectionStrings’ if you truly want to use it as such), and an AppSettings section (you can name this anything, but I like to keep it familiar).

To override these in your Azure App Service web app, simply navigate to your web app in Azure (or the deployment slot, as in this image), and open up the Application Settings and scroll down to the App Settings and Connection strings sections.

Connection Strings

Connection strings are simple. Since Core encourages you to use a section called ConnectionStrings and only put connection strings in it, you can simply reference the connection string name without any other gunk on it.

App Settings

With app settings we have to craft the key name to match the structure of the json file. So:

“AppSettings”: {
“Environment”: “localhost”,
“NonSlot1”: “localhost1”,
“NonSlot2”: “localhost2”
}

Becomes:

  • AppSettings:Environment
  • AppSettings:NonSlot1
  • AppSettings:NotSlot2
Azure deployment slot connection string functions

You simply add a colon between the parent and child items. A double underscore also works: AppSettings__Environment.

What’s nice about this is, we can now have nested app settings (if you like) and easily reference them using this new structure and naming convention:

“AppSettings”: {

“AwesomeSettings”: {
“Environment”: “awesomehost”,
“NonSlot1”: “awesomehost1”,
“NonSlot2”: “awesomehost2”
}

“CoolSettings”: {
“Environment”: “coolhost”,
“NonSlot1”: “coolhost1”,
“NonSlot2”: “coolhost2”
}

}

This would look like:

  • AppSettings:AwesomeSettings:Environment
  • AppSettings:CoolSettings:Environment
Deployment

Very cool stuff! For more reading check these posts out:

Deployment slots have been an invaluable feature for Azure Web Apps for a long time. To find out how to create slots for Azure Web Apps, you can visit the official documentation [here](https://docs.microsoft.com/en-us/azure/app-service-web/web-sites-staged-publishing' target='_blank). So what makes deployment slots so useful? I summarize some of the benefits below but this is by no means the exhaustive list:

  • [Testing in Production - A/B Testing](https://cmatskas.com/azure-websites-testing-in-production/' target='_blank)
  • Hot deployments that allow deployment to production with no downtime
  • UAT testing targeting a near live environment
  • Easy roll out and roll back
  • Full or incremental swapping
  • DevOps integration with slots (VSTS deployment directly to slot)
  • many more

There are also some important caveats that you need to be aware of when using slots:

  • Shared resources with the live deployment
  • Setting persistence
  • Load/Performance testing on slots can affect the live site
  • Slots are only available on the Standard and Premium tier
  • some more

As long as you're aware of the caveats and treat deployment slots, you can significantly improve the way you perform your testing and deployment to Azure. And now Azure Functions can benefit from this awesome feature, which is currently in Preview like [Proxies](https://azure.microsoft.com/en-gb/updates/announcing-azure-functions-proxies-in-public-preview/' target='_blank)

Azure Deployment Slot Connection String Functions

Enable Function Deployment Slots

As soon as you log in to your Azure subscription and navigate to your Functions blade you'll be met with a new item in the navigation pane:

To start using slots you need to enable the feature first (one-time setting). When you press the + you'll be presented with the following message:

Click on the link to enable the feature in the app settings section:

Notice the message above the (On/Off) buttons. This is one-time opt-in on the Function app that cannot be disabled. Feel free to ignore this as slots can be safely ignored if you don't want to use them.

Creating and using slots

I'll use the portal to showcase how to create and use deployment slots.

Azure Connection String Deployment Slot Setting

I haven't seen any updates to the CLI and PoSH cmd-lets with regards to Azure Function slots . I'll update the blog post once I know for sure.

Let's go ahead and create our first slot. Click on the + button next to the Slots item. Enter a name for your slot (make it meaningful as the slot name will be appended to the Function URL. Press Create to go ahead an create that slot. The slot creation is instantaneous.

Deployment

With the slot in place, we can deploy our code. It's important to notice that the slot operates on the whole Function app and not individual functions. This means that if you plan on using this feature for testing, you'll need to replicate the whole 'live' Function code. For my example, I went against my own advice as I'm only showcasing the feature and I don't care about my 'live' code.

You'll notice that my new slots comes with it's own unique URL. This is fantastic because I can access this slot in isolation and test it independently from other slots or the code deploying in my 'Production' slot which is the default/live slot. The slot URLs have the following format:

https://yourwebsitename-<slotname>.azurewebsites.net

Swapping slots

Assuming that all our tests pass and everyone has signed off the new code, we can now swap the Production and Beta slots. To do this, you need to use the Swap button which is a new addition to the UI.

Azure

This will kick off the wizard that will guide you through the swap steps


You need to choose the source and destination slots and the type of swap (simple or with preview). If you want to find the different between simple and swap with preview, check the complete documentation [here](https://docs.microsoft.com/en-us/azure/app-service-web/web-sites-staged-publishing#swap-with-preview-multi-phase-swap' target='_blank). Howwever, if you want the gist of it, see below:

  • Keeps the destination slot unchanged so existing workload on that slot (e.g. production) is not impacted.
  • Applies the configuration elements of the destination slot to the source slot, including the slot-specific connection strings and app settings.
  • Restarts the worker processes on the source slot using these aforementioned configuration elements.
  • When you complete the swap: Moves the pre-warmed-up source slot into the destination slot. The destination slot is moved into the source slot as in a manual swap.
  • When you cancel the swap: Reapplies the configuration elements of the source slot to the source slot.

Once the swap is complete, you end up with something that looks like this:

With a couple of steps we were able to deploy our code safely to production. I believe that deployment slots are a great addition to Functions and massive Kudos to the team for integrating this feature to the service.

I would urge you to give them a go and see how you can benefit your own work flow and deployment process. As always, feel free to let me know in the comments if you have any questions or issues with this feature.