使用python 2.7使用lambda函数停止AWS aurora数据库

时间:2018-11-30 05:55:47

标签: lambda aws-aurora

我使用下面的lambda函数停止了我的rds aurora数据库。但是它总是以错误“ RDS”对象没有属性'stop_db_cluster'”结尾。有人可以帮我吗?

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"
    }

我正在使用角色-lambda-start-stop-rds我的政策详细信息- {     “ Version”:“ 2012-10-17”,     “声明”:[         {             “ Sid”:“ VisualEditor0”,             “效果”:“允许”,             “动作”:[                 “ 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”             ],             “资源”:“ *”         }     ]

}

{     “ Version”:“ 2012-10-17”,     “声明”:[         {             “效果”:“允许”,             “ Action”:“ lambda:GetFunctionConfiguration”,             “资源”:“ arn:aws:lambda:ap-southeast-2:904108119046:function:RDSInstanceStop”         }     ] }

2 个答案:

答案 0 :(得分:0)

这是一个已知问题

来自https://github.com/boto/boto3/issues/1723

  

最近添加了这些操作,并且运行了lambda   可能没有最新版本的boto3,这意味着操作不   可用。您需要将更新版本的SDK与   lambda包。这是一些这样做的文档:   https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html

答案 1 :(得分:0)

我不得不使用运行时Python 3.7重新编写lambda函数:

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.'