# 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 and instances:
        client.create_tags(
            Resources=instance_ids,
            Tags=[{"Key": k, "Value": v} for k, v in tag_map.items()],
        )
    return len(instance_ids)


# Traceback (UnboundLocalError)
[ERROR]    2020-10-27T18:30:32.480Z    ec614241-19ef-4d44-b3a0-400229378905    error during policy execution
Traceback (most recent call last):
File "/var/task/c7n/handler.py", line 166, in dispatch_event
p.push(event, context)
File "/var/task/c7n/policy.py", line 1138, in push
return mode.run(event, lambda_ctx)
File "/var/task/c7n/policy.py", line 526, in run
return PullMode.run(self)
File "/var/task/c7n/policy.py", line 317, in run
results = a.process(resources)
File "/var/task/c7n/resources/asg.py", line 1212, in process
instance_count = sum(list(w.map(self.process_asg, asgs)))
File "/var/lang/lib/python3.8/concurrent/futures/_base.py", line 611, in result_iterator
yield fs.pop().result()
File "/var/lang/lib/python3.8/concurrent/futures/_base.py", line 439, in result
return self.__get_result()
File "/var/lang/lib/python3.8/concurrent/futures/_base.py", line 388, in __get_result
raise self._exception
File "/var/lang/lib/python3.8/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/var/task/c7n/resources/asg.py", line 1230, in process_asg
if not self.manager.config.dryrun and instances:
UnboundLocalError: local variable 'instances' referenced before assignment
[ERROR] 2020-10-27T18:30:32.480Z ec614241-19ef-4d44-b3a0-400229378905 error during policy execution Traceback (most recent call last): File "/var/task/c7n/handler.py", line 166, in dispatch_event p.push(event, context) File "/var/task/c7n/policy.py", line 1138, in push return mode.run(event, lambda_ctx) File "/var/task/c7n/policy.py", line 526, in run return PullMode.run(self) File "/var/task/c7n/policy.py", line 317, in run results = a.process(resources) File "/var/task/c7n/resources/asg.py", line 1212, in process instance_count = sum(list(w.map(self.process_asg, asgs))) File "/var/lang/lib/python3.8/concurrent/futures/_base.py", line 611, in result_iterator yield fs.pop().result() File "/var/lang/lib/python3.8/concurrent/futures/_base.py", line 439, in result return self.__get_result() File "/var/lang/lib/python3.8/concurrent/futures/_base.py", line 388, in __get_result raise self._exception File "/var/lang/lib/python3.8/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "/var/task/c7n/resources/asg.py", line 1230, in process_asg if not self.manager.config.dryrun and instances: UnboundLocalError: local variable 'instances' referenced before assignment

# Fixed code
def process_asg(self, asg):
    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"]}

    if not tag_map and not self.get("trim", False):
        self.log.error(
            "No tags found to propagate on asg:{} tags configured:{}".format(
                asg["AutoScalingGroupName"], self.data.get("tags")
            )
        )

    tag_set = set(tag_map)
    client = local_session(self.manager.session_factory).client("ec2")

    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 instance_ids and tag_map:
        client.create_tags(
            Resources=instance_ids,
            Tags=[{"Key": k, "Value": v} for k, v in tag_map.items()],
        )
    return len(instance_ids)