

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 搭配使用 `RevokeSecurityGroupEgress` 與 CLI
<a name="example_ec2_RevokeSecurityGroupEgress_section"></a>

下列程式碼範例示範如何使用 `RevokeSecurityGroupEgress`。

------
#### [ CLI ]

**AWS CLI**  
**範例 1：移除允許傳出流量至特定位址範圍的規則**  
下列 `revoke-security-group-egress` 範例命令會移除將指定之位址範圍存取權授予 TCP 連接埠 80 的規則。  

```
aws ec2 revoke-security-group-egress \
    --group-id sg-026c12253ce15eff7 \
    --ip-permissions [{IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges=[{CidrIp=10.0.0.0/16}]
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*Amazon EC2 使用者指南*》中的[安全群組](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-security-groups.html)。  
**範例 2：移除允許傳出流量至特定安全群組的規則**  
下列 `revoke-security-group-egress` 範例命令會移除將指定之安全群組存取權授予 TCP 連接埠 80 的規則。  

```
aws ec2 revoke-security-group-egress \
    --group-id sg-026c12253ce15eff7 \
    --ip-permissions '[{"IpProtocol": "tcp", "FromPort": 443, "ToPort": 443,"UserIdGroupPairs": [{"GroupId": "sg-06df23a01ff2df86d"}]}]'
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*Amazon EC2 使用者指南*》中的[安全群組](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-security-groups.html)。  
+  如需 API 詳細資訊，請參閱《*AWS CLI 命令參考*》中的 [RevokeSecurityGroupEgress](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/revoke-security-group-egress.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：此範例會移除 EC2-VPC 的指定安全群組的規則。這會撤銷 TCP 連接埠 80 上，指定之 IP 位址範圍的存取權。此範例使用的語法需要 PowerShell 版本 3 或更高版本。**  

```
$ip = @{ IpProtocol="tcp"; FromPort="80"; ToPort="80"; IpRanges="203.0.113.0/24" }
Revoke-EC2SecurityGroupEgress -GroupId sg-12345678 -IpPermission $ip
```
**範例 2：使用 PowerShell 版本 2 時，必須使用 New-Object 建立 IpPermission 物件。**  

```
$ip = New-Object Amazon.EC2.Model.IpPermission
$ip.IpProtocol = "tcp"
$ip.FromPort = 80
$ip.ToPort = 80
$ip.IpRanges.Add("203.0.113.0/24")
Revoke-EC2SecurityGroupEgress -GroupId sg-12345678 -IpPermission $ip
```
**範例 3：此範例會撤銷對 TCP 連接埠 80 上，指定之來源安全群組的存取權。**  

```
$ug = New-Object Amazon.EC2.Model.UserIdGroupPair
$ug.GroupId = "sg-1a2b3c4d"
$ug.UserId = "123456789012"
Revoke-EC2SecurityGroupEgress -GroupId sg-12345678 -IpPermission @( @{ IpProtocol="tcp"; FromPort="80"; ToPort="80"; UserIdGroupPairs=$ug } )
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [RevokeSecurityGroupEgress](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：此範例會移除 EC2-VPC 的指定安全群組的規則。這會撤銷 TCP 連接埠 80 上，指定之 IP 位址範圍的存取權。此範例使用的語法需要 PowerShell 版本 3 或更高版本。**  

```
$ip = @{ IpProtocol="tcp"; FromPort="80"; ToPort="80"; IpRanges="203.0.113.0/24" }
Revoke-EC2SecurityGroupEgress -GroupId sg-12345678 -IpPermission $ip
```
**範例 2：使用 PowerShell 版本 2 時，必須使用 New-Object 建立 IpPermission 物件。**  

```
$ip = New-Object Amazon.EC2.Model.IpPermission
$ip.IpProtocol = "tcp"
$ip.FromPort = 80
$ip.ToPort = 80
$ip.IpRanges.Add("203.0.113.0/24")
Revoke-EC2SecurityGroupEgress -GroupId sg-12345678 -IpPermission $ip
```
**範例 3：此範例會撤銷對 TCP 連接埠 80 上，指定之來源安全群組的存取權。**  

```
$ug = New-Object Amazon.EC2.Model.UserIdGroupPair
$ug.GroupId = "sg-1a2b3c4d"
$ug.UserId = "123456789012"
Revoke-EC2SecurityGroupEgress -GroupId sg-12345678 -IpPermission @( @{ IpProtocol="tcp"; FromPort="80"; ToPort="80"; UserIdGroupPairs=$ug } )
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [RevokeSecurityGroupEgress](https://docs.aws.amazon.com/powershell/v5/reference)。

------

如需 AWS SDK 開發人員指南和程式碼範例的完整清單，請參閱 [使用 SDK 建立 Amazon EC2 資源 AWS](sdk-general-information-section.md)。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。