How do I decouple an Amazon RDS instance from an Elastic Beanstalk environment without downtime, database sync issues, or data loss?

Last updated: 2022-12-23

I have an Amazon Relational Database Service (Amazon RDS) DB instance attached to my AWS Elastic Beanstalk environment. I want to remove the dependencies between the instance and the environment while avoiding downtime, database sync issues, and data loss.

Short description

Follow these steps to decouple your database from an Elastic Beanstalk environment without affecting the health of the environment:

  1. Create an Amazon RDS DB snapshot.
  2. Protect your RDS DB instance from deletion.
  3. Create a new Elastic Beanstalk environment.
  4. Perform a blue/green deployment.
  5. Update the database deletion policy for beanstalk environment A.
  6. Decouple the RDS instance from beanstalk environment A.
  7. Terminate the old Elastic Beanstalk environment.

Important: Attaching an RDS DB instance to an Elastic Beanstalk environment is ideal for development and testing environments. However, it's not recommended for production environments because the lifecycle of the database instance is tied to the lifecycle of your application environment. If you terminate the environment, then you lose your data because the RDS DB instance is deleted by the environment. For more information, see Using Elastic Beanstalk with Amazon RDS.

Resolution

Create an RDS DB snapshot

  1. Open the Elastic Beanstalk console.
  2. Choose the Elastic Beanstalk environment that you want to decouple from your RDS DB instance (environment A).
  3. In the navigation pane, choose Configuration.
  4. For Database, choose Modify.
  5. Select Endpoint.
  6. Create an RDS DB snapshot of your RDS DB instance.

Protect your RDS DB instance from deletion

  1. Open the Amazon RDS console.
  2. Choose your database, and then choose Modify.
  3. In Deletion protection, select the Enable deletion protection option.
  4. Choose Continue.
  5. In Scheduling Modifications, choose Apply immediately.
  6. Choose Modify DB Instance.
  7. Refresh the Amazon RDS console, and then verify that deletion protection is turned on.

Create a new Elastic Beanstalk environment

Your new Elastic Beanstalk environment (environment B) must not include an RDS DB instance in the same Elastic Beanstalk application.

Note:  To perform a blue/green deployment (or CNAME swap) later, make sure that environment A and environment B are part of the same application.

  1. Create environment B.
  2. Connect environment B to the existing RDS DB instance of environment A.
    Note: For more information, see Launching and connecting to an external Amazon RDS instance in a default VPC.
  3. Verify that environment B connects to the existing RDS DB instance, and that your application works correctly.

Perform a blue/green deployment to avoid downtime

  1. Open the Elastic Beanstalk console for environment B.
  2. Swap the environment URLs of the old and new Elastic Beanstalk environments.
    Note: For more information, see Blue/green deployments with Elastic Beanstalk.
  3. Verify that the URL of environment B responds, and that your application works correctly.

Important: Don't terminate environment A until the DNS changes are propagated, and your old DNS records expire. DNS records can take up to 48 hours to expire. DNS servers don't clear old records from their cache based on the time to live (TTL) that you set on your DNS records.

Update the Database deletion policy for beanstalk environment A

  1. Open the Elastic Beanstalk console for environment A.
  2. In the navigation pane, choose Configuration.
  3. In Database configuration, choose Edit.
  4. In Database settings, set the Database deletion policy to Retain.
  5. Choose Apply. It will take a few minutes to save the configuration change.

Important: Do not proceed to the next step until the Database deletion policy change is applied to beanstalk environment A.

Decouple the RDS instance from beanstalk environment A

  1. Open the Elastic Beanstalk console for environment A.
  2. In the navigation pane, choose Configuration.
  3. In Database configuration, choose Edit.
  4. In Database settings, verify that the Database deletion policy is set to Retain.
  5. Go to the Database connection section, and choose Decouple database.
  6. Choose Apply to initiate the database decoupling operation.

Note: The database remains operational during this period, and it usually takes less than five minutes to decouple a database.

Terminate the old Elastic Beanstalk environment

After the new environment’s functionality is validated, terminate the old Elastic Beanstalk environment (environment A).

When you terminate the environment, all Elastic Beanstalk resources are deleted, except for the RDS DB instance and the RDS security group that were created by Elastic Beanstalk.


Did this article help?


Do you need billing or technical support?