Use ApplySecurityGroupsToLoadBalancer with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use ApplySecurityGroupsToLoadBalancer with an AWS SDK or CLI

The following code examples show how to use ApplySecurityGroupsToLoadBalancer.

CLI
AWS CLI

To associate a security group with a load balancer in a VPC

This example associates a security group with the specified load balancer in a VPC.

Command:

<userinput>aws elb apply-security-groups-to-load-balancer --load-balancer-name <replaceable>my-load-balancer</replaceable> --security-groups <replaceable>sg-fc448899</replaceable></userinput>

Output:

{ "SecurityGroups": [ "sg-fc448899" ] }
PowerShell
Tools for PowerShell

Example 1: This example replaces the current security group for the specified load balancer with the specified security group.

Join-ELBSecurityGroupToLoadBalancer -LoadBalancerName my-load-balancer -SecurityGroup sg-87654321

Output:

sg-87654321

Example 2: To keep the current security group and specify an additional security group, specify both the existing and new security groups.

Join-ELBSecurityGroupToLoadBalancer -LoadBalancerName my-load-balancer -SecurityGroup @("sg-12345678", "sg-87654321")

Output:

sg-12345678 sg-87654321