- Newest
- Most votes
- Most comments
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.
{
"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
}
}
}
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']
}
If you are using Python (Boto3), these are the available methods:
Stop cluster: https://boto3.amazonaws.com/v1/documentation/api/1.28.1/reference/services/rds/client/stop_db_cluster.html
Start cluster: https://boto3.amazonaws.com/v1/documentation/api/1.26.83/reference/services/rds/client/start_db_cluster.html
Relevant content
- asked 4 months ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
what code on IAM policy to make that lambda running well?