Skip to content
Advertisement

How to run a SQL query in Cloud Formation template to enable Delayed_Durability in AWS RDS

I have a Cloud Formation template to create a SQL DB in the RDS and want to enable Delayed_Durability feature by default in it by running this query: ALTER DATABASE dbname SET DELAYED_DURABILITY = FORCED;

Is there a way to run this query right after db instance is created through CF template? My CF template looks like this:

         "Type":"AWS::RDS::DBInstance",
         "Properties":{  
            "AllocatedStorage":"200",
            "AutoMinorVersionUpgrade":"false",
            "BackupRetentionPeriod":"1",
            "DBInstanceClass":"db.m4.large",
            "DBInstanceIdentifier":"mydb",
            "DBParameterGroupName": {
                    "Ref": "MyDBParameterGroup"
                },
            "DBSubnetGroupName":{  
               "Ref":"dbSubnetGroup"
            },
            "Engine":"sqlserver-web",
            "EngineVersion":"13.00.4422.0.v1",
            "LicenseModel":"license-included",
            "MasterUsername":"prod_user",
            "MasterUserPassword":{ "Ref" : "dbpass" },
            "MonitoringInterval":"60",
            "MonitoringRoleArn": {
               "Fn::GetAtt": [
                  "RdsMontioringRole",
                  "Arn"
               ]
            },
            "PreferredBackupWindow":"09:39-10:09",
            "PreferredMaintenanceWindow":"Sun:08:58-Sun:09:28",
            "PubliclyAccessible": false,
            "StorageType":"gp2",
            "StorageEncrypted": true,
            "VPCSecurityGroups":[  
               {  
                  "Fn::ImportValue":{  
                     "Fn::Sub":"${NetworkStackName}-RDSSecGrp"
                  }
               }
            ],
            "Tags":[  
               {  
                  "Key":"Name",
                  "Value":"my-db"
               }
            ]
         }
      }

Advertisement

Answer

Is there a way to run this query right after db instance is created through CF template?

Depends. If you want to do it from within CloudFormation (CFN) then sadly, you can’t do this using plain CFN. To do it from CFN, you would have to develop a custom resource. The resource would be in the form of lambda function. You would pass the DB details to the function in your CFN, and it could run and execute your query. It could also return any results you want to your CFN for further use.

In contrast, if you create your CFN stack using AWS CLI or SDK, then once create-stack call is completed, you can run your query from bash or any programming language you use do deploy your stack.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement