【问题标题】:Stop AWS aurora database using lambda function using python 2.7使用 python 2.7 使用 lambda 函数停止 AWS 极光数据库
【发布时间】:2019-05-07 09:05:54
【问题描述】:

我使用下面的 lambda 函数来停止我的 rds aurora 数据库。但它总是以错误“RDS' object has no attribute '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 我的政策详细信息 - { “版本”:“2012-10-17”, “陈述”: [ { "Sid": "可视化编辑器0", “效果”:“允许”, “行动”: [ "rds:ResetDBParameterGroup", "rds:DescribeEngineDefaultParameters", "rds:CreateOptionGroup", "rds:CreateDBSubnetGroup", "rds:PurchaseReservedDBInstancesOffering", "日志: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", “日志:创建日志组”, "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", “日志: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” ], “资源”:“*” } ]

}

{ “版本”:“2012-10-17”, “陈述”: [ { “效果”:“允许”, "动作": "lambda:GetFunctionConfiguration", “资源”:“arn:aws:lambda:ap-southeast-2:904108119046:function:RDSInstanceStop” } ] }

【问题讨论】:

    标签: lambda amazon-aurora


    【解决方案1】:

    我不得不用 Runtime 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.'
    

    【讨论】:

    • 这可能是正确答案,应该标记为答案。
    【解决方案2】:

    这是一个已知问题

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

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

    【讨论】:

    • 感谢您的更新。但我进一步研究发现我使用的是 python 2.7,当我更改为 python 3.7 时它起作用了。但现在我收到以下错误,你能帮我吗?
    • 调用 StopDBCluster 操作时发生错误 (AccessDenied):用户:arn:aws:sts::904108119046:assumed-role/lambda-start-stop-rds/RDSInstanceStop 无权执行: rds:StopDBCluster 资源:arn:aws:rds:ap-southeast-2:904108119046:cluster:dbinstancename END RequestId: 38b48bd1-f818-11e8-81fb-4b70f65dc16d
    • 这是我的角色详情;
    • 需要修改角色策略以将 StopDBCluster 包含在允许的部分中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 2021-09-19
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    相关资源
    最近更新 更多