How do I move an Amazon RDS instance out of an Availability Zone?

6 minute read
3

I want to move an Amazon Relational Database Service (Amazon RDS) or Amazon Aurora instance out of an Availability Zone. I also want to delete the subnet that's in an Amazon RDS DB subnet group.

Short description

To launch an Amazon RDS DB instance, a DB subnet group must contain at least two subnets. You can launch RDS instances only in subnets within the associated DB subnet group. The subnets must be in different Availability Zones in the same AWS Region. To minimize disruption, it's a best practice to have subnets from at least three Availability Zones.

Resolution

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.

Determine the Availability Zone

Availability Zone IDs

Use Availability Zone IDs to determine the location of resources in one AWS account in relation to the resources in a different account. For example, you have Amazon Elastic Compute Cloud (Amazon EC2) instances that are running in an account that's located in use1-az2. These EC2 instances communicate with RDS databases that are running in an account that's in use1-az3. To avoid the additional costs and latency that cross-AZ traffic creates, run both instances in the same Availability Zone. To reduce downtime if an outage occurs, use a Multi-AZ deployment.

To check the Availability Zone mappings in your account and get your Availability Zone names, run the describe-availability-zones command:

aws ec2 describe-availability-zones --zone-ids ZONE ID --region REGION

Note: Replace ZONE ID with your Availability Zone's ID and REGION with your Region.

Identify the RDS instance that you want to move

To identify the RDS instance that you want to move, use the Availability Zone name in AWS Config, or run list commands in the AWS CLI.

AWS Config

Turn on AWS Config. Then, run the following command to return a list of DB instances that run or have a secondary node in the Availability Zone:

SELECT
  resourceId,
  resourceName,
  accountId,
  awsRegion,
  configuration.availabilityZone,
  configuration.secondaryAvailabilityZone
WHERE
  resourceType = 'AWS::RDS::DBInstance'
  AND (
    configuration.availabilityZone = 'us-east-1e'
    OR configuration.secondaryAvailabilityZone = 'us-east-1e'
  )

Note: In the preceding query, the Availability Zone must match the zone name from the mappings in your account.

AWS CLI

Run the describe-db-instances command to list the RDS DB instances that are currently deployed in the Availability Zone:

aws rds describe-db-instances \
    --query 'DBInstances[?Engine!=`aurora-postgresql`&&Engine!=`aurora-mysql`&&(AvailabilityZone==`AVAILABILITY ZONE` || SecondaryAvailabilityZone==`AVAILABILITY ZONE`)].{"0-Engine":Engine, "1-DBInstanceIdentifier":DBInstanceIdentifier, "2-DBSubnetGroupName":DBSubnetGroup.DBSubnetGroupName, "3-AvailabilityZone":AvailabilityZone, "4-SecondaryAvailabilityZone":SecondaryAvailabilityZone} | sort_by([], &"1-DBInstanceIdentifier")' \
    --region us-east-1

Run the describe-db-instances command to list the Aurora DB instances that are currently deployed in the Availability Zone:

aws rds describe-db-instances \
    --query 'DBInstances[?Engine==`aurora-postgresql`&&Engine==`aurora-mysql`&&AvailabilityZone==`AVAILABILITY ZONE`].{"0-Engine":Engine, "1-DBClusterIdentifier":DBClusterIdentifier, "2-DBInstanceIdentifier":DBInstanceIdentifier, "3-DBSubnetGroupName":DBSubnetGroup.DBSubnetGroupName, "4-AvailabilityZone":AvailabilityZone} | sort_by([], &"1-DBClusterIdentifier")'  \
    --region us-east-1

Move the instance out of the Availability Zone

RDS DB instance

Note: You can't change the Availability Zone of a Single-AZ deployment.

To move an RDS instance out of an Availability Zone, complete the following steps:

  1. If the RDS instance is a Single-AZ deployment, then modify the DB instance to be a Multi-AZ deployment. The Multi-AZ deployment creates a standby DB instance in a different Availability Zone that uses a different subnet.
  2. Reboot the DB instance with failover. The standby DB instance becomes the primary DB instance.
    Note: Make sure that the DB instance appears in the new Availability Zone after failover.
  3. Modify the DB instance to be a Single-AZ DB instance. The Single-AZ DB instance is then removed from the subnet that you want to delete.

Aurora DB instance

To move an Aurora instance out of an Availability Zone, complete the following steps:

  1. If your Aurora DB instance is a Single-AZ deployment, then add a new reader instance to your Aurora DB cluster. When you're creating the new reader instance, choose one of the Availability Zones from your DB subnet group. 
  2. Force a failover for the DB cluster to promote the new reader instance as the new writer DB instance.
    Note: If you have more than one target reader instance and you don't specify one, then Amazon RDS promotes the reader with the highest promotion tier. Priorities range from 0 for the highest priority to 15 for the lowest priority. For more information, see Fault tolerance for an Aurora DB cluster.
  3. Delete the previous primary DB instance.

Remove the subnets from the DB subnet group

Note: If you launched an instance in the subnet, then you can't delete the subnet. If you launched DB instances in the subnet that you're deleting, then you get the following error message: "We're sorry, your request to modify DB subnet group has failed. Some of the subnets to be deleted are currently in use: "

To remove the subnets from the DB subnet group, complete the following steps:

  1. Open the Amazon RDS console.

  2. In the navigation pane, choose Subnet groups.

  3. Select the subnet group that the DB instance is associated with, and then choose Edit.

  4. On the Edit DB subnet groups page, under Add subnets, remove the subnets that you want to delete

  5. To check what RDS DB instances are still in the Availability Zone, run the describe-db-instances command:

    aws rds describe-db-instances \
        --query 'DBInstances[?Engine!=`aurora-postgresql`&&Engine!=`aurora-mysql`&&(AvailabilityZone==`AVAILABILITY ZONE` || SecondaryAvailabilityZone==`AVAILABILITY ZONE`)].{"0-Engine":Engine, "1-DBInstanceIdentifier":DBInstanceIdentifier, "2-DBSubnetGroupName":DBSubnetGroup.DBSubnetGroupName, "3-AvailabilityZone":AvailabilityZone, "4-SecondaryAvailabilityZone":SecondaryAvailabilityZone} | sort_by([], &"1-DBInstanceIdentifier")' \
        --region us-east-1

    To check what Aurora DB instances are still in the Availability Zone, run the describe-db-instances command:

    aws rds describe-db-instances \
        --query 'DBInstances[?Engine==`aurora-postgresql`&&Engine==`aurora-mysql`&&AvailabilityZone==`AVAILABILITY ZONE`].{"0-Engine":Engine, "1-DBClusterIdentifier":DBClusterIdentifier, "2-DBInstanceIdentifier":DBInstanceIdentifier, "3-DBSubnetGroupName":DBSubnetGroup.DBSubnetGroupName, "4-AvailabilityZone":AvailabilityZone} | sort_by([], &"1-DBClusterIdentifier")'  \
        --region us-east-1
  6. Choose Save.

Return the RDS instance to the original deployment

If you started with a Multi-AZ deployment, then modify the DB instance to be a Multi-AZ deployment. If you started with a Single-AZ deployment, then no further steps are required.

If you removed a reader instance from your Aurora DB cluster, then add a new reader DB instance to your cluster. If you started with a Single-AZ deployment, then no further steps are required.

Related information

Working with DB subnet groups

How can I move an Amazon RDS DB instance from a public subnet to a private subnet within the same VPC?

How can I delete a subnet that is part of an Amazon RDS DB subnet group?

AWS OFFICIAL
AWS OFFICIALUpdated 4 months ago