Enjoying an AWS Aurora main course? How about ordering some healthy RDS backup sides beautifully served up by Terraform!

Written by Laurent Allegre, Cloud and DevOps Engineer at Airwalk Reply.


description: Custom Automation RDS snapshots
schemaVersion: '0.3'
assumeRole: '{{ AutomationAssumeRole }}'
parameters:
  InstanceId:
    type: String
    description: RDS Instance ID
    default: '${instance_id}'
  AutomationAssumeRole:
    type: String
    description: the ARN of the Role that allows to perform the actions on your behalf
    default: 'arn:aws:iam::{{ global:ACCOUNT_ID }}:role/${automation_assume_role}'
  DBClusterIndentifier:
    type: String
    description: The identifier of the DB Cluster to create a snapshot for. Not case-sensitive.
    default: '${cluster_identifier}'
  DBClusterSnapshotIdentifier:
    type: String
    description: The identifier of the DB cluster snapshot. Stored as lower case string.
    default: 'aurora-snapshot-{{ automation:EXECUTION_ID }}'
  SnsTopic:
    type: String
    description: The SNS topic to send automation notifications to, that users will be able to subscribe to.
    default: '${ssm_sns_topic}'
    
mainSteps:

  - name: AssertNotStartingOrAvailable
    action: 'aws:assertAwsResourceProperty'
    onFailure: step:FailedJobMessage
    isCritical: false
    nextStep: CheckDBInstance
    inputs:
      Service: rds
      Api: DescribeDBClusters
      DBClusterIdentifier: '{{ DBClusterIdentifier }}'
      PropertySelector: '$.DBClusters[0].Status'
      DesiredValues:
        - available
        - starting
        
  - name: CheckDBInstance
    action: 'aws:waitForAwsResourceProperty'
    onFailure: step:FailedJobMessage
    nextStep: createSnapshot
    maxAttempts: 10
    timeoutSeconds: 600
    inputs:
      Service: rds
      Api: DescribeDBInstances
      DBInstanceIndentifier: '{{ InstanceId }}'
      PropertySelector: '$.DBInstances[0].DBInstanceStatus'
      DesiredValues:
        - available
        
  - name: createSnapshot
    action: 'aws:executeAwsApi'
    maxAttempts: 3
    onFailure: step:FailedJobMessage
    nextStep: waitForSnapshotCompletion
    inputs:
      Service: rds
      Api: CreateDBClusterSnapshot
      DBClusterIdentifier: '{{ DBClusterIdentifier }}'
      DBClusterSnapshotIdentifier: '{{ DBClusterSnapshotIdentifier }}'
      
  - name: waitForSnapshotCompletion
    action: 'aws:waitForAwsResourceProperty'
    onFailure: step:FailedJobMessage
    nextStep: CompleteJobNotification
    inputs:
      Service: rds
      Api: DescribeDBClusterSnapshots
      DBClusterIdentifier: '{{ DBClusterIdentifier }}'
      DBClusterSnapshotIdentifier: '{{ DBClusterSnapshotIdentifier }}'
      PropertySelector: '$.DBClusterSnapshots[0].Status'
      DesiredValues:
        - available
        
  - name: CompleteJobNotification
    action: 'aws:executeAwsApi'
    onFailure: Abort
    inputs:
      Service: sns
      Api: Publish
      TopicArn: '{{ SnsTopic }}'
      Message: "RDS Snapshot created ID: {{ DBClusterSnapshotIdentifier }}"
      Subject: "RDS Snapshot complete"
    outputs:
    - Name: MessageId
      Selector: '$.MessageId'
      type: String
    isEnd: true
    
  - name: FailedJobMessage
    action: 'aws:executeAwsApi'
    onFailure: Abort
    inputs:
      Service: sns
      Api: Publish
      TopicArn: '{{ SnsTopic }}'
      Message: "RDS Snapshot Failed ID: {{ DBClusterSnapshotIdentifier }}"
      Subject: "RDS Snapshot Failed!!"
    outputs:
    - Name: MessageId
      Selector: '$.MessageId'
      type: String
    isEnd: true
    
View Full Version

resource "aws_ssm_document" "aurora_snaps" {
  name            = "RDS-snapshots"
  document_type   = "Automation"
  document_format = "YAML"
  tags            = var.tags
  content         = templatefile("${path.module}/ssm_rds_create_snap.yaml.tpl", {
                      automation_assume_role = aws_iam_role.ssm_automation.name
                      cluster_identifier     = aws_rds_cluster.default.cluster_identifier
                      instance_id            = aws_rds_cluster_instance.default.identifier
                      ssm_sns_topic          = aws_sns_topic.rds_snapshots_automation.arn
                      })
}

resource "aws_rds_cluster" "default" {
  cluster_identifier = var.cluster_identifier
  database_name      = var.db_name
  storage_encrypted  = true
  kms_key_id         = var.kms_key_arn
  ...
}

resource "aws_rds_cluster_instance" "default" {
  identifier         = var.cluster_identifier
  cluster_identifier = aws_rds_cluster.default.id
  instance_class     = var.instance_type
  ...
}



View Full Version