默认情况下,如果您的Lambda函数出错,它将自动重试另外2次。我正在处理Lambda函数中的某些错误,这些错误不是真正的错误,基本上是我要搜索的json不在返回的json中,我记录找不到该json,我希望脚本停止在那一点上,因为他们已无事可做。但是,当我尝试捕获异常时,它会正确记录并退出,但由于Lambda将其报告为错误,因此再次运行它。
是我使用sys.exit()引起的吗?在第一个示例中,我不会尝试/捕获错误,但会重试。
示例:循环遍历json节点,如果找到匹配项,然后分配变量,如果它不处理异常,并向cloudwatch(打印)写出找不到匹配项。
while index < result_nodes:
if TL2['message_response']['computers'][index]['resource_name'] == instance_name:
resource_id = TL2['message_response']['computers'][index]['resource_id']
index += 1
index += 1
if resource_id == '':
print('No match found, quitting.')
sys.exit()
另一类似用法
instance_name = ''
try:
for tag in response['Reservations'][0]['Instances'][0]['Tags']:
if tag['Key'] == 'Name':
instance_name = tag['Value']
if instance_name == '':
print('EC2 Instance name not set, quitting')
sys.exit()
else:
print('Step 2 - Get Computer Name: ' + instance_name)
except Exception:
print('No tags exist for this terminated EC2 instance, quitting.')
sys.exit()
尝试删除sys.exit()
,它不再重试。相似情况https://stackoverflow.com/a/52739848/12103989