# Buggy code
def get_resources(self, rids, cache=True):
    # Launch template versions have a compound primary key
    #
    # Support one of four forms of resource ids:
    #
    #  - array of launch template ids
    #  - array of tuples (launch template id, version id)
    #  - array of dicts (with LaunchTemplateId and VersionNumber)
    #  - array of dicts (with LaunchTemplateId and LatestVersionNumber)
    #
    # If an alias version is given $Latest, $Default, the alias will be
    # preserved as an annotation on the returned object 'c7n:VersionAlias'
    if not rids:
        return []

    t_versions = {}
    if isinstance(rids[0], tuple):
        for tid, tversion in rids:
            t_versions.setdefault(tid, []).append(tversion)
    elif isinstance(rids[0], dict):
        for tinfo in rids:
            t_versions.setdefault(tinfo["LaunchTemplateId"], []).append(
                tinfo.get("VersionNumber", tinfo.get("LatestVersionNumber"))
            )
    elif isinstance(rids[0], six.string_types):
        for tid in rids:
            t_versions[tid] = []

    client = utils.local_session(self.session_factory).client("ec2")

    results = []
    # We may end up fetching duplicates on $Latest and $Version
    for tid, tversions in t_versions.items():
        ltv = client.describe_launch_template_versions(
            LaunchTemplateId=tid, Versions=tversions
        ).get("LaunchTemplateVersions")
        if not tversions:
            tversions = [str(t["VersionNumber"]) for t in ltv]
        for tversion, t in zip(tversions, ltv):
            if not tversion.isdigit():
                t["c7n:VersionAlias"] = tversion
            results.append(t)
    return results


# Traceback (ClientError)
2019-08-11 07:22:23,823: custodian.commands:ERROR Error while executing policy remove-outdated-amis, continuing
Traceback (most recent call last):
File "/Users/user/.local/share/virtualenvs/cloudcustodian-ymoVWLAP/lib/python2.7/site-packages/c7n/commands.py", line 269, in run
policy()
File "/Users/user/.local/share/virtualenvs/cloudcustodian-ymoVWLAP/lib/python2.7/site-packages/c7n/policy.py", line 941, in __call__
resources = PullMode(self).run()
File "/Users/user/.local/share/virtualenvs/cloudcustodian-ymoVWLAP/lib/python2.7/site-packages/c7n/policy.py", line 229, in run
resources = self.policy.resource_manager.resources()
File "/Users/user/.local/share/virtualenvs/cloudcustodian-ymoVWLAP/lib/python2.7/site-packages/c7n/resources/ami.py", line 53, in resources
return super(AMI, self).resources(query=query)
File "/Users/user/.local/share/virtualenvs/cloudcustodian-ymoVWLAP/lib/python2.7/site-packages/c7n/query.py", line 460, in resources
resources = self.filter_resources(resources)
File "/Users/user/.local/share/virtualenvs/cloudcustodian-ymoVWLAP/lib/python2.7/site-packages/c7n/manager.py", line 108, in filter_resources
resources = f.process(resources, event)
File "/Users/user/.local/share/virtualenvs/cloudcustodian-ymoVWLAP/lib/python2.7/site-packages/c7n/resources/ami.py", line 305, in process
images = self._pull_ec2_images().union(self._pull_asg_images())
File "/Users/user/.local/share/virtualenvs/cloudcustodian-ymoVWLAP/lib/python2.7/site-packages/c7n/resources/ami.py", line 296, in _pull_asg_images
list(tmpl_mgr.get_asg_templates(asgs).keys())):
File "/Users/user/.local/share/virtualenvs/cloudcustodian-ymoVWLAP/lib/python2.7/site-packages/c7n/resources/ec2.py", line 1870, in get_resources
LaunchTemplateId=tid, Versions=tversions).get(
File "/Users/user/.local/share/virtualenvs/cloudcustodian-ymoVWLAP/lib/python2.7/site-packages/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/Users/user/.local/share/virtualenvs/cloudcustodian-ymoVWLAP/lib/python2.7/site-packages/botocore/client.py", line 661, in _make_api_call
raise error_class(parsed_response, operation_name)
ClientError: An error occurred (InvalidLaunchTemplateId.NotFound) when calling the DescribeLaunchTemplateVersions operation: The specified launch template, with template ID lt-0634c8a44afa8b953, does not exist.

# Fixed code
def get_resources(self, rids, cache=True):
    # Launch template versions have a compound primary key
    #
    # Support one of four forms of resource ids:
    #
    #  - array of launch template ids
    #  - array of tuples (launch template id, version id)
    #  - array of dicts (with LaunchTemplateId and VersionNumber)
    #  - array of dicts (with LaunchTemplateId and LatestVersionNumber)
    #
    # If an alias version is given $Latest, $Default, the alias will be
    # preserved as an annotation on the returned object 'c7n:VersionAlias'
    if not rids:
        return []

    t_versions = {}
    if isinstance(rids[0], tuple):
        for tid, tversion in rids:
            t_versions.setdefault(tid, []).append(tversion)
    elif isinstance(rids[0], dict):
        for tinfo in rids:
            t_versions.setdefault(tinfo["LaunchTemplateId"], []).append(
                tinfo.get("VersionNumber", tinfo.get("LatestVersionNumber"))
            )
    elif isinstance(rids[0], six.string_types):
        for tid in rids:
            t_versions[tid] = []

    client = utils.local_session(self.session_factory).client("ec2")

    results = []
    # We may end up fetching duplicates on $Latest and $Version
    for tid, tversions in t_versions.items():
        try:
            ltv = client.describe_launch_template_versions(
                LaunchTemplateId=tid, Versions=tversions
            ).get("LaunchTemplateVersions")
        except ClientError as e:
            if e.response["Error"]["Code"] == "InvalidLaunchTemplateId.NotFound":
                continue
            if e.response["Error"]["Code"] == "InvalidLaunchTemplateId.VersionNotFound":
                continue
            raise
        if not tversions:
            tversions = [str(t["VersionNumber"]) for t in ltv]
        for tversion, t in zip(tversions, ltv):
            if not tversion.isdigit():
                t["c7n:VersionAlias"] = tversion
            results.append(t)
    return results