# Buggy code
def augment(self, resources):
    """EC2 API and AWOL Tags

    While ec2 api generally returns tags when doing describe_x on for
    various resources, it may also silently fail to do so unless a tag
    is used as a filter.

    See footnote on http://goo.gl/YozD9Q for official documentation.

    Apriori we may be using custodian to ensure tags (including
    name), so there isn't a good default to ensure that we will
    always get tags from describe_ calls.
    """

    # First if we're in event based lambda go ahead and skip this,
    # tags can't be trusted in  ec2 instances anyways.
    if not resources or self.data.get("mode", {}).get("type", "") in (
        "cloudtrail",
        "ec2-instance-state",
    ):
        return resources

    # AWOL detector, so we don't make extraneous api calls.
    resource_count = len(resources)
    search_count = min(int(resource_count % 0.05) + 1, 5)
    if search_count > resource_count:
        search_count = resource_count
    found = False
    for r in random.sample(resources, search_count):
        if "Tags" in r:
            found = True
            break

    if found:
        return resources

    # Okay go and do the tag lookup
    client = utils.local_session(self.session_factory).client("ec2")
    tag_set = client.describe_tags(
        Filters=[{"Name": "resource-type", "Values": ["instance"]}]
    )["Tags"]
    resource_tags = {}
    for t in tag_set:
        t.pop("ResourceType")
        rid = t.pop("ResourceId")
        resource_tags.setdefault(rid, []).append(t)

    m = self.query.resolve(self.resource_type)
    for r in resources:
        r["Tags"] = resource_tags.get(r[m.id], ())
    return resources


# Traceback (NotImplementedError)
2016-07-06 17:38:43,679: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/development/workspace/cloud-custodian/c7n/policy.py", line 213, in poll
results = a.process(resources)
File "/development/workspace/cloud-custodian/c7n/tags.py", line 339, in process
tags, self.id_key, f.exception()))
File "/development/workspace/cloud-custodian/c7n/tags.py", line 60, in id_key
raise NotImplementedError()
NotImplementedError

# Fixed code
def augment(self, resources):
    """EC2 API and AWOL Tags

    While ec2 api generally returns tags when doing describe_x on for
    various resources, it may also silently fail to do so unless a tag
    is used as a filter.

    See footnote on http://goo.gl/YozD9Q for official documentation.

    Apriori we may be using custodian to ensure tags (including
    name), so there isn't a good default to ensure that we will
    always get tags from describe_ calls.
    """

    # First if we're in event based lambda go ahead and skip this,
    # tags can't be trusted in  ec2 instances anyways.
    if not resources or self.data.get("mode", {}).get("type", "") in (
        "cloudtrail",
        "ec2-instance-state",
    ):
        return resources

    # AWOL detector, so we don't make extraneous api calls.
    resource_count = len(resources)
    search_count = min(int(resource_count % 0.05) + 1, 5)
    if search_count > resource_count:
        search_count = resource_count
    found = False
    for r in random.sample(resources, search_count):
        if "Tags" in r:
            found = True
            break

    if found:
        return resources

    # Okay go and do the tag lookup
    client = utils.local_session(self.session_factory).client("ec2")
    tag_set = client.describe_tags(
        Filters=[{"Name": "resource-type", "Values": ["instance"]}]
    )["Tags"]
    resource_tags = {}
    for t in tag_set:
        t.pop("ResourceType")
        rid = t.pop("ResourceId")
        resource_tags.setdefault(rid, []).append(t)

    m = self.get_model()
    for r in resources:
        r["Tags"] = resource_tags.get(r[m.id], ())
    return resources