AWS Database Blog
Cross-account migration of HAQM RDS for SQL Server with column-level encryption
Organizations running SQL Server workloads on HAQM Relational Database Service (HAQM RDS) sometimes need to migrate their databases to different AWS accounts. This migration becomes more complex when mission-critical data requires column-level encryption to meet compliance requirements.
In this post, we demonstrate how you can migrate your symmetric key-encrypted database on HAQM RDS for SQL Server to another AWS account without compromising security. The solution we present can also help you implement symmetric key encryption on a new database in HAQM RDS for SQL Server.
Solution overview
In this solution, we demonstrate how to migrate a database that uses SQL Server certificate and symmetric key encryption from one HAQM RDS for SQL Server instance to another instance in a different AWS account. The workflow architecture illustrated in the diagram outlines the key components and steps of this migration process.
Before implementing this solution in your environment, note that it requires creating new AWS resources that will incur costs. For detailed pricing information, refer to AWS Pricing. We strongly recommend testing this solution in a non-production environment and performing comprehensive validation before deploying it in production.
Prerequisites
This post assumes that you’re familiar with navigating the AWS Management Console. For this example, you need the following resources and services enabled in your AWS account:
- An HAQM RDS for SQL Server instance in the source and target account
- An HAQM S3 bucket in the target account and grant access to the source RDS
- A SQL Server certificate
- A SQL Server symmetric key
Walkthrough
- Configure an HAQM Simple Storage Service (HAQM S3) bucket and its policy in the target account to allow access from the source account
- Setup a sample database on the source RDS instance
- Back up the SQL Server certificate and symmetric key
- Convert the SQL Server certificate and symmetric key into binary data
- Back up the SQL Server database from the source account to the S3 bucket in the target account
- Restore the database backup to the RDS for SQL Server instance in the target account
- Drop and recreate the symmetric key, certificate, and primary key in the RDS for SQL Server database in the target account
- Validate the encrypted data on the target account
Configure the S3 bucket in the target account to allow access from the source account
Refer to Setting up for cross-account native backup and restore in HAQM RDS for Microsoft SQL Server for detailed steps to configure the S3 bucket in the target account to allow access from the source account.
Setup a sample database on the source RDS instance
The following sample SQL commands will create a database and a table, insert rows after table creation. Further create certificate, symmetric key and encrypt a column with the certificate created.
Back up the SQL Server certificate and symmetric key
To migrate a database with column-level encryption to the RDS for SQL Server instance from another account, you must migrate both the SQL Server certificate and the symmetric key.
Before you begin, refer to the following screenshot. This post assumes that the encrypted_ssn
column in the dbo.employees
table of the employeedetails
database contains encrypted data. The encryption was performed using the SQL certificate named SQLCertificateemployeedetails
and the symmetric key named Sym_Key_employeedetails
.
The following screenshot shows commands to view the unencrypted data of the dbo.employees
table.
To back up the SQL Server certificate and private key from the source RDS instance, run the following command. The password used here must match the same password used to create the primary key on the source server.
Convert the SQL Server certificate and symmetric key into binary data
Both the symmetric key and the certificate must be migrated to the target RDS instance because they form an encryption hierarchy in SQL Server’s encryption system. Because HAQM RDS for SQL Server instances don’t allow direct file system access, you can’t directly copy certificate files to RDS instances. OPENROWSET provides a workaround for this limitation. To convert the SQL Server certificate and symmetric key into binary data, complete the following steps:
- To convert the backup of the SQL Server certificate using the function
OPENROWSET
, run the following command: - Copy the output of the column named
BulkColumn
and save it to a text file for later use on the target RDS instance. - Similarly, run the following command to convert the backup of the private key into binary data:
- Copy the output of the column named
BulkColumn
and save it to a text file for later use on the target RDS instance.
Now that you have created a binary copy of the SQL Server certificate and private key, proceed to the next step.
Back up the SQL Server database from the source account to the S3 bucket in the target account
To be able to back up and restore the database on HAQM RDS, the native SQL Server backup and restore option must be enabled and configured. Complete the following steps to back up the employeedetails
database from the source account to the S3 bucket configured in the previous step:
- Run the following command to back up the
employeedetails
database to your HAQM S3 bucket. - To check the status of the database backup, run the following command:
You can refer to the lifecycle
column in the command output to check for the database backup completion. When the command displays SUCCESS
, you should see the database backup file on the target S3 bucket.
Restore database backup to the RDS for SQL Server instance in the target account
Now that the source database backup is available in the S3 bucket, follow these steps to restore the database onto the target RDS instance:
- Connect to Target RDS for SQL Server from SQL Server Management Studio and run the following command to restore the database:
- You can check the status of the database restore by running the following command:
- Validate that the
employeedetails
database was restored along with the certificate and symmetric keys.
Although the SQL Server certificate and symmetric key are restored in the employeedetails
database, the data from the encrypted column is not accessible. You can run the following query to verify the results. Notice the output of the encrypted_ssn
column shows NULL
.
The preceding output is due to the fact that the master key is not encrypted by the target RDS for SQL Server instance. Run the following command to verify.
Notice the output value of the is_master_key_encrypted_by_server
column. If the output is 1
, the key is encrypted by the same server; otherwise, it is not.
Drop and recreate the symmetric key, certificate, and primary key in the RDS for SQL Server database in the target account
To access the encrypted data transferred to the target account, drop and recreate the target database’s existing symmetric key, certificate, and primary key. Refer to the following steps:
- Drop the symmetric key, certificate, and primary key in this order using the following command:
Dropping the symmetric key, certificate, and primary key doesn’t affect the existing encryption on other databases because the certificate or the key deletion is associated with a particular database only.
- Use the following command to create the primary key for the
employeedetails
database on the RDS for SQL Server instance in the target account. Use the same password you used to create the primary key on the source server. Using the same password maintains the encryption chain integrity and ensures successful data access after migration. - Next, create the SQL Server certificate using the binary data output of the SQL Server certificate and the private key generated in the steps 1 and 3 executed on the source RDS instance. Run the following command to create the SQL Server certificate:
- To create the symmetric key using the SQL Server certificate, run the following command:
- Make sure that the symmetric key, certificate, and primary key are recreated successfully. To validate that the key is server encrypted, run the following command:
Validate the encrypted data on the target account
Finally, to validate the encrypted data on the employeedetails
database in the target account, use the following command:
Clean up
Remember to clean up AWS resources that you no longer need in order to avoid additional costs. Please refer to Deleting a DB instance for more.
Conclusion
In this post, we showed you how to securely migrate a column-level encrypted database from one HAQM RDS for SQL Server instance to another AWS account. This solution also enables you to migrate self-managed SQL Server databases with symmetric key encryption to HAQM RDS, even without direct host access. By following these steps, you can maintain data security while performing cross-account database migrations.We encourage you to try this solution for your specific use case and share your experience in the comments section below.