我希望使用lambda函数停止多个RDS实例。我使用的代码是:
import sys
import botocore
import boto3
from botocore.exceptions import ClientError
def lambda_handler(event, context):
rds = boto3.client('rds')
lambdaFunc = boto3.client('lambda')
print('Trying to get Environment variable')
try:
funcResponse = lambdaFunc.get_function_configuration(
FunctionName='lambdaStopRDS'
)
DBinstance = funcResponse['Environment']['Variables']['DBInstanceName']
print('Stopping RDS service for DBInstance : ' + DBinstance)
except ClientError as e:
print(e)
try:
response = rds.stop_db_instance(
DBInstanceIdentifier=DBinstance
)
print('Success :: ')
return response
except ClientError as e:
print(e)
return
{
'message' : "Script execution completed. See Cloudwatch logs for complete output"
}我将添加以下环境变量:
Key:DBInstanceName Value:数据库-1,数据库-2
并得到以下错误:
Trying to get Environment variable
Stopping RDS service for DBInstance : database-1, database-2
An error occurred (InvalidParameterValue) when calling the StopDBInstance operation: Invalid database identifier: database-1, database-2在这里,键必须是唯一的,所以我不能添加另一个具有相同名称的密钥,并添加另一个RDS。有没有任何方法可以在没有标记的情况下停止同一个VPC/区域内的多个RDS实例?
发布于 2021-04-18 23:59:09
实例只接受一个db id,而不是多个id。但是,您正在尝试传递其中的两个database-1, database-2。因此,您必须在循环中这样做。例如:
try:
db_ids = [v.strip() for v in DBinstance.split(',')]
for db_id in db_ids:
response = rds.stop_db_instance(
DBInstanceIdentifier=db_id
)
print('Success :: ')
return response
except ClientError as e:
print(e)https://stackoverflow.com/questions/67154487
复制相似问题