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.")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…