AWS Lambda function to start/stop Aurora Cluster

0

i have aurora cluster, and i want to schedule, is there a way to schedule start/stop of aurora cluster via lambda ? The idea here is to save cost. I was able to schedule start/stop of my RDS instance with lambda and cloudwatch rules. But i cannot find anything similar for my aurora cluster.

BayuAL
asked 3 months ago412 views
3 Answers
3

This seems like a good scenario to use a Step Functions state machine so you don't need to write, deploy, and maintain a Lambda Function. A Lambda Function with Boto3 is fine here, but when you are just calling other AWS Service API Actions, Step Functions Service Integrations are probably a better fit. And then you can add easily-configurable failure handling or the ability to add additional compensating logic (maybe you want to notify someone if you are unable to start a cluster as expected).

Below is an example of what that might look like, building on the Boto3 answer provided by Gary earlier.

Enter image description here

{
  "Comment": "A state machine to stop an RDS Instance or Cluster",
  "StartAt": "Cluster or Instance?",
  "States": {
    "Cluster or Instance?": {
      "Type": "Choice",
      "Choices": [
        {
          "And": [
            {
              "Variable": "$.rds_type",
              "IsPresent": true
            },
            {
              "Variable": "$.rds_type",
              "StringEquals": "Instance"
            }
          ],
          "Next": "Stop Instance"
        }
      ],
      "Default": "Stop Cluster"
    },
    "Stop Instance": {
      "Type": "Task",
      "Parameters": {
        "DbClusterIdentifier.$": "$.rds_cluster"
      },
      "Resource": "arn:aws:states:::aws-sdk:rds:stopDBInstance",
      "Retry": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "BackoffRate": 2,
          "IntervalSeconds": 8,
          "MaxAttempts": 6,
          "MaxDelaySeconds": 290,
          "JitterStrategy": "FULL"
        }
      ],
      "End": true
    },
    "Stop Cluster": {
      "Type": "Task",
      "Parameters": {
        "DbClusterIdentifier.$": "$.rds_cluster"
      },
      "Resource": "arn:aws:states:::aws-sdk:rds:stopDBCluster",
      "Retry": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "BackoffRate": 2,
          "IntervalSeconds": 8,
          "MaxAttempts": 6,
          "MaxDelaySeconds": 290,
          "JitterStrategy": "FULL"
        }
      ],
      "End": true
    }
  }
}
AWS
answered 3 months ago
profile picture
EXPERT
reviewed 3 months ago
2

Its the same for aurora as well as RDS.. Are you using python?

In boto3 use stop_db_cluster https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rds/client/stop_db_cluster.html

I use a step function to pass in the RDS Type to lambda. Depending on if its a Instance or Cluster it will execute a different command.

import os
import json
import logging
import boto3

#Logging
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)

from botocore.exceptions import ClientError

print(boto3.__version__)

rdsClient = boto3.client('rds')

def lambda_handler(event, context):
    rds_cluster = event['rdsInstanceId']
    if event['rdsType'] == 'Cluster':
        print ("Stopping RDS Cluster" + rds_cluster)
        rdsClient.stop_db_cluster(
            DBClusterIdentifier=rds_cluster
        )
    elif event['rdsType'] == 'Instance':
        print ("Stopping RDS Instance" + rds_cluster)
        rdsClient.stop_db_instance(
            DBInstanceIdentifier=rds_cluster
        )
        
    return {
        'statusCode': 200,
        'rdsInstanceId': rds_cluster,
        'rdsType': event['rdsType']
        }
profile picture
EXPERT
answered 3 months ago
EXPERT
reviewed 3 months ago
  • what code on IAM policy to make that lambda running well?

0
profile picture
EXPERT
answered 3 months ago
profile picture
EXPERT
reviewed 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions