Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
956 views
in Technique[技术] by (71.8m points)

python 3.x - How to Filter rds Instances by Tag Value that is Date Validator with Lambda Function with Boto3

My Python knowledge is not good --> I am trying to stop rds instances that have the tag Key=ttl and Value = older than today (<today's date). My code does show to have run successfully but the results are not as wanted. I would like to get printed stopInstances or No rds instances to shutdown.. Does anyone know how to fix this? Log: enter image description here

Code:

import boto3
import time
from datetime import datetime
# Example RDS Instance tags: 

#define boto3 the connection
rds = boto3.client('rds')

def lambda_handler(event, context):
    
    print ("Check RDS's tags")
    
     # Get current time in format yyyy-mm-dd
current_time = datetime.today().strftime('%Y-%m-%d')

     # Search all the instances which contains ttl filter 
instances = rds.describe_db_instances()

stopInstances = []   
# startInstances = []   

     # Locate all instances that are tagged ttl.
for instance in instances["DBInstances"]:
        
         tags = rds.list_tags_for_resource(ResourceName=instance["DBInstanceArn"])
            
         for tag in tags["TagList"]:

             if tag['Key'] == 'ttl':

                 if tag['Value'] < current_time:

                     stopInstances.append(instance["DBInstanceIdentifier"])
                     rds.stop_db_instance(DBInstanceIdentifier=instance["DBInstanceIdentifier"])    
                    
                     pass

                 pass


     # shut down all instances tagged to stop. 
if len(stopInstances) > 0:
         # perform the shutdown
      print ("stopInstances")
else:
      print ("No rds instances to shutdown.")
question from:https://stackoverflow.com/questions/65885282/how-to-filter-rds-instances-by-tag-value-that-is-date-validator-with-lambda-func

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I just had to change my tags, and the sign of the validator (if tag['Value'] < current_time:). Once this is fixed, the code is working fine.

import boto3
import time
from datetime import datetime
# Example RDS Instance tags: 

#define boto3 the connection
rds = boto3.client('rds')

def lambda_handler(event, context):
    
    print ("Check RDS's tags")
    
     # Get current time in format yyyy-mm-dd
current_time = datetime.today().strftime('%Y-%m-%d')

     # Search all the instances which contains ttl filter 
instances = rds.describe_db_instances()

stopInstances = []   
# startInstances = []   

     # Locate all instances that are tagged ttl.
for instance in instances["DBInstances"]:
        
         tags = rds.list_tags_for_resource(ResourceName=instance["DBInstanceArn"])
            
         for tag in tags["TagList"]:

             if tag['Key'] == 'ttl':

                 if tag['Value'] < current_time:

                     stopInstances.append(instance["DBInstanceIdentifier"])
                     rds.stop_db_instance(DBInstanceIdentifier=instance["DBInstanceIdentifier"])    
                    
                     pass

                 pass


     # shut down all instances tagged to stop. 
if len(stopInstances) > 0:
         # perform the shutdown
      print ("stopInstances")
else:
      print ("No rds instances to shutdown.")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...