# Buggy code
def process(self, resources, event=None):
    client = local_session(self.manager.session_factory).client(
        "support", region_name="us-east-1"
    )
    checks = client.describe_trusted_advisor_check_result(
        checkId=self.check_id, language="en"
    )["result"]

    region = self.manager.config.region
    checks["flaggedResources"] = [
        r
        for r in checks["flaggedResources"]
        if r["metadata"][0] == region
        or (r["metadata"][0] == "-" and region == "us-east-1")
    ]
    resources[0]["c7n:ServiceLimits"] = checks

    delta = timedelta(self.data.get("refresh_period", 1))
    check_date = parse_date(checks["timestamp"])
    if datetime.now(tz=tzutc()) - delta > check_date:
        client.refresh_trusted_advisor_check(checkId=self.check_id)
    threshold = self.data.get("threshold")

    services = self.data.get("services")
    limits = self.data.get("limits")
    exceeded = []

    for resource in checks["flaggedResources"]:
        if threshold is None and resource["status"] == "ok":
            continue
        limit = dict(zip(self.check_limit, resource["metadata"]))
        if services and limit["service"] not in services:
            continue
        if limits and limit["check"] not in limits:
            continue
        limit["status"] = resource["status"]
        limit["percentage"] = float(limit["extant"] or 0) / float(limit["limit"]) * 100
        if threshold and limit["percentage"] < threshold:
            continue
        exceeded.append(limit)
    if exceeded:
        resources[0]["c7n:ServiceLimitsExceeded"] = exceeded
        return resources
    return []


# Traceback (KeyError)
$ export AWS_PROFILE=...
$ custodian run --dryrun --output-dir=. custodian.yml
018-05-15 17:47:25,372: custodian.commands:ERROR Error while executing policy account-service-limits, continuing
Traceback (most recent call last):
File "/Users/9200779/git/bitbucket.org/vmdb/custodian/custodian/lib/python2.7/site-packages/c7n/commands.py", line 237, in run
policy()
File "/Users/9200779/git/bitbucket.org/vmdb/custodian/custodian/lib/python2.7/site-packages/c7n/policy.py", line 703, in __call__
resources = PullMode(self).run()
File "/Users/9200779/git/bitbucket.org/vmdb/custodian/custodian/lib/python2.7/site-packages/c7n/policy.py", line 222, in run
resources = self.policy.resource_manager.resources()
File "/Users/9200779/git/bitbucket.org/vmdb/custodian/custodian/lib/python2.7/site-packages/c7n/resources/account.py", line 66, in resources
return self.filter_resources([get_account(self.session_factory, self.config)])
File "/Users/9200779/git/bitbucket.org/vmdb/custodian/custodian/lib/python2.7/site-packages/c7n/manager.py", line 90, in filter_resources
resources = f.process(resources, event)
File "/Users/9200779/git/bitbucket.org/vmdb/custodian/custodian/lib/python2.7/site-packages/c7n/resources/account.py", line 432, in process
checks['flaggedResources'] = [r for r in checks['flaggedResources']
KeyError: u'flaggedResources'

# Fixed code
def process(self, resources, event=None):
    client = local_session(self.manager.session_factory).client(
        "support", region_name="us-east-1"
    )
    checks = self.get_check_result(client, self.check_id)

    region = self.manager.config.region
    checks["flaggedResources"] = [
        r
        for r in checks["flaggedResources"]
        if r["metadata"][0] == region
        or (r["metadata"][0] == "-" and region == "us-east-1")
    ]
    resources[0]["c7n:ServiceLimits"] = checks

    delta = timedelta(self.data.get("refresh_period", 1))
    check_date = parse_date(checks["timestamp"])
    if datetime.now(tz=tzutc()) - delta > check_date:
        client.refresh_trusted_advisor_check(checkId=self.check_id)
    threshold = self.data.get("threshold")

    services = self.data.get("services")
    limits = self.data.get("limits")
    exceeded = []

    for resource in checks["flaggedResources"]:
        if threshold is None and resource["status"] == "ok":
            continue
        limit = dict(zip(self.check_limit, resource["metadata"]))
        if services and limit["service"] not in services:
            continue
        if limits and limit["check"] not in limits:
            continue
        limit["status"] = resource["status"]
        limit["percentage"] = float(limit["extant"] or 0) / float(limit["limit"]) * 100
        if threshold and limit["percentage"] < threshold:
            continue
        exceeded.append(limit)
    if exceeded:
        resources[0]["c7n:ServiceLimitsExceeded"] = exceeded
        return resources
    return []