Reputation: 5543
My aws-app has multiple AWS CDK stacks among those stack is also RDS.
My goal is to execute code that creates new databases and tables in the newly provisioned DB before my software is actually deployed.
The problem is that the code that creates the database and tables is executed during the synth phase.
Upvotes: 1
Views: 584
Reputation: 16302
The CDK works by creating cloudformation templates and then calling the cloudformation create-stack or update-stack api to have the cloudformation service create, modify, or delete the resoruces based on stack templates.
You need to create a custom resource, most simply an aws lambda function that can connect to the database and create the tables. You can then use that custom resource in the stack definition to connect to your rds server and run the required commands to create the databases and users, generate a password (and store in Secrets manager), and grant permissions to the correct databases. https://docs.aws.amazon.com/cdk/api/latest/docs/custom-resources-readme.html describes how CDK can help you create a custom resource, then invoke it in later cdk stacks.
Creating custom resource to manage database users for various applications makes sense. along with the users, you'd want to create the databases and schemas to which they should have access.
If you haven't created an aws lambda before, it's a great experience. IT really forces you away from the server oriented mindset which can dramatically reduce cost and effort associated with web service creation and delivery.
You may prefer to handle database table management with a db schema management utility like liquibase or the ORM of your application framework to create and manage database tables. Since this configuration is closely coupled to the application code, getting it into the infrastructure automation is cumbersome and there's likely to be friction between the infrastructure processes and the application processes because the detailed db schema information will have to be passed between them.
Upvotes: 3