Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 750
Mike Hogan Avatar asked Jan 17 '13 17:01

Mike Hogan


2 Answers

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.

like image 50
jamieb Avatar answered Sep 16 '22 20:09

jamieb


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.

like image 33
sigpwned Avatar answered Sep 19 '22 20:09

sigpwned