VM::EC...yGroup(3pm)

πŸ“¦
version 1.28 Β· 7 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– VM::EC2::SecurityGroup β€” the perl manual, but make it cute πŸ’–

okay bestie, gather round, because this is the VM::EC2::SecurityGroup page from the User Contributed Perl Documentation, and it's literally all about describing an Amazon EC2 security group. it's giving firewall rules, but make it ✨documentation✨.


🌸 NAME 🌸

VM::EC2::SecurityGroup β€” an object that describes an Amazon EC2 security group πŸ›‘οΈ


✨ SYNOPSIS ✨

here's the whole vibe, copy it exactly, no cap:

use VM::EC2;

$ec2       = VM::EC2->new(...);
@sg = $ec2->describe_security_groups;
for my $sg (@sg) {
    $name = $sg->groupName;
    $id   = $sg->groupId;
    $desc = $sg->groupDescription;
    $tags = $sg->tags;
    @inbound_permissions  = $sg->ipPermissions;
    @outbound_permissions = $sg->ipPermissionsEgress;
    for $i (@inbound_permissions) {
       $protocol = $i->ipProtocol;
       $fromPort = $i->fromPort;
       $toPort   = $i->toPort;
       @ranges   = $i->ipRanges;
    }
}

$sg = $sg[0];

# Add a new security rule
$sg->authorize_incoming(-protocol  => 'tcp',
                        -port      => 80,
                        -source_ip => ['192.168.2.0/24','192.168.2.1/24']);

# write it to AWS.
$sg->update();

πŸ¦‹ DESCRIPTION πŸ¦‹

so this object is the whole personality of an Amazon EC2 security group πŸ’…. you get one back from VM::EC2->describe_security_groups(). you can ALSO get one by calling an Instance object's groups() method, and then invoking one of the group's permissions() method. see VM::EC2::Group for that one, bestie.


πŸ‘‘ METHODS πŸ‘‘

the following object methods are supported, and yes, every single one slays:

  • ownerId β€” Owner of this security group
  • groupId β€” ID of this security group
  • groupName β€” Name of this security group
  • groupDescription β€” Description of this group
  • vpcId β€” Virtual Private Cloud ID, if applicable
  • ipPermissions β€” A list of rules that govern incoming connections to instances running under this security group. Each rule is a VM::EC2::SecurityGroup::IpPermission object.
  • ipPermissionsEgress β€” A list of rules that govern outgoing connections from instances running under this security group. Each rule is a VM::EC2::SecurityGroup::IpPermission object. This field is only valid for VPC groups.
  • tags β€” Hashref containing tags associated with this group. See VM::EC2::Generic.

and for convenience, these aliases are provided for the commonly used methods (iconic behaviour tbh):

  • inbound_permissions β€” same as ipPermissions()
  • outbound_permissions β€” same as ipPermissionsEgress()
  • name β€” same as groupName()

see VM::EC2::SecurityGroup::IpPermission for the deets on accessing port numbers, IP ranges and other fields associated with incoming and outgoing firewall rules 🧚


πŸ”₯ MODIFYING FIREWALL RULES πŸ”₯

to add or revoke firewall rules, call the authorize_incoming, authorize_outgoing, revoke_incoming or revoke_outgoing() methods one or more times. each of these methods either adds or removes a single firewall rule, so don't be surprised. after adding or revoking the desired rules, call update() to write the modified group back to Amazon. the object will change to reflect the new permissions. periodt.

πŸ’œ $permission = $group->authorize_incoming(%args)

add a rule for incoming firewall traffic. the arguments are as follows:

  • -protocol β€” The protocol, either a string (tcp,udp,icmp) or the corresponding protocol number (6, 17, 1). Use -1 to indicate all protocols. (required)
  • -port, -ports β€” The port or port range. When referring to a single port, you may use either the port number or the service name (e.g. "ssh"). For this to work the service name must be located in /etc/services. When specifying a port range, use "start..end" as in "8000..9000". Note that this is a string that contains two dots, and not two numbers separated by the perl range operator. For the icmp protocol, this argument corresponds to the ICMP type number. (required)
  • -group, -groups β€” Security groups to authorize. Instances that belong to the named security groups will be allowed access. You may specify either a single group or a list of groups as an arrayref. The following syntaxes are recognized:
    • "sg-12345" β€” authorize group with this groupId
    • "12345/my group" β€” authorize group named "my group" owned by user 12345
    • "my group" β€” authorize group named "my group" owned by yourself
  • -source, -source_ip β€” Authorize incoming traffic from an IP address, IP address range, or set of such ranges. IP addresses use the CIDR notation of a.b.c.d/mask, as described in http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing. Pass an arrayref to simultaneously authorize multiple CIDR ranges.

the result of this call is a VM::EC2::SecurityGroup::IpPermission object corresponding to the rule you defined. note that the rule is not written to Amazon until you call update() πŸ‘€

here are some examples, bestie, use them well:

$sg->authorize_incoming(-protocol  => 'tcp',
                        -port      => 80,
                        -source_ip => ['192.168.2.0/24','192.168.2.1/24']);

# TCP on ports 22 and 23 from anyone
$sg->authorize_incoming(-protocol  => 'tcp',
                        -port      => '22..23',
                        -source_ip => '0.0.0.0/0');

# ICMP on echo (ping) port from anyone
$sg->authorize_incoming(-protocol  => 'icmp',
                        -port      => -1,
                        -source_ip => '0.0.0.0/0');

# TCP to port 25 (mail) from instances belonging to
# the "Mail relay" group belonging to user 12345678.
$sg->authorize_incoming(-protocol  => 'tcp',
                        -port      => 25,
                        -group     => '12345678/Mail relay');

🌈 $permission = $group->authorize_outgoing(%args)

this is identical to authorize_incoming() except that the rule applies to outbound traffic. only VPC security groups can define outgoing firewall rules, so keep that in mind πŸ’€

πŸ“ $permission = $group->revoke_incoming($rule) / $permission = $group->revoke_incoming(%args)

this method revokes an incoming firewall rule. you can call it with a single argument consisting of a VM::EC2::SecurityGroup::IpPermission object in order to revoke that rule. alternatively, when called with the named arguments listed for authorize_incoming(), it will attempt to match an existing rule to the provided arguments and queue it for deletion.

here's an example of revoking all rules that allow ssh (port 22) access:

@ssh_rules = grep {$_->fromPort == 22} $group->ipPermissions;
$group->revoke_incoming($_) foreach @ssh_rules;
$group->update();

πŸŽ€ $boolean = $group->update()

this method will write all queued rule authorizations and revocations to Amazon, and return a true value if successful. the method will return false if any of the rule updates failed 😭. you can examine the VM::EC2 object's error_str() method to determine what went wrong, and check the group object's ipPermissions() method to see what firewall rules are currently defined.

⭐ $boolean = $group->write()

an alias for update(). same energy, shorter name πŸ’…

🫢 $group->refresh()

this method refreshes the group information from Amazon. it is called automatically by update().


🧚 STRING OVERLOADING 🧚

when used in a string context, this object will interpolate the groupId.


πŸ’« SEE ALSO πŸ’«

VM::EC2 VM::EC2::Generic VM::EC2::Instance VM::EC2::Group VM::EC2::SecurityGroup::IpPermission


πŸ’– AUTHOR πŸ’–

Lincoln Stein <lincoln.stein@gmail.com>.

Copyright (c) 2011 Ontario Institute for Cancer Research

This package and its accompanying libraries is free software; you can redistribute it and/or modify it under the terms of the GPL (either version 1, or at your option, any later version) or the Artistic License 2.0. Refer to LICENSE for the full license text. In addition, please see DISCLAIMER.txt for disclaimers of warranty.


and that's the whole page, bestie πŸŽ€ no cap, every single flag and fact is in there. this was perl v5.38.2, dated 2024-08-10, and it's the VM::EC2::SecurityGroup page from the User Contributed Perl Documentation. mwah, go secure those groups πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from