1

I created a Postgres RDS database using the CDK. I read the documentation but didn't find a way to create tables through it.

The language I'm using is TypeScript.

Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
Gulshan
  • 63
  • 8
  • Please, clarify the type of database you'd like to create, – AWS has a ton of options for a database, each of which has to be treated differently – Parzh from Ukraine Nov 09 '21 at 10:13

3 Answers3

0

You cannot create a RDS table using the CDK or CloudFormation.

You can use the AWS SDK client to execute a CREATE TABLE... command against your database. The SDK client can be used locally or in a lambda function, for instance.

fedonev
  • 20,327
  • 2
  • 25
  • 34
0

This post might help you. You need to use CustomResource

https://stackoverflow.com/a/62331465/6741215

shwz
  • 426
  • 1
  • 6
  • 22
0

There's no AWS CDK support for this. However, I've created a package named cloudformation-sql-run that helps with that. You can use it as follows:

const createPosts = new SqlRun(this, 'Create Posts', {
  vpc: vpc,
  connection: SqlRunConnection.fromDatabaseInstance(db),
  up: {
    run: [{
      sql: `CREATE TABLE posts(name varchar)`
    }, {
      sql: `INSERT INTO posts(name) VALUE (:secret)`,
      parameters: {
        secret: SqlSecret.fromSecretsManager(password)
      }
    }],
  },
  down: {
    run: [{
      sql: `DROP TABLE posts`
    }]
  }
});
miensol
  • 39,733
  • 7
  • 116
  • 112