Is there a way to run initial SQL when creating an RDS database instance using CloudFormation?

后端 未结 4 1415
北海茫月
北海茫月 2020-12-24 05:21

I am creating an RDS instance using CloudFormation using this:

\"Resources\": {
        \"myDB\": {
            \"Type\": \"AWS::RDS::DBInstance\",
                     


        
相关标签:
4条回答
  • 2020-12-24 05:26

    No, it's not possible. However, you could have an EC2 instance connect to your RDS instance to do it. I'd probably store a .sql file in S3 and use a cloud-init script on the EC2 instance to download the file and execute it.

    0 讨论(0)
  • 2020-12-24 05:37

    It would also be possible to create a CloudFormation custom resource. There is a good discussion about how to build one using SNS here; it is also possible to build one using Lambda. Custom resources are essentially just RPCs, so it wouldn't be difficult to create one to initialize a database with a schema, for example.

    0 讨论(0)
  • 2020-12-24 05:43

    CloudFormation still doesn't hold any solutions for us, but hopefully they will add Database Migration Service support soon.

    In the meantime, there is great solution if you're using CodePipeline: create a migration stage that invokes a Lambda function to run your migration. I stumbled across this guide for invoking Lambda from CodePipeline that may be helpful for those unfamiliar.

    0 讨论(0)
  • 2020-12-24 05:47

    Another option is to use DBSnapshotIdentifier property for AWS::RDS::DBInstance resource. The only catch is that you need to have a DB loaded in AWS to create the snapshot in the first place. From then on, you can automate your cloudformation stack to be using it though.

    DBSnapshotIdentifier: Name (ARN) of the DB snapshot that's used to restore the DB instance.

    If the property contains a value (other than an empty string), AWS CloudFormation creates a database from the specified snapshot.

    After you restore a DB instance with a DBSnapshotIdentifier property, you must specify the same DBSnapshotIdentifier property for any future updates to the DB instance. When you specify this property for an update, the DB instance is not restored from the DB snapshot again, and the data in the database is not changed. However, if you don't specify the DBSnapshotIdentifier property, an empty DB instance is created, and the original DB instance is deleted.

    Look in the doc from more info: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-rds-database-instance.html#cfn-rds-dbinstance-dbsnapshotidentifier

    0 讨论(0)
提交回复
热议问题