RevokeSecurityGroupIngress与 AWS SDK 或 CLI 配合使用 - Amazon Elastic Compute Cloud

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

RevokeSecurityGroupIngress与 AWS SDK 或 CLI 配合使用

以下代码示例演示如何使用 RevokeSecurityGroupIngress

CLI
AWS CLI

示例 1:从安全组中移除规则

以下revoke-security-group-ingress示例从默认 VPC 的指定安全组中删除203.0.113.0/24地址范围的 TCP 端口 22 访问权限。

aws ec2 revoke-security-group-ingress \ --group-name mySecurityGroup --protocol tcp \ --port 22 \ --cidr 203.0.113.0/24

如果成功执行此命令,则不会产生任何输出。

有关更多信息,请参阅 Amazon EC2 用户指南中的安全组

示例 2:使用 IP 权限集删除规则

以下revoke-security-group-ingress示例使用ip-permissions参数删除允许 ICMP 消息的入站规则Destination Unreachable: Fragmentation Needed and Don't Fragment was Set(类型 3,代码 4)。

aws ec2 revoke-security-group-ingress \ --group-id sg-026c12253ce15eff7 \ --ip-permissions IpProtocol=icmp,FromPort=3,ToPort=4,IpRanges=[{CidrIp=0.0.0.0/0}]

如果成功执行此命令,则不会产生任何输出。

有关更多信息,请参阅 Amazon EC2 用户指南中的安全组

PowerShell
用于 PowerShell

示例 1:此示例从 EC2-VPC 的指定安全组的指定地址范围内撤消对 TCP 端口 22 的访问权限。请注意,您必须使用安全组 ID 而不是安全组名称来识别 EC2-VPC 的安全组。此示例使用的语法需要 PowerShell 版本 3 或更高版本。

$ip = @{ IpProtocol="tcp"; FromPort="22"; ToPort="22"; IpRanges="203.0.113.0/24" } Revoke-EC2SecurityGroupIngress -GroupId sg-12345678 -IpPermission $ip

示例 2:在 PowerShell 版本 2 中,必须使用 New-Object 来创建对象。 IpPermission

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

示例 3:此示例从 EC2-Classic 的指定安全组的指定地址范围内撤消对 TCP 端口 22 的访问权限。此示例使用的语法需要 PowerShell 版本 3 或更高版本。

$ip = @{ IpProtocol="tcp"; FromPort="22"; ToPort="22"; IpRanges="203.0.113.0/24" } Revoke-EC2SecurityGroupIngress -GroupName "my-security-group" -IpPermission $ip

示例 4:在 PowerShell 版本 2 中,必须使用 New-Object 来创建对象。 IpPermission

$ip = New-Object Amazon.EC2.Model.IpPermission $ip.IpProtocol = "tcp" $ip.FromPort = 22 $ip.ToPort = 22 $ip.IpRanges.Add("203.0.113.0/24") Revoke-EC2SecurityGroupIngress -GroupName "my-security-group" -IpPermission $ip

有关 S AWS DK 开发者指南和代码示例的完整列表,请参阅使用 AWS 软件开发工具包创建 Amazon EC2 资源。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。