# Buggy code
def _set_topic_subs(self):
    subscriptions_existing_list = []
    desired_subscriptions = [
        (sub["protocol"], self._canonicalize_endpoint(sub["protocol"], sub["endpoint"]))
        for sub in self.subscriptions
    ]

    if self.subscriptions_existing:
        for sub in self.subscriptions_existing:
            sub_key = (sub["Protocol"], sub["Endpoint"])
            subscriptions_existing_list.append(sub_key)
            if (
                self.purge_subscriptions
                and sub_key not in desired_subscriptions
                and sub["SubscriptionArn"] not in ("PendingConfirmation", "Deleted")
            ):
                self.changed = True
                self.subscriptions_deleted.append(sub_key)
                if not self.check_mode:
                    self.connection.unsubscribe(sub["SubscriptionArn"])

    for protocol, endpoint in desired_subscriptions:
        if (protocol, endpoint) not in subscriptions_existing_list:
            self.changed = True
            self.subscriptions_added.append(sub)
            if not self.check_mode:
                self.connection.subscribe(self.arn_topic, protocol, endpoint)


# Traceback (UnboundLocalError)
UnboundLocalError: local variable 'sub' referenced before assignment

fatal: [localhost]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 405, in <module>\n    main()\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 392, in main\n    sns_topic.ensure_ok()\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 312, in ensure_ok\n    self._set_topic_subs()\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 283, in _set_topic_subs\n    self.subscriptions_added.append(sub)\nUnboundLocalError: local variable 'sub' referenced before assignment\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

*********************************************************************************************************************************************************************************
task path: XXXXXXX
ok: [localhost] => {
"ansible_facts": {
"sqs_id": "arn:aws:sqs:us-east-1:XXXXXXXX:cloudtrail_sqs_sbx"
},
"changed": false,
"failed": false
}

TASK [SNS : Create SNS] *********************************************************************************************************************************************************************************************

The full traceback is:
Traceback (most recent call last):
File "/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py", line 405, in <module>
main()
File "/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py", line 392, in main
sns_topic.ensure_ok()
File "/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py", line 312, in ensure_ok
self._set_topic_subs()
File "/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py", line 283, in _set_topic_subs
self.subscriptions_added.append(sub)
UnboundLocalError: local variable 'sub' referenced before assignment

fatal: [localhost]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 405, in <module>\n    main()\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 392, in main\n    sns_topic.ensure_ok()\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 312, in ensure_ok\n    self._set_topic_subs()\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 283, in _set_topic_subs\n    self.subscriptions_added.append(sub)\nUnboundLocalError: local variable 'sub' referenced before assignment\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def _set_topic_subs(self):
    subscriptions_existing_list = []
    desired_subscriptions = [
        (sub["protocol"], self._canonicalize_endpoint(sub["protocol"], sub["endpoint"]))
        for sub in self.subscriptions
    ]

    if self.subscriptions_existing:
        for sub in self.subscriptions_existing:
            sub_key = (sub["Protocol"], sub["Endpoint"])
            subscriptions_existing_list.append(sub_key)
            if (
                self.purge_subscriptions
                and sub_key not in desired_subscriptions
                and sub["SubscriptionArn"] not in ("PendingConfirmation", "Deleted")
            ):
                self.changed = True
                self.subscriptions_deleted.append(sub_key)
                if not self.check_mode:
                    self.connection.unsubscribe(sub["SubscriptionArn"])

    for protocol, endpoint in desired_subscriptions:
        if (protocol, endpoint) not in subscriptions_existing_list:
            self.changed = True
            self.subscriptions_added.append((protocol, endpoint))
            if not self.check_mode:
                self.connection.subscribe(self.arn_topic, protocol, endpoint)