Use AuthorizeSecurityGroupEgress with a CLI - AWS SDK Code Examples

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

Use AuthorizeSecurityGroupEgress with a CLI

The following code examples show how to use AuthorizeSecurityGroupEgress.

CLI
AWS CLI

Example 1: To add a rule that allows outbound traffic to a specific address range

The following authorize-security-group-egress example adds a rule that grants access to the specified address ranges on TCP port 80.

aws ec2 authorize-security-group-egress \ --group-id sg-1234567890abcdef0 \ --ip-permissions 'IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges=[{CidrIp=10.0.0.0/16}]'

Output:

{ "Return": true, "SecurityGroupRules": [ { "SecurityGroupRuleId": "sgr-0b15794cdb17bf29c", "GroupId": "sg-1234567890abcdef0", "GroupOwnerId": "123456789012", "IsEgress": true, "IpProtocol": "tcp", "FromPort": 80, "ToPort": 80, "CidrIpv4": "10.0.0.0/16" } ] }

Example 2: To add a rule that allows outbound traffic to a specific security group

The following authorize-security-group-egress example adds a rule that grants access to the specified security group on TCP port 80.

aws ec2 authorize-security-group-egress \ --group-id sg-1234567890abcdef0 \ --ip-permissions 'IpProtocol=tcp,FromPort=80,ToPort=80,UserIdGroupPairs=[{GroupId=sg-0aad1c26bbeec5c22}]'

Output:

{ "Return": true, "SecurityGroupRules": [ { "SecurityGroupRuleId": "sgr-0b5dd815afcea9cc3", "GroupId": "sg-1234567890abcdef0", "GroupOwnerId": "123456789012", "IsEgress": true, "IpProtocol": "tcp", "FromPort": 80, "ToPort": 80, "ReferencedGroupInfo": { "GroupId": "sg-0aad1c26bbeec5c22", "UserId": "123456789012" } } ] }

For more information, see Security groups in the Amazon VPC User Guide.

PowerShell
Tools for PowerShell

Example 1: This example defines an egress rule for the specified security group for EC2-VPC. The rule grants access to the specified IP address range on TCP port 80. The syntax used by this example requires PowerShell version 3 or higher.

$ip = @{ IpProtocol="tcp"; FromPort="80"; ToPort="80"; IpRanges="203.0.113.0/24" } Grant-EC2SecurityGroupEgress -GroupId sg-12345678 -IpPermission $ip

Example 2: With PowerShell version 2, you must use New-Object to create the IpPermission object.

$ip = New-Object Amazon.EC2.Model.IpPermission $ip.IpProtocol = "tcp" $ip.FromPort = 80 $ip.ToPort = 80 $ip.IpRanges.Add("203.0.113.0/24") Grant-EC2SecurityGroupEgress -GroupId sg-12345678 -IpPermission $ip

Example 3: This example grants access to the specified source security group on TCP port 80.

$ug = New-Object Amazon.EC2.Model.UserIdGroupPair $ug.GroupId = "sg-1a2b3c4d" $ug.UserId = "123456789012" Grant-EC2SecurityGroupEgress -GroupId sg-12345678 -IpPermission @( @{ IpProtocol="tcp"; FromPort="80"; ToPort="80"; UserIdGroupPairs=$ug } )