PowerShell script to enable transparent data encryption using your own key
Applies to: Azure SQL Managed Instance
This PowerShell script example configures transparent data encryption (TDE) in Azure SQL Managed Instance, using a customer-managed key from Azure Key Vault. This is often referred to as a bring-your-own-key (BYOK) scenario for TDE. To learn more, see Azure SQL Transparent Data Encryption with customer-managed key.
- An existing managed instance. See Use PowerShell to create a managed instance.
This article uses the Azure Az PowerShell module, which is the recommended PowerShell module for interacting with Azure. To get started with the Az PowerShell module, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.
Use Azure Cloud Shell
Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article, without having to install anything on your local environment.
To start Azure Cloud Shell:
|Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell.|
|Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser.|
|Select the Cloud Shell button on the menu bar at the upper right in the Azure portal.|
To run the code in this article in Azure Cloud Shell:
Start Cloud Shell.
Select the Copy button on a code block to copy the code.
Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux, or by selecting Cmd+Shift+V on macOS.
Select Enter to run the code.
Using PowerShell locally or using Azure Cloud Shell requires Azure PowerShell 2.3.2 or a later version. If you need to upgrade, see Install Azure PowerShell module, or run the below sample script to install the module for the current user:
Install-Module -Name Az -AllowClobber -Scope CurrentUser
If you are running PowerShell locally, you also need to run
Connect-AzAccount to create a connection with Azure.
# You will need an existing Managed Instance as a prerequisite for completing this script. # See https://docs.microsoft.com/en-us/azure/sql-database/scripts/sql-database-create-configure-managed-instance-powershell # Log in to your Azure account: Connect-AzAccount # If there are multiple subscriptions, choose the one where AKV is created: Set-AzContext -SubscriptionId "subscription ID" # Install the Az.Sql PowerShell package if you are running this PowerShell locally (uncomment below): # Install-Module -Name Az.Sql # 1. Create Resource and setup Azure Key Vault (skip if already done) # Create Resource group (name the resource and specify the location) $location = "westus2" # specify the location $resourcegroup = "MyRG" # specify a new RG name New-AzResourceGroup -Name $resourcegroup -Location $location # Create new Azure Key Vault with a globally unique VaultName and soft-delete option turned on: $vaultname = "MyKeyVault" # specify a globally unique VaultName New-AzKeyVault -VaultName $vaultname -ResourceGroupName $resourcegroup -Location $location # Authorize Managed Instance to use the AKV (wrap/unwrap key and get public part of key, if public part exists): $objectid = (Set-AzSqlInstance -ResourceGroupName $resourcegroup -Name "MyManagedInstance" -AssignIdentity).Identity.PrincipalId Set-AzKeyVaultAccessPolicy -BypassObjectIdValidation -VaultName $vaultname -ObjectId $objectid -PermissionsToKeys get,wrapKey,unwrapKey # Allow access from trusted Azure services: Update-AzKeyVaultNetworkRuleSet -VaultName $vaultname -Bypass AzureServices # Allow access from your client IP address(es) to be able to complete remaining steps: Update-AzKeyVaultNetworkRuleSet -VaultName $vaultname -IpAddressRange "xxx.xxx.xxx.xxx/xx" # Turn the network rules ON by setting the default action to Deny: Update-AzKeyVaultNetworkRuleSet -VaultName $vaultname -DefaultAction Deny # 2. Provide TDE Protector key (skip if already done) # First, give yourself necessary permissions on the AKV, (specify your account instead of contoso.com): Set-AzKeyVaultAccessPolicy -VaultName $vaultname -UserPrincipalName "firstname.lastname@example.org" -PermissionsToKeys create,import,get,list # The recommended way is to import an existing key from a .pfx file. Replace "<PFX private key password>" with the actual password below: $keypath = "c:\some_path\mytdekey.pfx" # Supply your .pfx path and name $securepfxpwd = ConvertTo-SecureString -String "<PFX private key password>" -AsPlainText -Force $key = Add-AzKeyVaultKey -VaultName $vaultname -Name "MyTDEKey" -KeyFilePath $keypath -KeyFilePassword $securepfxpwd # ...or get an existing key from the vault: # $key = Get-AzKeyVaultKey -VaultName $vaultname -Name "MyTDEKey" # Alternatively, generate a new key directly in Azure Key Vault (recommended for test purposes only - uncomment below): # $key = Add-AzureKeyVaultKey -VaultName $vaultname -Name MyTDEKey -Destination Software -Size 2048 # 3. Set up BYOK TDE on Managed Instance: # Assign the key to the Managed Instance: # $key = 'https://contoso.vault.azure.net/keys/contosokey/01234567890123456789012345678901' Add-AzSqlInstanceKeyVaultKey -KeyId $key.id -InstanceName "MyManagedInstance" -ResourceGroupName $resourcegroup # Set TDE operation mode to BYOK: Set-AzSqlInstanceTransparentDataEncryptionProtector -Type AzureKeyVault -InstanceName "MyManagedInstance" -ResourceGroup $resourcegroup -KeyId $key.id
For more information on Azure PowerShell, see Azure PowerShell documentation.
Additional PowerShell script samples for SQL Managed Instance can be found in Azure SQL Managed Instance PowerShell scripts.
Submit and view feedback for