in src/actions.py [0:0]
def check_alarm_tag(instance_id, tag_key):
try:
ec2_client = boto3_client('ec2')
# does instance have appropriate alarm tag?
instance = ec2_client.describe_instances(
Filters=[
{
'Name': 'tag-key',
'Values': [
tag_key
]
}
],
InstanceIds=[
instance_id
]
)
# can only be one instance when called by CloudWatch Events
if 'Reservations' in instance and len(instance['Reservations']) > 0 and len(
instance['Reservations'][0]['Instances']) > 0:
ec2_client.create_tags(
Resources=[
instance_id
],
Tags=[
{
'Key': tag_key,
'Value': str(datetime.utcnow())
}
]
)
return instance['Reservations'][0]['Instances'][0]
else:
return False
except Exception as e:
# If any other exceptions which we didn't expect are raised
# then fail and log the exception message.
logger.error('Failure describing instance {} with tag key: {} : {}'.format(instance_id, tag_key, e))
raise