[英]Result returned by AWS Lambda python function execution in “”Null“”
[英]AWS Lambda Python 3.6 Null Value Unexpected Result (!= None:)
我有一个 Lambda Python 3.6 function 检查 EC2 实例上是否存在特定标签和值。 标签是“expenddate”,该值要么是空白,要么是日期格式为 mm/dd/yy 格式。 我的目标是让 function 检查标签是否存在,然后在满足两个条件时进行处理,1)如果日期小于或等于当前日期,2)值不为空(无)。 这会根据日期正确处理,但当值是我不想要的空白时仍会报告。
这是我的代码的相关部分,特别是行'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"
更改您的 if 条件以检查
if (tag['Value']),= None 首先然后。 添加 if (tag['Value']) <= mdy.
参考短路评估
编辑: tag['Value'] 的返回类型是一个字符串,所以使用 None 比较不好,因为,
None 是 Python 中的特殊值,可用于表示变量没有可用的值 - 它没有有效长度,不能用于计算等。
Null 或空字符串表示存在字符串但其内容为空,即。 len('')==0 对于 Python,首选术语“空字符串”。
因此,您的条件将是: (tag['Value']) != ''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.