# Buggy code
def securitygroupid(vm_):
    """
    Returns the SecurityGroupId
    """
    securitygroupid_set = set()
    securitygroupid_list = config.get_cloud_config_value(
        "securitygroupid", vm_, __opts__, search_global=False
    )
    # If the list is None, then the set will remain empty
    # If the list is already a set then calling 'set' on it is a no-op
    # If the list is a string, then calling 'set' generates a one-element set
    # If the list is anything else, stacktrace
    if securitygroupid_list:
        securitygroupid_set = securitygroupid_set.union(set(securitygroupid_list))

    securitygroupname_list = config.get_cloud_config_value(
        "securitygroupname", vm_, __opts__, search_global=False
    )
    if securitygroupname_list:
        if not isinstance(securitygroupname_list, list):
            securitygroupname_list = [securitygroupname_list]
        params = {"Action": "DescribeSecurityGroups"}
        for sg in aws.query(
            params,
            location=get_location(),
            provider=get_provider(),
            opts=__opts__,
            sigver="4",
        ):
            if sg["groupName"] in securitygroupname_list:
                log.debug(
                    "AWS SecurityGroup ID of {0} is {1}".format(
                        sg["groupName"], sg["groupId"]
                    )
                )
                securitygroupid_set = securitygroupid_set.add(sg["groupId"])
    return list(securitygroupid_set)


# Traceback (TypeError)
$ salt 'salt*' state.sls logs_cloud/instances
salt.tools:
----------
ID: test-a
Function: cloud.present
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1744, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1702, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/states/cloud.py", line 126, in present
info = __salt__['cloud.create'](cloud_provider, name, opts=opts, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/modules/cloud.py", line 254, in create
info = client.create(provider, names, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/cloud/__init__.py", line 409, in create
mapper.create(vm_))
File "/usr/lib/python2.7/dist-packages/salt/cloud/__init__.py", line 1288, in create
output = self.clouds[func](vm_)
File "/usr/lib/python2.7/dist-packages/salt/cloud/clouds/ec2.py", line 2510, in create
data, vm_ = request_instance(vm_, location)
File "/usr/lib/python2.7/dist-packages/salt/cloud/clouds/ec2.py", line 1757, in request_instance
ex_securitygroupid = securitygroupid(vm_)
File "/usr/lib/python2.7/dist-packages/salt/cloud/clouds/ec2.py", line 1183, in securitygroupid
return list(securitygroupid_set)
TypeError: 'NoneType' object is not iterable
Started: 22:26:25.805204
Duration: 1386.472 ms
Changes:

# Fixed code
def securitygroupid(vm_):
    """
    Returns the SecurityGroupId
    """
    securitygroupid_set = set()
    securitygroupid_list = config.get_cloud_config_value(
        "securitygroupid", vm_, __opts__, search_global=False
    )
    # If the list is None, then the set will remain empty
    # If the list is already a set then calling 'set' on it is a no-op
    # If the list is a string, then calling 'set' generates a one-element set
    # If the list is anything else, stacktrace
    if securitygroupid_list:
        securitygroupid_set = securitygroupid_set.union(set(securitygroupid_list))

    securitygroupname_list = config.get_cloud_config_value(
        "securitygroupname", vm_, __opts__, search_global=False
    )
    if securitygroupname_list:
        if not isinstance(securitygroupname_list, list):
            securitygroupname_list = [securitygroupname_list]
        params = {"Action": "DescribeSecurityGroups"}
        for sg in aws.query(
            params,
            location=get_location(),
            provider=get_provider(),
            opts=__opts__,
            sigver="4",
        ):
            if sg["groupName"] in securitygroupname_list:
                log.debug(
                    "AWS SecurityGroup ID of {0} is {1}".format(
                        sg["groupName"], sg["groupId"]
                    )
                )
                securitygroupid_set.add(sg["groupId"])
    return list(securitygroupid_set)