Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python elif statement not working correctly in Lambda

I very new to Python and trying to use it to shutdown or start up an RDS cluster that I use.
Basically, AWS Lambda runs the python code

I’m getting an error with the elif command. The error is:

[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'index': invalid syntax (index.py, line 22)
Traceback (most recent call last):
  File "/var/task/index.py" Line 22
        elif status == 'stopped':

Here is the Python code. Can anyone tell me what the issue is?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

client = boto3.client('rds')
          response = client.describe_db_cluster()

          for resp in response['DBCluster']:
              db_cluster_arn = resp['DBClusterArn']

          response = client.list_tags_for_resource(ResourceName=db_cluster_arn)
          for tags in response['TagList']:
              if tags['Key'] == str(Key) and tags['Value'] == str(Tag):
                  status = resp['DBClusterStatus']
                  ClusterID = resp['DBClusterIdentifier']
                  print(InstanceID)
    
              if status == 'available':
                  print("shutting down %s " % ClusterID)
              client.stop_db_cluster(DBClusterIdentifier=ClusterID)
              elif status == 'stopped':
                  print("starting up %s " % ClusterID)
              client.start_db_cluster(DBClusterIdentifier=ClusterID)
              else:
                  print("The database is " + status + " status!")

>Solution :

You need to remove or indent the lines inbetween your if elif else blocks:

client = boto3.client('rds')
response = client.describe_db_cluster()

for resp in response['DBCluster']:
    db_cluster_arn = resp['DBClusterArn']

response = client.list_tags_for_resource(ResourceName=db_cluster_arn)
for tags in response['TagList']:
    if tags['Key'] == str(Key) and tags['Value'] == str(Tag):
        status = resp['DBClusterStatus']
        ClusterID = resp['DBClusterIdentifier']
        print(InstanceID)

    if status == 'available':
        print("shutting down %s " % ClusterID)
        client.stop_db_cluster(DBClusterIdentifier=ClusterID)
    elif status == 'stopped':
        print("starting up %s " % ClusterID)
        client.start_db_cluster(DBClusterIdentifier=ClusterID)
    else:
        print("The database is " + status + " status!")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading