# Buggy code
def process_asg(self, asg):
    client = local_session(self.manager.session_factory).client("ec2")
    instance_ids = [i["InstanceId"] for i in asg["Instances"]]
    tag_map = {
        t["Key"]: t["Value"]
        for t in asg.get("Tags", [])
        if t["PropagateAtLaunch"] and not t["Key"].startswith("aws:")
    }

    if self.data.get("tags"):
        tag_map = {k: v for k, v in tag_map.items() if k in self.data["tags"]}

    tag_set = set(tag_map)
    if self.data.get("trim", False):
        instances = [self.instance_map[i] for i in instance_ids]
        self.prune_instance_tags(client, asg, tag_set, instances)
    if not self.manager.config.dryrun:
        client.create_tags(
            Resources=instance_ids,
            Tags=[{"Key": k, "Value": v} for k, v in tag_map.items()],
        )
    return len(instance_ids)


# Traceback (botocore.exceptions.ClientError)
[ERROR] 2019-11-19T02:56:38.336Z b1c8af86-d773-412d-b755-638faf7698e8 Error while executing policy
20:56:38 Traceback (most recent call last):
20:56:38 File "/var/task/c7n/policy.py", line 320, in run
20:56:38 results = a.process(resources)
20:56:38 File "/var/task/c7n/resources/asg.py", line 1200, in process
20:56:38 instance_count = sum(list(w.map(self.process_asg, asgs)))
20:56:38 File "/var/lang/lib/python3.7/concurrent/futures/_base.py", line 598, in result_iterator
20:56:38 yield fs.pop().result()
20:56:38 File "/var/lang/lib/python3.7/concurrent/futures/_base.py", line 435, in result
20:56:38 return self.__get_result()
20:56:38 File "/var/lang/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
20:56:38 raise self._exception
20:56:38 File "/var/lang/lib/python3.7/concurrent/futures/thread.py", line 57, in run
20:56:38 result = self.fn(*self.args, **self.kwargs)
20:56:38 File "/var/task/c7n/resources/asg.py", line 1221, in process_asg
20:56:38 Tags=[{'Key': k, 'Value': v} for k, v in tag_map.items()])
20:56:38 File "/var/runtime/botocore/client.py", line 357, in _api_call
20:56:38 return self._make_api_call(operation_name, kwargs)
20:56:38 File "/var/runtime/botocore/client.py", line 661, in _make_api_call
20:56:38 raise error_class(parsed_response, operation_name)
20:56:38 botocore.exceptions.ClientError: An error occurred (MissingParameter) when calling the CreateTags operation: The request must contain the parameter resourceIdSet
20:56:38 [ERROR] 2019-11-19T02:56:38.338Z b1c8af86-d773-412d-b755-638faf7698e8 error during policy execution
20:56:38 Traceback (most recent call last):
20:56:38 File "/var/task/c7n/handler.py", line 168, in dispatch_event
20:56:38 p.push(event, context)
20:56:38 File "/var/task/c7n/policy.py", line 991, in push
20:56:38 return mode.run(event, lambda_ctx)
20:56:38 File "/var/task/c7n/policy.py", line 591, in run
20:56:38 return PullMode.run(self)
20:56:38 File "/var/task/c7n/policy.py", line 320, in run
20:56:38 results = a.process(resources)
20:56:38 File "/var/task/c7n/resources/asg.py", line 1200, in process
20:56:38 instance_count = sum(list(w.map(self.process_asg, asgs)))
20:56:38 File "/var/lang/lib/python3.7/concurrent/futures/_base.py", line 598, in result_iterator
20:56:38 yield fs.pop().result()
20:56:38 File "/var/lang/lib/python3.7/concurrent/futures/_base.py", line 435, in result
20:56:38 return self.__get_result()
20:56:38 File "/var/lang/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
20:56:38 raise self._exception
20:56:38 File "/var/lang/lib/python3.7/concurrent/futures/thread.py", line 57, in run
20:56:38 result = self.fn(*self.args, **self.kwargs)
20:56:38 File "/var/task/c7n/resources/asg.py", line 1221, in process_asg
20:56:38 Tags=[{'Key': k, 'Value': v} for k, v in tag_map.items()])
20:56:38 File "/var/runtime/botocore/client.py", line 357, in _api_call
20:56:38 return self._make_api_call(operation_name, kwargs)
20:56:38 File "/var/runtime/botocore/client.py", line 661, in _make_api_call
20:56:38 raise error_class(parsed_response, operation_name)
20:56:38 botocore.exceptions.ClientError: An error occurred (MissingParameter) when calling the CreateTags operation: The request must contain the parameter resourceIdSet
20:56:38 [ERROR] ClientError: An error occurred (MissingParameter) when calling the CreateTags operation: The request must contain the parameter resourceIdSet 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 168, in dispatch_event     p.push(event, context)   File "/var/task/c7
20:56:38 END RequestId: b1c8af86-d773-412d-b755-638faf7698e8

# Fixed code
def process_asg(self, asg):
    client = local_session(self.manager.session_factory).client("ec2")
    instance_ids = [i["InstanceId"] for i in asg["Instances"]]
    tag_map = {
        t["Key"]: t["Value"]
        for t in asg.get("Tags", [])
        if t["PropagateAtLaunch"] and not t["Key"].startswith("aws:")
    }

    if self.data.get("tags"):
        tag_map = {k: v for k, v in tag_map.items() if k in self.data["tags"]}

    tag_set = set(tag_map)
    if self.data.get("trim", False):
        instances = [self.instance_map[i] for i in instance_ids]
        self.prune_instance_tags(client, asg, tag_set, instances)
    if not self.manager.config.dryrun and instances:
        client.create_tags(
            Resources=instance_ids,
            Tags=[{"Key": k, "Value": v} for k, v in tag_map.items()],
        )
    return len(instance_ids)