How do I decouple an Amazon RDS instance from an AWS Elastic Beanstalk environment without downtime, database sync issues, or data loss?
Last updated: 2019-04-16
I have an Amazon Relational Database Service (Amazon RDS) instance attached to my AWS Elastic Beanstalk environment, and I want to remove the dependencies between the instance and the environment. How can I avoid downtime, database sync issues, or data loss when I do this?
Follow the steps below to:
- Use an Elastic Beanstalk blue (environment A) / green (environment B) deployment to decouple an Amazon RDS instance from environment A.
- Create a new Elastic Beanstalk environment (environment B) with the necessary information to connect to the Amazon RDS instance.
Note: An Amazon RDS instance attached to an Elastic Beanstalk environment is ideal for development and testing environments. However, it's not ideal 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 will lose your data because the Amazon RDS instance is deleted by the environment. For more information, see Using Elastic Beanstalk with Amazon Relational Database Service.
Create an RDS DB snapshot
- Open the Elastic Beanstalk console.
- Choose the Elastic Beanstalk environment that you want to decouple from your Amazon RDS instance (environment A).
- In the navigation pane, choose Configuration.
- For Database, choose Modify.
- Choose Endpoint.
- Create a DB snapshot of your Amazon RDS instance.
Safeguard your Amazon RDS DB instance from deletion
- Open the Amazon RDS console.
- Choose your database, and then choose Modify.
- In the Deletion protection section, select the Enable deletion protection option.
- Choose Continue.
- In the Scheduling Modifications section, choose Apply immediately.
- Choose Modify DB Instance.
- Refresh the Amazon RDS console, and then verify that deletion protection has been enabled successfully.
Create a new Elastic Beanstalk environment
Your new Elastic Beanstalk environment (environment B) must not include an Amazon RDS instance in the same Elastic Beanstalk application.
Note: To perform a blue/green deployment (or CNAME swap) later, verify that environment A and environment B are using the same application version.
- Create environment B.
- Connect environment B to the existing Amazon RDS instance of environment A.
- Verify that environment B can connect to the existing Amazon RDS instance and that your application functions as expected.
Perform a blue/green deployment to avoid downtime
- Open the Elastic Beanstalk console for environment B.
- Swap the environment URLs of the old and new Elastic Beanstalk environments.
- Verify that the URL of environment B responds and is working as expected when you open it.
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 necessarily clear old records from their cache based on the time to live (TTL) that you set on your DNS records.
Remove the security group rule for the old Elastic Beanstalk environment
- Open the Amazon RDS console.
- Choose your database.
- Choose the Connectivity & security view.
- From Security, choose the security group that you need to modify.
- For Actions, choose Edit inbound rules.
- Remove the rule which contains the security group of environment A as a source.
- In the Elastic Beanstalk console, verify that environment B and your application are working as expected.
Terminate the old Elastic Beanstalk environment
Terminate the old Elastic Beanstalk environment (environment A).
Note: Terminating environment A deletes all Elastic Beanstalk resources except for the Amazon RDS instance and the old security group due to deletion protection.
Delete the stack
- Open the AWS CloudFormation console.
- Choose the stack based on the environment ID of environment A.
Note: The status of your stack is DELETE_FAILED.
- For Actions, choose Delete Stack.
- In the Delete Stack pop-up window, choose Yes, Delete.
- (Optional) Terminate environment A and remove the RDS snapshot that you created earlier.