AWS Lambda函数从cloudwatch启动两次,仅成功运行一次,为什么未记录错误

时间:2019-01-01 22:11:18

标签: python amazon-web-services aws-lambda amazon-cloudwatch

我正在使用cloudwatch事件在新的ECS实例启动时触发Labmda。

我对日志有点迷惑,看起来该函数先被调用,然后静默失败,然后再次调用,并在相同事件输入下成功运行。

我了解可以为同一事件从cloudwatch多次调用lambda函数,因为交付合同至少一次,并且我打算对该函数进行更改,使其具有幂等性。

我的问题是,第一次调用并停止运行时会发生什么?

基于Jarmod问题的更多信息

第一次调用该函数时,只有以下标记可用:

'Tags': [{'Key': 'aws:ec2launchtemplate:id', 'ResourceId': 'i-0a0500abc17bc7e9e', 'ResourceType': 'instance', 'Value': 'lt-01e5585a011e48b97'}]

这意味着我的代码在以下行中失败:

if 'torres' in ec2_response['Tags'][0]['Value']

由于我将过滤器设置为仅获取Name标记,因此这些标记在第一时间变空了。我假设lambda正在根据Lambda re-run documentation重新运行,并且第二次被称为Name标签,并且一切都很好。

所以我的问题变成了,为什么我的lambda日志中没有出现错误?

以下是日志输出:

2019-01-01 11:34:46
START RequestId: 63190c9b-0dd2-11e9-b6ed-256f780af4a8 Version: $LATEST
Event: {'version': '0', 'id': '12f0dc8e-2c49-be57-823f-ab6229b12804', 
'detail-type': 'EC2 Instance State-change Notification', 'source': 
'aws.ec2', 'account': '701704546303', 'time': '2019-01-01T14:34:46Z', 
'region': 'us-west-2', 'resources': ['arn:aws:ec2:us-west- 
2:701704546303:instance/i-0a4af714ff4396d55'], 'detail': {'instance-id': 'i-0a4af714ff4396d55', 'state': 'running'}}
, Context: <bootstrap.LambdaContext object at 0x7fc8f40525f8>
Making ec2 name request
END RequestId: 63190c9b-0dd2-11e9-b6ed-256f780af4a8
REPORT RequestId: 63190c9b-0dd2-11e9-b6ed-256f780af4a8 Duration: 366.46 ms  Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 39 MB  

2019-01-01 11:35:46
START RequestId: 63190c9b-0dd2-11e9-b6ed-256f780af4a8 Version: $LATEST
Event: {'version': '0', 'id': '12f0dc8e-2c49-be57-823f-ab6229b12804',                     
'detail-type': 'EC2 Instance State-change Notification', 'source': 
'aws.ec2', 'account': '701704546303', 'time': '2019-01-01T14:34:46Z', 
'region': 'us-west-2', 'resources': ['arn:aws:ec2:us-west- 
2:701704546303:instance/i-0a4af714ff4396d55'], 'detail': {'instance- 
id': 'i-0a4af714ff4396d55', 'state': 'running'}}
, Context: <bootstrap.LambdaContext object at 0x7fc8f724ca58>
Making ec2 name request
torres instance, starting ecs task
END RequestId: 63190c9b-0dd2-11e9-b6ed-256f780af4a8
REPORT RequestId: 63190c9b-0dd2-11e9-b6ed-256f780af4a8  Duration: 1022.97 ms    Billed Duration: 1100 ms Memory Size: 128 MB    Max Memory Used: 40 MB  

功能如下:

def lambda_handler(event, context):
    print('Event: {}\n, Context: {}'.format(event, context))
    if event['detail-type'] != 'EC2 Instance State-change Notification':
        print('Wrong event detail-type')
        ret = {
            'status': 'Wrong event detail-type'
        }
        print(event)
    else:
        print('Making ec2 name request')
        ec2_client = boto3.client('ec2', region_name='us-west-2')
        try:
            ec2_response = ec2_client.describe_tags(
                Filters=[
                    {
                        'Name': 'resource-id',
                        'Values': [
                            event['detail']['instance-id']
                        ]
                    },
                    {
                        'Name': 'key',
                        'Values': [
                            'Name'
                        ]
                    }
                ]
            )
        except Exception as e:
            print('Failed ec2 call')
            print(e)
            raise e
        if 'torres' in ec2_response['Tags'][0]['Value']:
            print('torres instance, starting ecs task')
            ecs_client = boto3.client('ecs', region_name='us-west-2')
            try:
                ecs_response = ecs_client.run_task(
                    cluster='torres',
                    taskDefinition='torres:16'
                )
            except Exception as e:
                print('Failed ecs call')
                print(e)
                raise e
            task_definition_arns = [t['taskDefinitionArn'] for t in ecs_response['tasks']]
            ecs_status = ecs_response['ResponseMetadata']['HTTPStatusCode']

            ret = {
                'task_definition_arns': task_definition_arns,
                'ecs_status': ecs_status
            }
        else:
            print('Wrong instance')
            ret = {
                'status': 'Wrong instance',
                'event': event
            }

    return ret

0 个答案:

没有答案