Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AWS Lambda函数从cloudwatch启动两次,仅成功运行一次,为什么不记录错误_Python_Amazon Web Services_Aws Lambda_Amazon Cloudwatch - Fatal编程技术网

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

Python AWS Lambda函数从cloudwatch启动两次,仅成功运行一次,为什么不记录错误,python,amazon-web-services,aws-lambda,amazon-cloudwatch,Python,Amazon Web Services,Aws Lambda,Amazon Cloudwatch,我使用cloudwatch事件在新ecs实例启动时触发labmda 日志让我有点迷惑,它看起来像是调用了函数,然后无声地失败,然后再次调用,并使用相同的事件输入成功运行 我知道,对于同一事件,可以从cloudwatch多次调用lambda函数,因为交付契约至少有一次,我计划对函数进行更改,使其成为幂等函数 我的问题是,当它第一次被调用并停止运行时会发生什么 基于Jarmod问题的更多信息 第一次调用函数时,只有以下标记可用: 'Tags': [{'Key': 'aws:ec2launchtemp

我使用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']
因为当我将过滤器设置为只获取名称标签时,标签第一次返回时是空的。我假设lambda是基于重新运行的,第二次调用它时,名称标签就在那里,一切都很好

所以我的问题是,为什么我的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  

这就是完整的lambda函数吗?我找不到
进行asg生命周期钩子调用
输出…-你能确定Lambda超时不是问题吗?这通常是我在可能的冷启动情况下出现故障时的第一个冲动;-)在第一种情况下,ec2_响应包含什么?它确实有任何带有标记的有效内容吗?@Maurice-这是它的早期版本,我从日志中删除了它,它不会影响问题。@jarmod-你搞定了,标记不在那里。我不知道为什么这个异常没有进入日志。我想这是一个更好的问题:)@sshevlyagin这显然是一个-切换回3.6,你应该会看到异常。
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