6

I'm fairly new to AWS CDK, and I just created an architecture with an Aurora cluster inside of it. Is there a way to initialize a database schema inside of my CDK files? It seems a bit annoying to create the instance and then have to connect through other means in order to set up a schema. My impression of CDK is that you can do everything relating to setting up the services, so I was curious if this is a possibility.

If this isn't possible, can I use a lambda that fires off after the CDK deployment? Or maybe have a snapshot inside of s3? I'm just trying to find the best solution!

korum
  • 172
  • 1
  • 14

1 Answers1

10

You can do this, although it requires you to execute it as a custom resource within the CDK.

You would need to create a Lambda function the performs the SQL DDL tasks for you. This would then return a successful flag back to the execution runtime to say that this "resource" was successfully created.

Chris Williams
  • 32,215
  • 4
  • 30
  • 68
  • 2
    What would this look like? Do I initialize a lambda as a custom resource and then run it, or do I intialize a lambda first and then run it as a custom resource? – Jesper Bylund Sep 20 '20 at 06:55
  • 3
    The Lambda does not need to be launched ahead of time, an example is here: https://github.com/aws-samples/aws-cdk-examples/blob/master/typescript/custom-resource/my-custom-resource.ts#L23. The custom resource will launch the Lambda and pass the parameters in. – Chris Williams Sep 20 '20 at 07:01
  • 2
    There's a blog post with code using the CDK and custom resources here: https://aws.amazon.com/blogs/infrastructure-and-automation/use-aws-cdk-to-initialize-amazon-rds-instances/ – Scott McAllister Jun 11 '22 at 11:20