lambda - 使用python 2.7使用lambda函数停止AWS极光数据库
问题描述
我使用下面的 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”:“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”]、“资源”:“*”}]
}
{“版本”:“2012-10-17”,“声明”:[{“效果”:“允许”,“操作”:“lambda:GetFunctionConfiguration”,“资源”:“arn:aws:lambda:ap-东南 2:904108119046:function:RDSInstanceStop" } ] }
解决方案
我不得不用 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.'
推荐阅读
- augmented-reality - 查看放置的 PNG 时 USDZ 文件的闪烁/碎片视觉效果
- nginx - Nginx:本地站点加载一段时间,然后以“找不到服务器”中止
- html - 如何在 Material Paginator 中更改 pageSizeOptions 的内容
- javascript - find the length of array using map function ES6
- java - 关闭登录maven
- html - 缩小时使文本周围的区域变大
- sql-server - Change collation of a computed column
- nginx - 同一主机上的 NGINX 多个站点
- python - 在 Databricks python notebook 中,如何导入 file1 对象与 file2 位于不同的目录中?
- html - stroke url not working in Edge and IE 11 SVG