TIM02144
TIM02144

Reputation: 615

AWS Lambda Python 3.6 Null Value Unexpected Result (!= None:)

I have a Lambda Python 3.6 function that checks for the existence of a specific tag and value on EC2 instances. The tag is “expenddate” and the value will either be blank of have a date formatted in mm/dd/yy format. My goal is to have the function check for the existence of the tag and then process when two conditions are met, 1) if the date is less than or equal to the current date and 2) the value is not blank (None). This processes correctly based on the date but still reports back when the value is blank which I don’t want.

Here is relevant part of my code, specifically line ‘if (tag['Value']) <= mdy and (tag['Value']) != None:’.

        for instance in ec2.instances.all():
            if instance.tags is None:
                continue
            for tag in instance.tags:
                if tag['Key'] == 'expenddate':
                    expiredInstances=[]
                    if (tag['Value']) <= mdy and (tag['Value']) != None:
                        print('Sending publish message')
                        sns_client.publish(
                            TopicArn = 'arn:aws:sns:us-east-1:704819628235:EOTSS-Monitor-Tag-Exceptions1',
                            Subject = '!!!! Tag Exception has Expired.',
                            Message = str("The tag exception for instance %s has expired in account %s" % (instance.id,acctnum)))
                else:
                    print ("end")
    return "sucess"

Upvotes: 0

Views: 598

Answers (1)

Bhushan Wawre
Bhushan Wawre

Reputation: 21

change your if condition to check

if (tag['Value']) != None first and then, add if (tag['Value']) <= mdy.

Reference Short-circuit evaluation

edit: The return type for tag['Value'] is a string, so using None to compare is not good since,

None is a special value in Python that can be used to signify that a variable does not have a value that can be usefully used - it has no valid length, can’t be used in calculations, etc.

A Null or empty string means that there is a string but its content is empty ie. len('')==0 For Python, the term ‘Empty string’ is preferred.

So, your condition will be: (tag['Value']) != ''

Upvotes: 1

Related Questions