简体   繁体   English

由于 DB 状态,Python 脚本不会关闭或打开电源

[英]Python script wont power off or on due to DB status

I have created an API in AWS Gateway which uses AWS Lambda.我在使用 AWS Lambda 的 AWS Gateway 中创建了一个 API。 The Lambda uses a python script. Lambda 使用 python 脚本。 I can use the API gateway to invoke the Lambda which can power on or off RDS clusters.我可以使用 API 网关来调用 Lambda 可以打开或关闭 RDS 集群。 In my AWS account I have 4 RDS clusters.在我的 AWS 账户中,我有 4 个 RDS 集群。 If all 4 are powered off I can use the API to power them all on at once If all 4 are powered on I can use the API to power them all off at once如果所有 4 个都关闭,我可以使用 API 一次将它们全部打开如果所有 4 个都打开,我可以使用 API 一次将它们全部关闭

However the issue Im having is that if one of the clusters is powered on but the other 3 arent, my script wont power the other 3 on as it tells me that one cluster is not in the correct state (its already powered on)但是我遇到的问题是,如果其中一个集群已打开电源但其他 3 个未启动,我的脚本将不会启动其他 3 个,因为它告诉我一个集群不在正确的 state 中(它已经打开)

The same if one cluster is already powered off and I want to power off everything else, my script again fails as one of the clusters is already powered off如果一个集群已经关闭并且我想关闭其他所有东西,我的脚本再次失败,因为其中一个集群已经关闭

I get this error我收到这个错误
[ERROR] InvalidDBClusterStateFault: An error occurred (InvalidDBClusterStateFault) when calling the StartDBCluster operation: DbCluster cluster name is not in stopped [错误] InvalidDBClusterStateFault:调用 StartDBCluster 操作时发生错误(InvalidDBClusterStateFault):DbCluster集群名称未停止

Here is my script that can power off or on if everything is either off or on.这是我的脚本,如果一切都关闭或打开,它可以关闭或打开。 Can anyone tell me what I should add that could resolve my issue?谁能告诉我应该添加什么可以解决我的问题? Basically I'm trying to add to my script that if the status is available the script will shutdown the cluster and if stopped it starts it.基本上我试图添加到我的脚本中,如果状态可用,脚本将关闭集群,如果停止,它将启动它。 'Method' is the parameter I use for my api. “方法”是我用于 api 的参数。 Basically the api has an invoke URL which I can edit to have Method=StopAll or Method=StartAll基本上 api 有一个调用 URL 我可以编辑它有 Method=StopAll 或 Method=StartAll


           def handler(event, context):
              response = rds_client.describe_db_clusters(
                  MaxRecords=100
              )
              allClusters = [] 
              allClusters.append(response['DBClusters'])
              
              while 'Marker' in response:
                  old_marker = response['Marker']
                  response = rds_client.describe_db_clusters(
                      MaxRecords=100,
                      Marker = old_marker
                  )
                  allClusters.append(response['DBClusters'])
    if event['Method'] == "StopAll":
                  for cluster in allClusters:
                      for key in cluster:
                          rds_client.stop_db_cluster(
                                      DBClusterIdentifier=key['DBClusterIdentifier']
                                  )
                                  
              if event['Method'] == "StartAll":
                  for cluster in allClusters:
                      for key in cluster:
                          rds_client.start_db_cluster(
                                      DBClusterIdentifier=key['DBClusterIdentifier']
                                  )```

Whenever you call start_db_cluster or stop_db_cluster you can wrap in a try/except so that, even if one call fails, it will still attempt to make the rest:每当您调用start_db_clusterstop_db_cluster时,您都可以包装在 try/except 中,这样即使一个调用失败,它仍会尝试创建 rest:

try:
    rds_client.start_db_cluster(DBClusterIdentifier=cluster_id)
except rds_client.exceptions.InvalidDBClusterStateFault:
    print(f"Cluster {cluster_id} in invalid state")
except Exception as e:
    print(f"Error updating cluster {cluster_id}: {repr(e)}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM