0

I used the below lambda function to stop my rds aurora database. But it always ends in an error "RDS' object has no attribute 'stop_db_cluster'". can anyone help me here;

import sys
import botocore
import boto3
from botocore.exceptions import ClientError
def lambda_handler(event, context):
    client = boto3.client('rds')
    lambdaFunc = boto3.client('lambda')
    print ('Trying to get Environment variable')
    try:
        funcResponse = lambdaFunc.get_function_configuration(
            FunctionName='RDSInstanceStop'
        )
        DBinstance = funcResponse['Environment']['Variables']['DBInstanceName']
        print ('Stoping RDS service for DBInstance : ' + DBinstance)
    except ClientError as e:
        print(e)    
    try:
        response = client.stop_db_cluster(
            DBClusterIdentifier='DBInstanceName'
        )
        print ('Success :: ' )
        return response
    except ClientError as e:
        print(e)    
    return
    {
        'message' : "Script execution completed. See Cloudwatch logs for complete output"
    }

i am using the role - lambda-start-stop-rds my policy details - { "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "rds:ResetDBParameterGroup", "rds:DescribeEngineDefaultParameters", "rds:CreateOptionGroup", "rds:CreateDBSubnetGroup", "rds:PurchaseReservedDBInstancesOffering", "logs:CreateLogStream", "rds:ModifyDBParameterGroup", "rds:AddSourceIdentifierToSubscription", "rds:DownloadDBLogFilePortion", "rds:CopyDBParameterGroup", "rds:AddRoleToDBCluster", "rds:ModifyDBInstance", "rds:ModifyDBClusterParameterGroup", "rds:ModifyDBClusterSnapshotAttribute", "rds:DeleteDBInstance", "rds:CreateDBParameterGroup", "rds:DescribeDBSnapshots", "rds:DeleteDBSnapshot", "rds:DescribeDBSecurityGroups", "logs:CreateLogGroup", "rds:PromoteReadReplica", "rds:StartDBInstance", "rds:DeleteDBSubnetGroup", "rds:DescribeReservedDBInstances", "rds:CreateDBSnapshot", "rds:DescribeValidDBInstanceModifications", "rds:RestoreDBInstanceFromDBSnapshot", "rds:DeleteDBSecurityGroup", "rds:DescribeOrderableDBInstanceOptions", "rds:ModifyDBCluster", "rds:CreateDBClusterSnapshot", "rds:DeleteDBParameterGroup", "rds:DescribeCertificates", "rds:CreateDBClusterParameterGroup", "rds:ModifyDBSnapshotAttribute", "rds:RemoveTagsFromResource", "rds:DescribeOptionGroups", "rds:AuthorizeDBSecurityGroupIngress", "rds:CreateEventSubscription", "rds:ModifyOptionGroup", "rds:RestoreDBClusterFromSnapshot", "rds:DescribeDBEngineVersions", "rds:DescribeDBSubnetGroups", "rds:DescribePendingMaintenanceActions", "rds:DescribeDBParameterGroups", "rds:DescribeReservedDBInstancesOfferings", "rds:DeleteOptionGroup", "rds:FailoverDBCluster", "rds:DeleteEventSubscription", "rds:RemoveSourceIdentifierFromSubscription", "rds:CreateDBInstance", "rds:DescribeDBInstances", "rds:DescribeEngineDefaultClusterParameters", "rds:RevokeDBSecurityGroupIngress", "rds:DescribeDBParameters", "rds:DescribeEventCategories", "rds:ModifyCurrentDBClusterCapacity", "rds:DeleteDBCluster", "rds:ResetDBClusterParameterGroup", "rds:RestoreDBClusterToPointInTime", "rds:DescribeEvents", "rds:AddTagsToResource", "rds:DescribeDBClusterSnapshotAttributes", "rds:DescribeDBClusterParameters", "rds:DescribeEventSubscriptions", "rds:CopyDBSnapshot", "rds:CopyDBClusterSnapshot", "rds:ModifyEventSubscription", "rds:DescribeDBLogFiles", "rds:StopDBInstance", "logs:PutLogEvents", "rds:CopyOptionGroup", "rds:DescribeDBSnapshotAttributes", "rds:DeleteDBClusterSnapshot", "rds:ListTagsForResource", "rds:CreateDBCluster", "rds:CreateDBSecurityGroup", "rds:RebootDBInstance", "rds:DescribeDBClusterSnapshots", "rds:DescribeOptionGroupOptions", "rds:DownloadCompleteDBLogFile", "rds:DeleteDBClusterParameterGroup", "rds:ApplyPendingMaintenanceAction", "rds:CreateDBInstanceReadReplica", "rds:DescribeAccountAttributes", "rds:DescribeDBClusters", "rds:DescribeDBClusterParameterGroups", "rds:ModifyDBSubnetGroup", "rds:RestoreDBInstanceToPointInTime" ], "Resource": "*" } ]

}

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "lambda:GetFunctionConfiguration", "Resource": "arn:aws:lambda:ap-southeast-2:904108119046:function:RDSInstanceStop" } ] }

2 Answers 2

2

I had to re-write the lambda function with Runtime Python 3.7:

import botocore
import boto3

rdsId = 'data-cluster-d9xka2hfg766'

def stopRDS():
    rds = boto3.client('rds')
    instances = rds.describe_db_clusters( DBClusterIdentifier=rdsId)

    status = instances.get('DBClusters')[0].get('Status')

    if status == 'available':    
        resp = rds.stop_db_cluster(DBClusterIdentifier=rdsId)
        print('Requested to stop rds: ' + str(rdsId))  
    else:
        print('RDS ' + str(rdsId) + ' is ' + str(status))

def lambda_handler(event, context):
    stopRDS()
    return 'Stopped environment.'
Sign up to request clarification or add additional context in comments.

1 Comment

This is probably the correct answer and should be marked as answer.
0

This is a known issue

From https://github.com/boto/boto3/issues/1723

Looks like these operations were recently added and the lambda runtime may not have the latest version of boto3, meaning the operation isn't available. You'll need to bundle a newer version of the SDK with your lambda package. Here's some docs on doing that: https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html

4 Comments

Thanks for your update. But i researched further and found that i was using python 2.7 and when i changed to python 3.7 it worked. But now i am getting the below error, can you please help me here;
An error occurred (AccessDenied) when calling the StopDBCluster operation: User: arn:aws:sts::904108119046:assumed-role/lambda-start-stop-rds/RDSInstanceStop is not authorized to perform: rds:StopDBCluster on resource: arn:aws:rds:ap-southeast-2:904108119046:cluster:dbinstancename END RequestId: 38b48bd1-f818-11e8-81fb-4b70f65dc16d
This is my role details;
Need to revise the role policy to include StopDBCluster in the allowed section

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.