使用Python2.7使用lambda函数停止AWS aurora数据库

使用Python2.7使用lambda函数停止AWS aurora数据库,lambda,amazon-aurora,Lambda,Amazon Aurora,我使用下面的lambda函数来停止我的rds aurora数据库。但它总是以错误“RDS”对象没有属性“stop\u db\u cluster”结束。有人能帮我吗 import sys import botocore import boto3 from botocore.exceptions import ClientError def lambda_handler(event, context): client = boto3.client('rds') lambdaFunc

我使用下面的lambda函数来停止我的rds aurora数据库。但它总是以错误“RDS”对象没有属性“stop\u db\u 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 my policy details- { “版本”:“2012-10-17”, “声明”:[ { “Sid”:“VisualEditor0”, “效果”:“允许”, “行动”:[ “rds:ResetDBParameterGroup”, “rds:DescribeeneDefaultParameters”, “rds:CreateOptionGroup”, “rds:CreateDBSubnetGroup”, “rds:PurchaseReservedBinsTancesOffing”, “日志:CreateLogStream”, “rds:ModifyDBParameterGroup”, “rds:AddSourceIdentifierToSubscription”, “rds:下载DblogFilePart”, “rds:CopyDBParameterGroup”, “rds:AddRoleToDBCluster”, “rds:ModifyDBInstance”, “rds:ModifyDBClusterParameterGroup”, “rds:ModifyDBClusterSnapshotAttribute”, “rds:DeleteDBInstance”, “rds:CreateDBParameterGroup”, “rds:DescribeDBSnapshots”, “rds:DeleteDBSnapshot”, “rds:DescribedBSecurityGroup”, “日志:CreateLogGroup”, “rds:PromoteredReplica”, “rds:StartDBInstance”, “rds:DeleteDBSubnetGroup”, “rds:DescribeServedBinstances”, “rds:CreateDBSnapshot”, “rds:DescribeValidBinstanceModifications”, “rds:RestoreDBInstanceFromDBSnapshot”, “rds:DeleteDBSecurityGroup”, “rds:DescribeOrderableDBInstanceOptions”, “rds:ModifyDBCluster”, “rds:CreateDBClusterSnapshot”, “rds:DeleteDBParameterGroup”, “rds:描述证书”, “rds:CreateDBClusterParameterGroup”, “rds:ModifyDBSnapshotAttribute”, “rds:RemoveTagsFromResource”, “rds:DescribeOptionGroup”, “rds:AuthorizeDBSecurityGroupIngress”, “rds:CreateEventSubscription”, “rds:ModifyOptionGroup”, “rds:RestoreDBClusterFromSnapshot”, “rds:DescribeDBEngineVersions”, “rds:DescribeDBSubnetGroups”, “rds:描述挂起的维护操作”, “rds:DescribedBPParameterGroup”, “rds:DescribeServedBinsTancesOffices”, “rds:DeleteOptionGroup”, “rds:FailoverDBCluster”, “rds:DeleteEventSubscription”, “rds:RemoveSourceIdentifier fromSubscription”, “rds:CreatedDBInstance”, “rds:DescribeBindInstances”, “rds:DescribeeneDefaultClusterParameters”, “rds:RevokeDBSecurityGroupIngress”, “rds:DescribedBPParameters”, “rds:DescribeEventCategories”, “rds:ModifyCurrentDBClusterCapacity”, “rds:DeleteDBCluster”, “rds:ResetDBClusterParameterGroup”, “rds:RestoredBClusterTopointime”, “rds:描述事件”, “rds:AddTagsToResource”, “rds:DescribeDBClusterSnapshotAttributes”, “rds:DescribeDBClusterParameters”, “rds:DescribeEventSubscriptions”, “rds:CopyDBSnapshot”, “rds:CopyDBClusterSnapshot”, “rds:ModifyEventSubscription”, “rds:DescribeDBLogFiles”, “rds:StopDBInstance”, “日志:PutLogEvents”, “rds:CopyOptionGroup”, “rds:DescribeDBSnapshotAttributes”, “rds:DeleteDBClusterSnapshot”, “rds:ListTagsForResource”, “rds:CreateDBCluster”, “rds:CreateDBSecurityGroup”, “rds:RebootDBInstance”, “rds:DescribeDBClusterSnapshots”, “rds:DescribeOptionGroupOptions”, “rds:下载CompletedBlogFile”, “rds:DeleteDBClusterParameterGroup”, “rds:ApplyPendingMaintenanceAction”, “rds:CreatedDBInstanceReplica”, “rds:DescribeAccountAttribute”, “rds:DescribeDBClusters”, “rds:DescribedBClusterParameterGroup”, “rds:ModifyDBSubnetGroup”, “rds:RestoredBinstanceTopointime” ], “资源”:“*” } ]

} { “版本”:“2012-10-17”, “声明”:[ { “效果”:“允许”, “操作”:“lambda:GetFunctionConfiguration”, “资源”:“arn:aws:lambda:ap-southeast-2:904108119046:功能:RDSInstanceStop” } ] }这是一个已知的问题

看起来这些操作是最近添加的,lambda运行时 可能没有最新版本的boto3,这意味着该操作不可用 可用。您需要将SDK的较新版本与 lambda包。下面是一些关于这样做的文档:

这是一个众所周知的问题

看起来这些操作是最近添加的,lambda运行时 可能没有最新版本的boto3,这意味着该操作不可用 可用。您需要将SDK的较新版本与 lambda包。下面是一些关于这样做的文档:


我必须使用运行时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.'

我必须使用运行时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.'

谢谢你的更新。但我进一步研究发现,我使用的是Python2.7,当我改为Python3.7时,它起了作用。但是现在我得到下面的错误,你能在这里帮助我吗;错误