

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

# 更新您的安全群組以參考對等安全群組
<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. 在 [https://console.aws.amazon.com/vpc/](https://console.aws.amazon.com/vpc/) 開啟 Amazon 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. 在 [https://console.aws.amazon.com/vpc/](https://console.aws.amazon.com/vpc/) 開啟 Amazon VPC 主控台。

1. 在導覽窗格中，選擇 **Security groups** (安全群組)。

1. 選擇 **Actions (動作)**、**Manage stale rules (管理過時規則)**。

1. 針對 **VPC**，選擇具有過時規則的 VPC。

1. 選擇 **Edit** (編輯)。

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) 命令，來將其刪除。