Here is what I would suggest.
Important note: we will provide better support for integrating Qovery with external services. This is a temporary solution that would work fine and give you the full flexibility you need.
Create a blueprint environment
- Turn off the Preview Environment feature for your
production
environment. - Clone your
production
environment and give a nameblueprint
to the cloned environment. - Turn on the Preview Environment feature for your
blueprint
environment.
The idea of the blueprint
environment is to act as a root environment for every new Preview Environment.
Add lifecycle app
To manage the creation of the external RDS instance, we will add an application into our blueprint environment named lifecycle
. This one will:
- Create the RDS instance and load the latest production RDS snapshot when the Preview Environment is created.
- Delete the RDS instance when the Preview Environment is deleted.
Lifecycle App code example
The idea is to create an app that will react on the first creation and the deletion of an environment.
For the creation of the app, we’ll need to execute a Terraform file that will create the Preview Environment RDS Instance
. While for the deletion of the app, we’ll execute a Terraform file that will delete the Preview Environment RDS instance
.
Handling creation and deletion
import os
import signal
import sys
from flask import Flask
PROJECT_ID = os.getenv('PROJECT_ID')
ENVIRONMENT_ID = os.getenv('ENVIRONMENT_ID')
def create_app():
app = Flask(__name__)
# 1. Exec your Terraform command here.
# 1.a Create RDS instance and get the endpoint of the instance + the credentials.
# 2. Inject RDS instance endpoint and credentials into Qovery Env environment variables
return app
app = create_app()
def delete_rds_instance(_signo, _stack_frame):
print("Deleting RDS instance...")
# retrieve the credentials from the RDS instance
# delete the RDS instance with Terraform
sys.exit(0)
if __name__ == '__main__':
signal.signal(signal.SIGTERM, delete_rds_instance)
app.run(host="0.0.0.0", port=5555)
Here my example is with Python but you can use the language that fits best you
AWS RDS instance credentials
To get the AWS RDS instance credentials into your app, you have two options here:
- Not recommended: You can hardcode the credentials of your RDS instance into your application
- Recommended: You can generate the credentials and inject them as Qovery Secret via the Qovery API. I can provide an example if you want
Lifecycle App needs to start first
Since Lifecycle App creates all the resources required by your apps to properly starts, I’d recommend to force it to start first. You can take a look at this thread.
Let me know if you get the idea or you need any clarification