Working with security groups - Amazon Elastic Compute Cloud

Working with security groups

You can assign a security group to an instance when you launch the instance. When you add or remove rules, those changes are automatically applied to all instances to which you've assigned the security group.

After you launch an instance, you can change its security groups. For more information, see Changing an Instance's Security Groups in the Amazon VPC User Guide.

You can create, view, update, and delete security groups and security group rules using the Amazon EC2 console and the command line tools.

Creating a security group

You can create a custom security group using one of the following methods. You must specify the VPC for which you're creating the security group.

New console

To create a security group

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. Choose Create security group.

  4. In the Basic details section, do the following.

    1. Enter a descriptive name and brief description for the security group. The name and description can be up to 255 characters long, and they can include a-z, A-Z, 0-9, spaces, and ._-:/()#,@[]+=&;{}!$*.

    2. For VPC, choose the VPC in which to create the security group. The security group can only be used in the VPC in which it is created.

  5. You can add security group rules now, or you can add them at any time after you have created the security group. For more information about adding security group rules, see Adding rules to a security group.

  6. Choose Create.

Old console

To create a security group

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. Choose Create Security Group.

  4. Specify a name and description for the security group.

  5. For VPC, choose the ID of the VPC.

  6. You can start adding rules, or you can choose Create to create the security group now (you can always add rules later). For more information about adding rules, see Adding rules to a security group.

Command line

To create a security group

Use one of the following commands:

Copying a security group

You can create a new security group by creating a copy of an existing one. When you copy a security group, the copy is created with the same inbound and outbound rules as the original security group. If the original security group is in a VPC, the copy is created in the same VPC unless you specify a different one.

The copy receives a new unique security group ID and you must give it a name. You can also add a description.

You can't copy a security group from one Region to another Region.

You can create a copy of a security group using one of the following methods.

New console

To copy a security group

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. Select the security group to copy and choose Actions, Copy to new security group.

  4. Specify a name and optional description, and change the VPC and security group rules if needed.

  5. Choose Create.

Old console

To copy a security group

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. Select the security group you want to copy, choose Actions, Copy to new.

  4. The Create Security Group dialog opens, and is populated with the rules from the existing security group. Specify a name and description for your new security group. For VPC, choose the ID of the VPC. When you are done, choose Create.

Viewing your security groups

You can view information about your security groups using one of the following methods.

New console

To view your security groups

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. Your security groups are listed. To view the details for a specific security group, including its inbound and outbound rules, choose its ID in the Security group ID column.

Old console

To view your security groups

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. (Optional) Select VPC ID from the filter list, then choose the ID of the VPC.

  4. Select a security group. General information is displayed on the Description tab, inbound rules on the Inbound tab, outbound rules on the Outbound tab, and tags on the Tags tab.

Command line

To view your security groups

Use one of the following commands.

Adding rules to a security group

When you add a rule to a security group, the new rule is automatically applied to any instances that are associated with the security group. There might be a short delay before the rule is applied. For more information about choosing security group rules for specific types of access, see Security group rules reference. For security group rule quotas, see Amazon VPC quotas in the Amazon VPC User Guide.

You can add rules to a security group using one of the following methods.

New console

To add an inbound rule to a security group

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. In the list, select the security group and choose Actions, Edit inbound rules.

  4. Choose Add rule and do the following.

    1. For Type, choose the type of protocol to allow.

      • If you choose a custom TCP or UDP protocol, you must manually enter the port range to allow.

      • If you choose a custom ICMP protocol, you must choose the ICMP type name from Protocol, and, if applicable, the code name from Port range.

      • If you choose any other type, the protocol and port range are configured automatically.

    2. For Source, do one of the following.

      • Choose Custom and then enter an IP address in CIDR notation, a CIDR block, another security group, or a prefix list from which to allow inbound traffic.

      • Choose Anywhere to allow all inbound traffic of the specified protocol to reach your instance. This option automatically adds the 0.0.0.0/0 IPv4 CIDR block as an allowed source. This is acceptable for a short time in a test environment, but it's unsafe for production environments. In production, authorize only a specific IP address or range of addresses to access your instance.

        If your security group is in a VPC that's enabled for IPv6, this option automatically adds a second rule for IPv6 traffic (::/0).

      • Choose My IP to allow inbound traffic from only your local computer's public IPv4 address.

    3. For Description, optionally specify a brief description for the rule.

  5. Choose Preview changes, Save rules.

To add an outbound rule to a security group

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. In the list, select the security group and choose Actions, Edit outbound rules.

  4. Choose Add rule and do the following.

    1. For Type, choose the type of protocol to allow.

      • If you choose a custom TCP or UDP protocol, you must manually enter the port range to allow.

      • If you choose a custom ICMP protocol, you must choose the ICMP type name from Protocol, and, if applicable, the code name from Port range.

      • If you choose any other type, the protocol and port range are configured automatically.

    2. For Destination, do one of the following.

      • Choose Custom and then enter an IP address in CIDR notation, a CIDR block, another security group, or a prefix list for which to allow outbound traffic.

      • Choose Anywhere to allow outbound traffic to all IP addresses. This option automatically adds the 0.0.0.0/0 IPv4 CIDR block as an allowed source.

        If your security group is in a VPC that's enabled for IPv6, this option automatically adds a second rule for IPv6 traffic (::/0).

      • Choose My IP to allow outbound traffic only to your local computer's public IPv4 address.

    3. For Description, optionally specify a brief description for the rule.

  5. Choose Preview changes, Confirm.

Old console

To add rules to a security group

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups and select the security group.

  3. On the Inbound tab, choose Edit.

  4. In the dialog, choose Add Rule and do the following:

    • For Type, select the protocol.

    • If you select a custom TCP or UDP protocol, specify the port range in Port Range.

    • If you select a custom ICMP protocol, choose the ICMP type name from Protocol, and, if applicable, the code name from Port Range.

    • For Source, choose one of the following:

      • Custom: in the provided field, you must specify an IP address in CIDR notation, a CIDR block, or another security group.

      • Anywhere: automatically adds the 0.0.0.0/0 IPv4 CIDR block. This option enables all traffic of the specified type to reach your instance. This is acceptable for a short time in a test environment, but it's unsafe for production environments. In production, authorize only a specific IP address or range of addresses to access your instance.

        If your security group is in a VPC that's enabled for IPv6, the Anywhere option creates two rules—one for IPv4 traffic (0.0.0.0/0) and one for IPv6 traffic (::/0).

      • My IP: automatically adds the public IPv4 address of your local computer.

    • For Description, you can optionally specify a description for the rule.

    For more information about the types of rules that you can add, see Security group rules reference.

  5. Choose Save.

  6. You can also specify outbound rules. On the Outbound tab, choose Edit, Add Rule, and do the following:

    • For Type, select the protocol.

    • If you select a custom TCP or UDP protocol, specify the port range in Port Range.

    • If you select a custom ICMP protocol, choose the ICMP type name from Protocol, and, if applicable, the code name from Port Range.

    • For Destination, choose one of the following:

      • Custom: in the provided field, you must specify an IP address in CIDR notation, a CIDR block, or another security group.

      • Anywhere: automatically adds the 0.0.0.0/0 IPv4 CIDR block. This option enables outbound traffic to all IP addresses.

        If your security group is in a VPC that's enabled for IPv6, the Anywhere option creates two rules—one for IPv4 traffic (0.0.0.0/0) and one for IPv6 traffic (::/0).

      • My IP: automatically adds the IP address of your local computer.

    • For Description, you can optionally specify a description for the rule.

  7. Choose Save.

Command line

To add rules to a security group

Use one of the following commands.

To add one or more egress rules to a security group

Use one of the following commands.

Updating Security Group Rules

You can update a security group rule using one of the following methods.

New console

When you modify the protocol, port range, or source or destination of an existing security group rule using the console, the console deletes the existing rule and adds a new one for you.

To update a security group rule

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. Select the security group to update, choose Actions, and then choose Edit inbound rules to update a rule for inbound traffic or Edit outbound rules to update a rule for outbound traffic.

  4. Update the rule as required and then choose Preview changes, Confirm.

Old console

When you modify the protocol, port range, or source or destination of an existing security group rule using the console, the console deletes the existing rule and adds a new one for you.

To update a security group rule

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. Select the security group to update, and choose the Inbound tab to update a rule for inbound traffic or the Outbound tab to update a rule for outbound traffic.

  4. Choose Edit.

  5. Modify the rule entry as required and choose Save.

Command line

You cannot modify the protocol, port range, or source or destination of an existing rule using the Amazon EC2 API or a command line tools. Instead, you must delete the existing rule and add a new rule. You can, however, update the description of an existing rule.

To update the description for an existing inbound rule

Use one of the following commands.

To update the description for an existing outbound rule

Use one of the following commands.

Deleting rules from a security group

When you delete a rule from a security group, the change is automatically applied to any instances associated with the security group.

You can delete rules from a security group using one of the following methods.

New console

To delete a security group rule

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. Select the security group to update, choose Actions, and then choose Edit inbound rules to remove an inbound rule or Edit outbound rules to remove an outbound rule.

  4. Choose the remove button to the right of the rule to delete.

  5. Choose Preview changes, Confirm.

Old console

To delete a security group rule

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. Select a security group.

  4. On the Inbound tab (for inbound rules) or Outbound tab (for outbound rules), choose Edit. Choose Delete (a cross icon) next to each rule to delete.

  5. Choose Save.

Command line

To remove one or more ingress rules from a security group

Use one of the following commands.

To remove one or more egress rules from a security group

Use one of the following commands.

Deleting a security group

You can't delete a security group that is associated with an instance. You can't delete the default security group. You can't delete a security group that is referenced by a rule in another security group in the same VPC. If your security group is referenced by one of its own rules, you must delete the rule before you can delete the security group.

New console

To delete a security group

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. Select the security group to delete and choose Actions, Delete security group, Delete.

Old console

To delete a security group

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Security Groups.

  3. Select a security group and choose Actions, Delete Security Group.

  4. Choose Yes, Delete.

Command line

To delete a security group

Use one of the following commands.