Configure security groups for your Classic Load Balancer - Elastic Load Balancing

Configure security groups for your Classic Load Balancer

A security group acts as a firewall that controls the traffic allowed to and from one or more instances. When you launch an EC2 instance, you can associate one or more security groups with the instance. For each security group, you add one or more rules to allow traffic. You can modify the rules for a security group at any time; the new rules are automatically applied to all instances associated with the security group. For more information, see Amazon EC2 security groups in the Amazon EC2 User Guide for Linux Instances.

Security groups for load balancers in a VPC

When you use the AWS Management Console to create a load balancer in a VPC, you can choose an existing security group for the VPC or create a new security group for the VPC. If you choose an existing security group, it must allow traffic in both directions to the listener and health check ports for the load balancer. If you choose to create a security group, the console automatically adds rules to allow all traffic on these ports.

[Nondefault VPC] If you use the AWS CLI or API create a load balancer in a nondefault VPC, but you don't specify a security group, your load balancer is automatically associated with the default security group for the VPC.

[Default VPC] If you use the AWS CLI or API to create a load balancer in your default VPC, you can't choose an existing security group for your load balancer. Instead, Elastic Load Balancing provides a security group with rules to allow all traffic on the ports specified for the load balancer. Elastic Load Balancing creates only one such security group per AWS account, with a name of the form default_elb_id (for example, default_elb_fc5fbed3-0405-3b7d-a328-ea290EXAMPLE). Subsequent load balancers that you create in the default VPC also use this security group. Be sure to review the security group rules to ensure that they allow traffic on the listener and health check ports for the new load balancer. When you delete your load balancer, this security group is not deleted automatically.

If you add a listener to an existing load balancer, you must review your security groups to ensure they allow traffic on the new listener port in both directions.

The security groups for your load balancers must allow them to communicate with your instances. The recommended rules depend on the type of load balancer (internet-facing or internal).

The following table shows the recommended rules for an internet-facing load balancer.

Inbound
Source Protocol Port Range Comment

0.0.0.0/0

TCP

listener

Allow all inbound traffic on the load balancer listener port

Outbound

Destination Protocol Port Range Comment

instance security group

TCP

instance listener

Allow outbound traffic to instances on the instance listener port

instance security group

TCP

health check

Allow outbound traffic to instances on the health check port

The following table shows the recommended rules for an internal load balancer.

Inbound
Source Protocol Port Range Comment

VPC CIDR

TCP

listener

Allow inbound traffic from the VPC CIDR on the load balancer listener port

Outbound

Destination Protocol Port Range Comment

instance security group

TCP

instance listener

Allow outbound traffic to instances on the instance listener port

instance security group

TCP

health check

Allow outbound traffic to instances on the health check port

Manage security groups using the console

Use the following procedure to change the security groups associated with your load balancer in a VPC.

New EC2 experience
To update a security group assigned to your load balancer
  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation pane, under Load Balancing, choose Load Balancers.

  3. Choose the name of the load balancer to open its detail page.

  4. On the Security tab, choose Edit.

  5. On the Edit security groups page, Under Security groupsadd or remove security groups as needed.

    You can add up to five security groups.

  6. When you are finished, choose Save changes.

Old EC2 experience
To update a security group assigned to your load balancer
  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation pane, under Load Balancing, choose Load Balancers.

  3. Select your load balancer.

  4. On the Description tab, choose Edit security groups.

  5. On the Edit security groups page, select or clear security groups as needed.

  6. When you are finished, choose Save.

Manage security groups using the AWS CLI

Use the following apply-security-groups-to-load-balancer command to associate a security group with a load balancer in a VPC. The specified security groups override the previously associated security groups.

aws elb apply-security-groups-to-load-balancer --load-balancer-name my-loadbalancer --security-groups sg-53fae93f

The following is an example response:

{ "SecurityGroups": [ "sg-53fae93f" ] }

Security groups for instances in a VPC

The security groups for your instances must allow them to communicate with the load balancer. The following table shows the recommended rules.

Inbound
Source Protocol Port Range Comment

load balancer security group

TCP

instance listener

Allow traffic from the load balancer on the instance listener port

load balancer security group

TCP

health check

Allow traffic from the load balancer on the health check port

We also recommend that you allow inbound ICMP traffic to support Path MTU Discovery. For more information, see Path MTU Discovery in the Amazon EC2 User Guide for Linux Instances.

Network ACLs for load balancers in a VPC

The default network access control list (ACL) for the VPC allows all inbound and outbound traffic. If you create custom network ACLs, you must add rules that allow the load balancer and instances to communicate.

The recommended rules for the subnet for your load balancer depend on the type of load balancer (internet-facing or internal).

The following are the recommended rules for an internet-facing load balancer.

Inbound
Source Protocol Port Comment

0.0.0.0/0

TCP

listener

Allow all inbound traffic on the load balancer listener port

VPC CIDR

TCP

1024-65535

Allow inbound traffic from the VPC CIDR on the ephemeral ports

Outbound

Destination Protocol Port Comment

VPC CIDR

TCP

instance listener

Allow all outbound traffic on the instance listener port

VPC CIDR

TCP

health check

Allow all outbound traffic on the health check port

0.0.0.0/0

TCP

1024-65535

Allow all outbound traffic on the ephemeral ports

The following are the recommended rules for an internal load balancer.

Inbound
Source Protocol Port Comment

VPC CIDR

TCP

listener

Allow inbound traffic from the VPC CIDR on the load balancer listener port

VPC CIDR

TCP

1024-65535

Allow inbound traffic from the VPC CIDR on the ephemeral ports

Outbound

Destination Protocol Port Comment

VPC CIDR

TCP

instance listener

Allow outbound traffic to the VPC CIDR on the instance listener port

VPC CIDR

TCP

health check

Allow outbound traffic to the VPC CIDR on the health check port

VPC CIDR

TCP

1024-65535

Allow outbound traffic to the VPC CIDR on the ephemeral ports

The recommended rules for the subnet for your instances depend on whether the subnet is private or public. The following rules are for a private subnet. If your instances are in a public subnet, change the source and destination from the CIDR of the VPC to 0.0.0.0/0.

Inbound
Source Protocol Port Comment

VPC CIDR

TCP

instance listener

Allow inbound traffic from the VPC CIDR on the instance listener port

VPC CIDR

TCP

health check

Allow inbound traffic from the VPC CIDR on the health check port

Outbound

Destination Protocol Port Comment

VPC CIDR

TCP

1024-65535

Allow outbound traffic to the VPC CIDR on the ephemeral ports