We use machine learning technology to do auto-translation. Click "English" on top navigation bar to check Chinese version.
Encrypt Amazon RDS for PostgreSQL and Amazon Aurora PostgreSQL database with minimal downtime
Recently one of our customers, asked us to help them encrypt their unencrypted
Amazon RDS offers two types of snapshots automatic and manual. This solution uses a manual snapshot. PostgreSQL logical replication uses a publish and subscribe model with one or more subscribers, subscribing to one or more publications on a publisher node. Amazon RDS for PostgreSQL and
As an Amazon RDS and Aurora security
Converting an unencrypted RDS for PostgreSQL or Amazon Aurora PostgreSQL database to encrypted
For an encrypted database instance data on storage, transaction logs, backups, and snapshots are encrypted. There are two ways you can accomplish. One is by using the
You can use the same solution for switching the encryption key from an
Overview of solution
This solution carries out the conversion in four high-level steps:
- Enable native PostgreSQL replication on the unencrypted source database and take a manual snapshot.
- Restore the snapshot to a new database instance with encryption enabled.
- Perform logical replication on the restored database for change data capture.
- Cut over your application to the encrypted database and retire (or delete) the unencrypted database.
Prerequisites
You can try out the hands-on steps using
You must complete the following steps before starting:
- Have an
Amazon Web Services account with permissions to create resources. - Have an unencrypted RDS for PostgreSQL DB cluster and PostgreSQL client utility to try out the steps discussed in this post. You can use an existing unencrypted database or
create a new database . Make sure that Enable Encryption is unchecked (see the following screenshot).
- Set up an
Amazon Elastic Compute Cloud (Amazon EC2) instance with connectivity to the DB instance. For more information, refer toCreating and connecting to a PostgreSQL DB instance .
Set up publication on the source database
In this step, you set up a
- To enable logical replication in Amazon RDS for PostgreSQL, modify a
custom parameter group to setrds.logical_replication
to 1 and attach the custom group to the database.
This step requires a reboot of the database instance. If your database instance is already setup with a custom parameter group, modify the aforementioned parameter and reboot the instance.
- Verify the setup by running the following SQL statement:
Now you can create the publication. You can select the tables for which you would like to capture the changes on the replica. For this use case, you capture changes for all tables. Because a publication is associated with a single database, you need to create one publication per database. In this post, we are assuming that there is a single database.
- Start a client session to the database using any PostgreSQL client application (such as psql) and run the following command:
Next, you create the
- Create the replication slot with the following code:
Backup and restore to create an encrypted database
In this step, take a backup and restore it to a new database instance. The new instance will have encryption enabled. Note that there is a minor difference between Amazon RDS for PostgreSQL and Amazon Aurora PostgreSQL-Compatible Edition from a restoration perspective. To create an encrypted Aurora cluster, you can
Note that the steps listed here are for Amazon RDS. The following diagram depicts the steps carried out in this phase.
Complete the following steps:
- Create an RDS snapshot using the Amazon RDS console or the following code via the Amazon Web Services Command Line Interface (Amazon Web Services CLI):
In this next step, you need to provide the
- Create an encrypted snapshot using the following code:
Now you can restore the database from the encrypted snapshot. Make sure to enable the PostgreSQL logs; you need the log sequence number (LSN) in a later step. You can carry out this step using the Amazon RDS console or the Amazon Web Services CLI, as documented in this post.
- Restore the database with the following code. Specify the VPC, subnet group and security group for your encrypted database. To ensure that the
rds.logical_replication
parameter is set up in the new database, use the same custom parameter group as your source database.
Set up replication between unencrypted and encrypted databases
At this stage your database is updated to the latest LSN available in the snapshot. Now you set up the
Start by verifying the replication setup on the encrypted database:
-
Create a subscription that will receive changes from the encrypted database:
By default, the subscription will be disabled; you enable it in a later step.
- Get the LSN from the PostgreSQL logs
You can get the LSN by opening the PostgreSQL logs in the Amazon RDS console,
You now use the PostgreSQL system administration function
- Connect to the encrypted database using a PostgreSQL client such as psql and run the following commands:
- Enable the replication:
- To Verify that replication is working, use the SQL client to ensure that changes are showing up in the encrypted database:
You should see lsn_distance = 0.
Keep in mind that the duration of full synchronization will depend on the amount of data changes made between the creation of the snapshot and enabling of replication.
When your new encrypted database is in sync with old unencrypted database, you’re ready for cutover.
Switch over applications to the encrypted database
At this time, you can repoint your applications to encrypted database using the guidance below.
- Disconnect your applications from the database and
stop the unencrypted database to prevent accidental writes. - Configure your applications to connect to the encrypted database instance
- Restart and verify the applications functionality
- Keep the unencrypted instance for a period of time before removing it.
Clean up
To avoid incurring future charges, delete the resources you created.
- Remove the publication
- Remove the replication slot:
- Disable the PostgreSQL logs and log publishing to CloudWatch (optional).
- Delete the CloudWatch group:
- Delete the unencrypted database if you no longer need it.
Conclusion
In this post, we showed how to convert an unencrypted RDS for PostgreSQL database to an encrypted database with minimum downtime. The process involves using an encrypted snapshot for restoration to an encrypted database instance followed by using native replication for CDC between the unencrypted and encrypted database instances. The benefit of this approach is that the overall time taken for this activity for especially large database is less. After the encrypted database has caught up with the changes on the unencrypted database, you can switch your applications to the encrypted database with minimal downtime.
If you have questions, comments, or feedback, leave them in the comments section.
About the Authors
Santosh Bhupathi is a Senior Database Specialist Solution Architect based in Philadelphia. With a focus on Relational and No-SQL databases, he provides guidance and technical assistance to customers to help them design, deploy, and optimize database workloads on Amazon Web Services.
Rajeev Sakhuja is a Solution Architect based out of New York City. He enjoys partnering with customers to solve complex business problems using Amazon Web Services services. In his free time, he likes to hike, and create video courses on application architecture & emerging technologies; check him out on Udemy.
The mentioned AWS GenAI Services service names relating to generative AI are only available or previewed in the Global Regions. Amazon Web Services China promotes AWS GenAI Services relating to generative AI solely for China-to-global business purposes and/or advanced technology introduction.