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

Amazon Web-ServicesAmazon RdsAmazon Cloudformation

Amazon Web-Services Problem Overview


I am creating an RDS instance using CloudFormation using this:

"Resources": {
"myDB": {
"Type": "AWS::RDS::DBInstance",
"Properties": {
"AllocatedStorage": "5",
"DBInstanceClass": "db.m1.small",
"Engine": "MySQL",
"EngineVersion": "5.5",
"DBName": "mydb",
"MasterUsername": {
"Ref": "DBUser"
},
"MasterUserPassword": {
"Ref": "DBPassword"
},
"DBParameterGroupName": {
"Ref": "myRDSParamGroup"
}
}
}

and it all works. But I need to run initial SQL on the DB when its created, to setup my apps schema. My current approach is to have the app self migrating, but I'd like to do it in the CloudFormation definition. Is this possible?

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

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.

Solution 2 - Amazon Web-Services

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.

Solution 3 - Amazon Web-Services

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.

Solution 4 - Amazon Web-Services

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMike HoganView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesjamiebView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicessigpwnedView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicescazzerView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesmwdevView Answer on Stackoverflow