# Buggy code
def dispatch_event(event, context):
    error = event.get("detail", {}).get("errorCode")
    if error and C7N_SKIP_EVTERR:
        log.debug("Skipping failed operation: %s" % error)
        return

    if C7N_DEBUG_EVENT:
        event["debug"] = True
        log.info("Processing event\n %s", format_event(event))

    # Policies file should always be valid in lambda so do loading naively
    global policy_config
    if policy_config is None:
        with open("config.json") as f:
            policy_config = json.load(f)

    if not policy_config or not policy_config.get("policies"):
        return False

    options = init_config(policy_config)

    policies = PolicyCollection.from_data(policy_config, options)
    if policies:
        for p in policies:
            try:
                p.push(event, context)
            except Exception:
                log.exception("error during policy execution")
                if C7N_CATCH_ERR:
                    continue
                raise
    return True


# Traceback (AttributeError)
START RequestId: bd7b329c-d677-11e8-8925-01840da49e11 Version: $LATEST
[INFO]	2018-10-23T03:57:20.426Z	bd7b329c-d677-11e8-8925-01840da49e11	Processing event
{
"account": "[sanitized]",
"region": "us-east-1",
"detail": {},
"detail-type": "Scheduled Event",
"source": "aws.events",
"version": "0",
"time": "2018-10-23T03:56:45Z",
"debug": true,
"id": "a83eb09d-de5e-2155-62ef-24490c335d43",
"resources": [
"arn:aws:events:us-east-1:8[sanitized]:rule/custodian-enable-vpc-flow-logs"
]
}

[DEBUG]	2018-10-23T03:57:20.427Z	bd7b329c-d677-11e8-8925-01840da49e11	Storing output with <CloudWatchLogOutput to group:/aws/enable-vpc-flow-logs/resources stream:enable-vpc-flow-logs>
[DEBUG]	2018-10-23T03:57:20.899Z	bd7b329c-d677-11e8-8925-01840da49e11	Running policy enable-vpc-flow-logs resource: vpc region:us-east-1 c7n:0.8.31.2
[DEBUG]	2018-10-23T03:57:21.312Z	bd7b329c-d677-11e8-8925-01840da49e11	Filtered from 1 to 1 vpc
[INFO]	2018-10-23T03:57:21.312Z	bd7b329c-d677-11e8-8925-01840da49e11	policy: enable-vpc-flow-logs resource:vpc region:us-east-1 count:1 time:0.25
[DEBUG]	2018-10-23T03:57:21.321Z	bd7b329c-d677-11e8-8925-01840da49e11	metric:ResourceCount Count:1 policy:enable-vpc-flow-logs restype:vpc scope:policy
[DEBUG]	2018-10-23T03:57:21.321Z	bd7b329c-d677-11e8-8925-01840da49e11	metric:PolicyException Count:1 policy:enable-vpc-flow-logs restype:vpc
[DEBUG]	2018-10-23T03:57:21.321Z	bd7b329c-d677-11e8-8925-01840da49e11	metric:ApiCalls Count:0 policy:enable-vpc-flow-logs restype:vpc
[ERROR]	2018-10-23T03:57:21.522Z	bd7b329c-d677-11e8-8925-01840da49e11	Error while executing policy
Traceback (most recent call last):
File "/var/task/c7n/policy.py", line 273, in run
results = a.process(resources)
File "/var/task/c7n/resources/vpc.py", line 1810, in process
if not self.state:
AttributeError: 'CreateFlowLogs' object has no attribute 'state'
'CreateFlowLogs' object has no attribute 'state': AttributeError
Traceback (most recent call last):
File "/var/task/custodian_policy.py", line 4, in run
return handler.dispatch_event(event, context)
File "/var/task/c7n/handler.py", line 91, in dispatch_event
p.push(event, context)
File "/var/task/c7n/policy.py", line 834, in push
return mode.run(event, lambda_ctx)
File "/var/task/c7n/policy.py", line 516, in run
return PullMode.run(self)
File "/var/task/c7n/policy.py", line 273, in run
results = a.process(resources)
File "/var/task/c7n/resources/vpc.py", line 1810, in process
if not self.state:
AttributeError: 'CreateFlowLogs' object has no attribute 'state'

END RequestId: bd7b329c-d677-11e8-8925-01840da49e11

# Fixed code
def dispatch_event(event, context):
    error = event.get("detail", {}).get("errorCode")
    if error and C7N_SKIP_EVTERR:
        log.debug("Skipping failed operation: %s" % error)
        return

    if C7N_DEBUG_EVENT:
        event["debug"] = True
        log.info("Processing event\n %s", format_event(event))

    # Policies file should always be valid in lambda so do loading naively
    global policy_config
    if policy_config is None:
        with open("config.json") as f:
            policy_config = json.load(f)

    if not policy_config or not policy_config.get("policies"):
        return False

    options = init_config(policy_config)

    policies = PolicyCollection.from_data(policy_config, options)
    if policies:
        for p in policies:
            try:
                # validation provides for an initialization point for
                # some filters/actions.
                p.validate()
                p.push(event, context)
            except Exception:
                log.exception("error during policy execution")
                if C7N_CATCH_ERR:
                    continue
                raise
    return True