# Buggy code
def process_resource_set(self, client, asgs, tags):
    tag_params = []
    propagate = self.data.get("propagate", False)
    for t in tags:
        if "PropagateAtLaunch" not in t:
            t["PropagateAtLaunch"] = propagate
    for t in tags:
        for a in asgs:
            atags = dict(t)
            atags["ResourceType"] = "auto-scaling-group"
            atags["ResourceId"] = a["AutoScalingGroupName"]
            tag_params.append(atags)
    self.manager.retry(client.create_or_update_tags, Tags=tag_params)


# 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_resource_set(self, client, asgs, tags):
    tag_params = []
    propagate = self.data.get("propagate", False)
    for t in tags:
        if "PropagateAtLaunch" not in t:
            t["PropagateAtLaunch"] = propagate
    for t in tags:
        for a in asgs:
            atags = dict(t)
            atags["ResourceType"] = "auto-scaling-group"
            atags["ResourceId"] = a["AutoScalingGroupName"]
            tag_params.append(atags)
            a.setdefault("Tags", []).append(atags)
    self.manager.retry(client.create_or_update_tags, Tags=tag_params)