【问题标题】:Stopping multiple AWS RDS instances using lambda function使用 lambda 函数停止多个 AWS RDS 实例
【发布时间】:2021-07-13 04:58:12
【问题描述】:

我想使用 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"
   }

我正在添加以下环境变量:

:DBInstanceName :database-1,database-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

这里,Keys 必须是唯一的,所以我不能添加另一个同名的 key 并添加另一个 RDS。 有什么方法可以在没有标签的情况下停止同一 VPC/区域内的多个 RDS 实例?

【问题讨论】:

    标签: amazon-web-services aws-lambda boto3 amazon-rds


    【解决方案1】:

    stop_db_instance只需要一个 db 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)
    

    【讨论】:

      猜你喜欢
      • 2017-11-28
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 2015-08-26
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多