# Buggy code
def process(self, asgs):
    tags = self.get_tag_set()
    error = None

    client = self.get_client()
    with self.executor_factory(max_workers=3) as w:
        futures = {}
        for asg_set in chunks(asgs, self.batch_size):
            futures[w.submit(self.process_resource_set, client, asg_set, tags)] = (
                asg_set
            )
        for f in as_completed(futures):
            asg_set = futures[f]
            if f.exception():
                self.log.exception(
                    "Exception tagging tag:%s error:%s asg:%s"
                    % (
                        tags,
                        f.exception(),
                        ", ".join([a["AutoScalingGroupName"] for a in asg_set]),
                    )
                )
    if error:
        raise error


# 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(self, asgs):
    tags = self.get_tag_set()
    error = None

    client = self.get_client()
    with self.executor_factory(max_workers=2) as w:
        futures = {}
        for asg_set in chunks(asgs, self.batch_size):
            futures[w.submit(self.process_resource_set, client, asg_set, tags)] = (
                asg_set
            )
        for f in as_completed(futures):
            asg_set = futures[f]
            if f.exception():
                self.log.exception(
                    "Exception tagging tag:%s error:%s asg:%s"
                    % (
                        tags,
                        f.exception(),
                        ", ".join([a["AutoScalingGroupName"] for a in asg_set]),
                    )
                )
    if error:
        raise error