

# 更新您的安全组以引用对等安全组
<a name="vpc-peering-security-groups"></a>

您可以更新 VPC 安全组的入站或出站规则以引用对等 VPC 的安全组。此操作将允许流量流入和流出与对等的 VPC 中的已引用安全组关联的实例。

**注意**  
不会在控制台中显示对等 VPC 中的安全组供您选择。

**要求**
+ 要在对等 VPC 中引用安全组，VPC 对等连接必须处于`active`状态。
+ 对等 VPC 可以是您的账户中的 VPC，也可以是另一AWS账户中的 VPC。要引用位于其他 AWS 账户但属于相同区域的安全组，请将账号与安全组的 ID 一起包括在内。例如 `123456789012/sg-1a2b3c4d`。
+ 您无法引用位于不同区域内的对等 VPC 的安全组。而是使用对等 VPC 的 CIDR 块。
+ 如果您将路由配置为通过中间设备在不同子网中的两个实例之间转发流量，则必须确保这两个实例的安全组允许流量在实例之间流动。每个实例的安全组必须引用另一个实例的私有 IP 地址或包含另一个实例的子网的 CIDR 范围作为源。如果您引用另一个实例的安全组作为源，则安全组不允许流量在实例之间流动。

**使用控制台更新安全组规则**

1. 通过以下网址打开 Amazon VPC 控制台：[https://console.aws.amazon.com/vpc/](https://console.aws.amazon.com/vpc/)。

1. 在导航窗格中，选择**安全组**。

1. 选择安全组，然后执行下列操作之一：
   + 要修改入站规则，请选择**操作**、**编辑入站规则**。
   + 要修改出站规则，请选择**操作**、**编辑出站规则**。

1. 要添加规则，请选择**添加规则**，然后指定类型、协议和端口范围。对于**源**（入站规则）或**目的地**（出站规则），请执行下列操作之一：
   + 对于同一账户和区域中的对等 VPC，请输入安全组的 ID。
   + 对于位于不同账户但位于相同区域的对等 VPC，请输入账户 ID 和安全组 ID，中间用正斜杠分隔（例如，`123456789012/sg-1a2b3c4d`）。
   + 对于位于不同区域中的对等 VPC，输入对等 VPC 的 CIDR 块。

1. 要编辑现有的规则，请更改其值（例如，源或描述）。

1. 要删除规则，请选择该规则旁的**删除**。

1. 选择**保存规则**。

**使用命令行更新入站规则**
+ [authorize-security-group-ingress](https://docs.aws.amazon.com/cli/latest/reference/ec2/authorize-security-group-ingress.html) 和 [revoke-security-group-ingress](https://docs.aws.amazon.com/cli/latest/reference/ec2/revoke-security-group-ingress.html) (AWS CLI)
+ [Grant-EC2SecurityGroupIngress](https://docs.aws.amazon.com/powershell/latest/reference/items/Grant-EC2SecurityGroupIngress.html) 和 [Revoke-EC2SecurityGroupIngress](https://docs.aws.amazon.com/powershell/latest/reference/items/Revoke-EC2SecurityGroupIngress.html) (AWS Tools for Windows PowerShell)

例如，要更新安全组 `sg-aaaa1111` 以允许通过 HTTP 从对等 VPC 中的 `sg-bbbb2222` 进行入站访问，请使用以下命令。如果对等 VPC 位于同一区域但位于不同账户，则添加 `--group-owner` *aws-account-id*。

```
aws ec2 authorize-security-group-ingress --group-id sg-aaaa1111 --protocol tcp --port 80 --source-group sg-bbbb2222
```

**使用命令行更新出站规则**
+ [authorize-security-group-egress](https://docs.aws.amazon.com/cli/latest/reference/ec2/authorize-security-group-egress.html) 和 [revoke-security-group-egress](https://docs.aws.amazon.com/cli/latest/reference/ec2/revoke-security-group-egress.html) (AWS CLI)
+ [Grant-EC2SecurityGroupEgress](https://docs.aws.amazon.com/powershell/latest/reference/items/Grant-EC2SecurityGroupEgress.html) 和 [Revoke-EC2SecurityGroupEgress](https://docs.aws.amazon.com/powershell/latest/reference/items/Revoke-EC2SecurityGroupEgress.html) (AWS Tools for Windows PowerShell)

在更新安全组规则后，使用 [describe-security-groups](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-security-groups.html) 命令来查看安全组规则中引用的安全组。

## 确定引用的安全组
<a name="vpc-peering-referenced-groups"></a>

要确定在对等 VPC 中的安全组规则中是否正在引用您的安全组，请对账户中的一个或多个安全组使用以下命令之一。
+ [describe-security-group-references](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-security-group-references.html) (AWS CLI)
+ [Get-EC2SecurityGroupReference](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2SecurityGroupReference.html) (AWS Tools for Windows PowerShell)

在以下示例中，响应表示安全组 `sg-bbbb2222` 正在被 VPC `vpc-aaaaaaaa` 中的安全组引用：

```
aws ec2 describe-security-group-references --group-id sg-bbbb2222
```

```
{    
  "SecurityGroupsReferenceSet": [
    {
      "ReferencingVpcId": "vpc-aaaaaaaa",
      "GroupId": "sg-bbbb2222",
      "VpcPeeringConnectionId": "pcx-b04deed9"       
    }   
  ]
}
```

如果删除 VPC 对等连接，或者对等 VPC 的拥有者删除引用的安全组，则安全组规则将过时。

## 查看并删除过时安全组规则
<a name="vpc-peering-stale-groups"></a>

过时的安全组规则是一种引用相同 VPC 或对等 VPC 中的已删除安全组的规则，或引用 VPC 对等连接已删除的对等 VPC 中的安全组的规则。系统不会从您的安全组中自动移除过时的安全组规则，您必须手动删除它们。当安全组规则因删除了 VPC 对等连接而变得过时的时候，如果您使用相同的 VPC 创建了新的 VPC 对等连接，则安全组规则将不再标记为过时。

您可以使用 Amazon VPC 控制台查看和删除某个 VPC 的过时安全组规则。

**查看和删除过时安全组规则**

1. 通过以下网址打开 Amazon VPC 控制台：[https://console.aws.amazon.com/vpc/](https://console.aws.amazon.com/vpc/)。

1. 在导航窗格中，选择 **Security Groups**（安全组）。

1. 选择 **Actions (操作)**、**Manage stale rules (管理过时规则)**。

1. 对于 **VPC**，请选择具有过时规则的 VPC。

1. 选择**编辑**。

1. 选择您希望删除的规则旁边的 **Delete**（删除）按钮。选择 **Preview changes (预览更改)**，然后选择 **Save rules (保存规则)**。

**使用命令行描述您的过时的安全组规则**
+ [describe-stale-security-groups](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-stale-security-groups.html) (AWS CLI)
+ [Get-EC2StaleSecurityGroup](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2StaleSecurityGroup.html) (AWS Tools for Windows PowerShell)

在以下示例中，VPC A `(vpc-aaaaaaaa`) 和 VPC B 是对等的，并且已删除 VPC 对等连接。您在 VPC A 中的安全组 `sg-aaaa1111` 引用了 VPC B 中的 `sg-bbbb2222`。在为您的 VPC 运行 `describe-stale-security-groups` 命令时，响应表示安全组 `sg-aaaa1111` 具有引用了 `sg-bbbb2222` 的过时 SSH 规则。

```
aws ec2 describe-stale-security-groups --vpc-id vpc-aaaaaaaa
```

```
{
    "StaleSecurityGroupSet": [
        {
            "VpcId": "vpc-aaaaaaaa", 
            "StaleIpPermissionsEgress": [], 
            "GroupName": "Access1", 
            "StaleIpPermissions": [
                {
                    "ToPort": 22, 
                    "FromPort": 22, 
                    "UserIdGroupPairs": [
                        {
                            "VpcId": "vpc-bbbbbbbb", 
                            "PeeringStatus": "deleted", 
                            "UserId": "123456789101", 
                            "GroupName": "Prod1", 
                            "VpcPeeringConnectionId": "pcx-b04deed9", 
                            "GroupId": "sg-bbbb2222"
                        }
                    ], 
                    "IpProtocol": "tcp"
                }
            ], 
            "GroupId": "sg-aaaa1111", 
            "Description": "Reference remote SG"
        }
    ]
}
```

找到过时的安全组规则后，您可以使用 [revoke-security-group-ingress](https://docs.aws.amazon.com/cli/latest/reference/ec2/revoke-security-group-ingress.html) 或 [revoke-security-group-egress](https://docs.aws.amazon.com/cli/latest/reference/ec2/revoke-security-group-egress.html) 命令将其删除。