I would like to stop multiple RDS instances using the lambda function. The code I am using is:
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"
}
I am adding the following environment variables:
Key: DBInstanceName Value: database-1, database-2
and getting the following error:
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
Here, Keys must be unique, so I can not add another key with same name and add another RDS. Is there any way to stop multiple RDS instances within same VPC/region without tags?
stop_db_instance takes only one db id, not multiple ones. However, you are trying to pass two of them database-1, database-2. So you have to do in a loop. For example:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With