简体   繁体   中英

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: 在此处输入图像描述

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.")

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.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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