You can't compare dates in the query as you specified. You need to write some code comparing the dates in your python script and take action. Roughly like below:
import datetime
import boto3
# Connect to EC2
ec2 = boto3.resource('ec2')
def get_ttl(instance):
for tag in instance.tags:
if 'ttl'in tag['Key']:
ttl = tag['Value']
return ttl
def lambda_handler(event,context):
running_instances = ec2.instances.filter(Filters=[{
'Name': 'instance-state-name',
'Values': ['running']}])
for instance in running_instances:
ttl = get_ttl(instance)
if ttl:
if datetime.date.today() > datetime.datetime.strptime(ttl, "%Y-%m-%d").date():
print('stopping instance')
else:
print("nothing to do here")
Basically, it resolves around comparing dates
In [30]: datestring = '2021-02-22'
In [31]: datetime.date.today() == datetime.datetime.strptime(datestring, "%Y-%m-%d").date()
Out[31]: False
In [32]: datetime.date.today() < datetime.datetime.strptime(datestring, "%Y-%m-%d").date()
Out[32]: True
In [33]: datetime.date.today() > datetime.datetime.strptime(datestring, "%Y-%m-%d").date()
Out[33]: False
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…