describe_security_groups

EC2.Client.describe_security_groups(**kwargs)

Describes the specified security groups or all of your security groups.

A security group is for use with instances either in the EC2-Classic platform or in a specific VPC. For more information, see Amazon EC2 security groups in the Amazon Elastic Compute Cloud User Guide and Security groups for your VPC in the Amazon Virtual Private Cloud User Guide .

Note

We are retiring EC2-Classic. We recommend that you migrate from EC2-Classic to a VPC. For more information, see Migrate from EC2-Classic to a VPC in the Amazon Elastic Compute Cloud User Guide .

See also: AWS API Documentation

Request Syntax

response = client.describe_security_groups(
    Filters=[
        {
            'Name': 'string',
            'Values': [
                'string',
            ]
        },
    ],
    GroupIds=[
        'string',
    ],
    GroupNames=[
        'string',
    ],
    DryRun=True|False,
    NextToken='string',
    MaxResults=123
)
Parameters
  • Filters (list) --

    The filters. If using multiple filters for rules, the results include security groups for which any combination of rules - not necessarily a single rule - match all filters.

    • description - The description of the security group.
    • egress.ip-permission.cidr - An IPv4 CIDR block for an outbound security group rule.
    • egress.ip-permission.from-port - For an outbound rule, the start of port range for the TCP and UDP protocols, or an ICMP type number.
    • egress.ip-permission.group-id - The ID of a security group that has been referenced in an outbound security group rule.
    • egress.ip-permission.group-name - The name of a security group that is referenced in an outbound security group rule.
    • egress.ip-permission.ipv6-cidr - An IPv6 CIDR block for an outbound security group rule.
    • egress.ip-permission.prefix-list-id - The ID of a prefix list to which a security group rule allows outbound access.
    • egress.ip-permission.protocol - The IP protocol for an outbound security group rule ( tcp | udp | icmp , a protocol number, or -1 for all protocols).
    • egress.ip-permission.to-port - For an outbound rule, the end of port range for the TCP and UDP protocols, or an ICMP code.
    • egress.ip-permission.user-id - The ID of an Amazon Web Services account that has been referenced in an outbound security group rule.
    • group-id - The ID of the security group.
    • group-name - The name of the security group.
    • ip-permission.cidr - An IPv4 CIDR block for an inbound security group rule.
    • ip-permission.from-port - For an inbound rule, the start of port range for the TCP and UDP protocols, or an ICMP type number.
    • ip-permission.group-id - The ID of a security group that has been referenced in an inbound security group rule.
    • ip-permission.group-name - The name of a security group that is referenced in an inbound security group rule.
    • ip-permission.ipv6-cidr - An IPv6 CIDR block for an inbound security group rule.
    • ip-permission.prefix-list-id - The ID of a prefix list from which a security group rule allows inbound access.
    • ip-permission.protocol - The IP protocol for an inbound security group rule ( tcp | udp | icmp , a protocol number, or -1 for all protocols).
    • ip-permission.to-port - For an inbound rule, the end of port range for the TCP and UDP protocols, or an ICMP code.
    • ip-permission.user-id - The ID of an Amazon Web Services account that has been referenced in an inbound security group rule.
    • owner-id - The Amazon Web Services account ID of the owner of the security group.
    • tag :<key> - The key/value combination of a tag assigned to the resource. Use the tag key in the filter name and the tag value as the filter value. For example, to find all resources that have a tag with the key Owner and the value TeamA , specify tag:Owner for the filter name and TeamA for the filter value.
    • tag-key - The key of a tag assigned to the resource. Use this filter to find all resources assigned a tag with a specific key, regardless of the tag value.
    • vpc-id - The ID of the VPC specified when the security group was created.
    • (dict) --

      A filter name and value pair that is used to return a more specific list of results from a describe operation. Filters can be used to match a set of resources by specific criteria, such as tags, attributes, or IDs.

      If you specify multiple filters, the filters are joined with an AND , and the request returns only results that match all of the specified filters.

      • Name (string) --

        The name of the filter. Filter names are case-sensitive.

      • Values (list) --

        The filter values. Filter values are case-sensitive. If you specify multiple values for a filter, the values are joined with an OR , and the request returns all results that match any of the specified values.

        • (string) --
  • GroupIds (list) --

    The IDs of the security groups. Required for security groups in a nondefault VPC.

    Default: Describes all of your security groups.

    • (string) --
  • GroupNames (list) --

    [EC2-Classic and default VPC only] The names of the security groups. You can specify either the security group name or the security group ID. For security groups in a nondefault VPC, use the group-name filter to describe security groups by name.

    Default: Describes all of your security groups.

    • (string) --
  • DryRun (boolean) -- Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation . Otherwise, it is UnauthorizedOperation .
  • NextToken (string) -- The token returned from a previous paginated request. Pagination continues from the end of the items returned by the previous request.
  • MaxResults (integer) -- The maximum number of items to return for this request. To get the next page of items, make another request with the token returned in the output. This value can be between 5 and 1000. If this parameter is not specified, then all items are returned. For more information, see Pagination.
Return type

dict

Returns

Response Syntax

{
    'SecurityGroups': [
        {
            'Description': 'string',
            'GroupName': 'string',
            'IpPermissions': [
                {
                    'FromPort': 123,
                    'IpProtocol': 'string',
                    'IpRanges': [
                        {
                            'CidrIp': 'string',
                            'Description': 'string'
                        },
                    ],
                    'Ipv6Ranges': [
                        {
                            'CidrIpv6': 'string',
                            'Description': 'string'
                        },
                    ],
                    'PrefixListIds': [
                        {
                            'Description': 'string',
                            'PrefixListId': 'string'
                        },
                    ],
                    'ToPort': 123,
                    'UserIdGroupPairs': [
                        {
                            'Description': 'string',
                            'GroupId': 'string',
                            'GroupName': 'string',
                            'PeeringStatus': 'string',
                            'UserId': 'string',
                            'VpcId': 'string',
                            'VpcPeeringConnectionId': 'string'
                        },
                    ]
                },
            ],
            'OwnerId': 'string',
            'GroupId': 'string',
            'IpPermissionsEgress': [
                {
                    'FromPort': 123,
                    'IpProtocol': 'string',
                    'IpRanges': [
                        {
                            'CidrIp': 'string',
                            'Description': 'string'
                        },
                    ],
                    'Ipv6Ranges': [
                        {
                            'CidrIpv6': 'string',
                            'Description': 'string'
                        },
                    ],
                    'PrefixListIds': [
                        {
                            'Description': 'string',
                            'PrefixListId': 'string'
                        },
                    ],
                    'ToPort': 123,
                    'UserIdGroupPairs': [
                        {
                            'Description': 'string',
                            'GroupId': 'string',
                            'GroupName': 'string',
                            'PeeringStatus': 'string',
                            'UserId': 'string',
                            'VpcId': 'string',
                            'VpcPeeringConnectionId': 'string'
                        },
                    ]
                },
            ],
            'Tags': [
                {
                    'Key': 'string',
                    'Value': 'string'
                },
            ],
            'VpcId': 'string'
        },
    ],
    'NextToken': 'string'
}

Response Structure

  • (dict) --

    • SecurityGroups (list) --

      Information about the security groups.

      • (dict) --

        Describes a security group.

        • Description (string) --

          A description of the security group.

        • GroupName (string) --

          The name of the security group.

        • IpPermissions (list) --

          The inbound rules associated with the security group.

          • (dict) --

            Describes a set of permissions for a security group rule.

            • FromPort (integer) --

              If the protocol is TCP or UDP, this is the start of the port range. If the protocol is ICMP or ICMPv6, this is the type number. A value of -1 indicates all ICMP/ICMPv6 types. If you specify all ICMP/ICMPv6 types, you must specify all ICMP/ICMPv6 codes.

            • IpProtocol (string) --

              The IP protocol name ( tcp , udp , icmp , icmpv6 ) or number (see Protocol Numbers ).

              [VPC only] Use -1 to specify all protocols. When authorizing security group rules, specifying -1 or a protocol number other than tcp , udp , icmp , or icmpv6 allows traffic on all ports, regardless of any port range you specify. For tcp , udp , and icmp , you must specify a port range. For icmpv6 , the port range is optional; if you omit the port range, traffic for all types and codes is allowed.

            • IpRanges (list) --

              The IPv4 ranges.

              • (dict) --

                Describes an IPv4 range.

                • CidrIp (string) --

                  The IPv4 CIDR range. You can either specify a CIDR range or a source security group, not both. To specify a single IPv4 address, use the /32 prefix length.

                • Description (string) --

                  A description for the security group rule that references this IPv4 address range.

                  Constraints: Up to 255 characters in length. Allowed characters are a-z, A-Z, 0-9, spaces, and ._-:/()#,@[]+=&;{}!$*

            • Ipv6Ranges (list) --

              [VPC only] The IPv6 ranges.

              • (dict) --

                [EC2-VPC only] Describes an IPv6 range.

                • CidrIpv6 (string) --

                  The IPv6 CIDR range. You can either specify a CIDR range or a source security group, not both. To specify a single IPv6 address, use the /128 prefix length.

                • Description (string) --

                  A description for the security group rule that references this IPv6 address range.

                  Constraints: Up to 255 characters in length. Allowed characters are a-z, A-Z, 0-9, spaces, and ._-:/()#,@[]+=&;{}!$*

            • PrefixListIds (list) --

              [VPC only] The prefix list IDs.

              • (dict) --

                Describes a prefix list ID.

                • Description (string) --

                  A description for the security group rule that references this prefix list ID.

                  Constraints: Up to 255 characters in length. Allowed characters are a-z, A-Z, 0-9, spaces, and ._-:/()#,@[]+=;{}!$*

                • PrefixListId (string) --

                  The ID of the prefix.

            • ToPort (integer) --

              If the protocol is TCP or UDP, this is the end of the port range. If the protocol is ICMP or ICMPv6, this is the code. A value of -1 indicates all ICMP/ICMPv6 codes. If you specify all ICMP/ICMPv6 types, you must specify all ICMP/ICMPv6 codes.

            • UserIdGroupPairs (list) --

              The security group and Amazon Web Services account ID pairs.

              • (dict) --

                Describes a security group and Amazon Web Services account ID pair.

                Note

                We are retiring EC2-Classic. We recommend that you migrate from EC2-Classic to a VPC. For more information, see Migrate from EC2-Classic to a VPC in the Amazon Elastic Compute Cloud User Guide .

                • Description (string) --

                  A description for the security group rule that references this user ID group pair.

                  Constraints: Up to 255 characters in length. Allowed characters are a-z, A-Z, 0-9, spaces, and ._-:/()#,@[]+=;{}!$*

                • GroupId (string) --

                  The ID of the security group.

                • GroupName (string) --

                  The name of the security group. In a request, use this parameter for a security group in EC2-Classic or a default VPC only. For a security group in a nondefault VPC, use the security group ID.

                  For a referenced security group in another VPC, this value is not returned if the referenced security group is deleted.

                • PeeringStatus (string) --

                  The status of a VPC peering connection, if applicable.

                • UserId (string) --

                  The ID of an Amazon Web Services account.

                  For a referenced security group in another VPC, the account ID of the referenced security group is returned in the response. If the referenced security group is deleted, this value is not returned.

                  [EC2-Classic] Required when adding or removing rules that reference a security group in another Amazon Web Services account.

                • VpcId (string) --

                  The ID of the VPC for the referenced security group, if applicable.

                • VpcPeeringConnectionId (string) --

                  The ID of the VPC peering connection, if applicable.

        • OwnerId (string) --

          The Amazon Web Services account ID of the owner of the security group.

        • GroupId (string) --

          The ID of the security group.

        • IpPermissionsEgress (list) --

          [VPC only] The outbound rules associated with the security group.

          • (dict) --

            Describes a set of permissions for a security group rule.

            • FromPort (integer) --

              If the protocol is TCP or UDP, this is the start of the port range. If the protocol is ICMP or ICMPv6, this is the type number. A value of -1 indicates all ICMP/ICMPv6 types. If you specify all ICMP/ICMPv6 types, you must specify all ICMP/ICMPv6 codes.

            • IpProtocol (string) --

              The IP protocol name ( tcp , udp , icmp , icmpv6 ) or number (see Protocol Numbers ).

              [VPC only] Use -1 to specify all protocols. When authorizing security group rules, specifying -1 or a protocol number other than tcp , udp , icmp , or icmpv6 allows traffic on all ports, regardless of any port range you specify. For tcp , udp , and icmp , you must specify a port range. For icmpv6 , the port range is optional; if you omit the port range, traffic for all types and codes is allowed.

            • IpRanges (list) --

              The IPv4 ranges.

              • (dict) --

                Describes an IPv4 range.

                • CidrIp (string) --

                  The IPv4 CIDR range. You can either specify a CIDR range or a source security group, not both. To specify a single IPv4 address, use the /32 prefix length.

                • Description (string) --

                  A description for the security group rule that references this IPv4 address range.

                  Constraints: Up to 255 characters in length. Allowed characters are a-z, A-Z, 0-9, spaces, and ._-:/()#,@[]+=&;{}!$*

            • Ipv6Ranges (list) --

              [VPC only] The IPv6 ranges.

              • (dict) --

                [EC2-VPC only] Describes an IPv6 range.

                • CidrIpv6 (string) --

                  The IPv6 CIDR range. You can either specify a CIDR range or a source security group, not both. To specify a single IPv6 address, use the /128 prefix length.

                • Description (string) --

                  A description for the security group rule that references this IPv6 address range.

                  Constraints: Up to 255 characters in length. Allowed characters are a-z, A-Z, 0-9, spaces, and ._-:/()#,@[]+=&;{}!$*

            • PrefixListIds (list) --

              [VPC only] The prefix list IDs.

              • (dict) --

                Describes a prefix list ID.

                • Description (string) --

                  A description for the security group rule that references this prefix list ID.

                  Constraints: Up to 255 characters in length. Allowed characters are a-z, A-Z, 0-9, spaces, and ._-:/()#,@[]+=;{}!$*

                • PrefixListId (string) --

                  The ID of the prefix.

            • ToPort (integer) --

              If the protocol is TCP or UDP, this is the end of the port range. If the protocol is ICMP or ICMPv6, this is the code. A value of -1 indicates all ICMP/ICMPv6 codes. If you specify all ICMP/ICMPv6 types, you must specify all ICMP/ICMPv6 codes.

            • UserIdGroupPairs (list) --

              The security group and Amazon Web Services account ID pairs.

              • (dict) --

                Describes a security group and Amazon Web Services account ID pair.

                Note

                We are retiring EC2-Classic. We recommend that you migrate from EC2-Classic to a VPC. For more information, see Migrate from EC2-Classic to a VPC in the Amazon Elastic Compute Cloud User Guide .

                • Description (string) --

                  A description for the security group rule that references this user ID group pair.

                  Constraints: Up to 255 characters in length. Allowed characters are a-z, A-Z, 0-9, spaces, and ._-:/()#,@[]+=;{}!$*

                • GroupId (string) --

                  The ID of the security group.

                • GroupName (string) --

                  The name of the security group. In a request, use this parameter for a security group in EC2-Classic or a default VPC only. For a security group in a nondefault VPC, use the security group ID.

                  For a referenced security group in another VPC, this value is not returned if the referenced security group is deleted.

                • PeeringStatus (string) --

                  The status of a VPC peering connection, if applicable.

                • UserId (string) --

                  The ID of an Amazon Web Services account.

                  For a referenced security group in another VPC, the account ID of the referenced security group is returned in the response. If the referenced security group is deleted, this value is not returned.

                  [EC2-Classic] Required when adding or removing rules that reference a security group in another Amazon Web Services account.

                • VpcId (string) --

                  The ID of the VPC for the referenced security group, if applicable.

                • VpcPeeringConnectionId (string) --

                  The ID of the VPC peering connection, if applicable.

        • Tags (list) --

          Any tags assigned to the security group.

          • (dict) --

            Describes a tag.

            • Key (string) --

              The key of the tag.

              Constraints: Tag keys are case-sensitive and accept a maximum of 127 Unicode characters. May not begin with aws: .

            • Value (string) --

              The value of the tag.

              Constraints: Tag values are case-sensitive and accept a maximum of 256 Unicode characters.

        • VpcId (string) --

          [VPC only] The ID of the VPC for the security group.

    • NextToken (string) --

      The token to include in another request to get the next page of items. This value is null when there are no more items to return.

Examples

This example describes the specified security group.

response = client.describe_security_groups(
    GroupIds=[
        'sg-903004f8',
    ],
)

print(response)

Expected Output:

{
    'ResponseMetadata': {
        '...': '...',
    },
}

This example describes the security groups that include the specified tag (Purpose=test).

response = client.describe_security_groups(
    Filters=[
        {
            'Name': 'tag:Purpose',
            'Values': [
                'test',
            ],
        },
    ],
)

print(response)

Expected Output:

{
    'ResponseMetadata': {
        '...': '...',
    },
}