

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

# 在不同 AWS 帳戶中使用 VPCs 中的一致可用區域
<a name="use-consistent-availability-zones-in-vpcs-across-different-aws-accounts"></a>

*Adam Spicer，Amazon Web Services*

## 總結
<a name="use-consistent-availability-zones-in-vpcs-across-different-aws-accounts-summary"></a>

在 Amazon Web Services (AWS) 雲端上，可用區域的名稱可能會因您的 AWS 帳戶和識別其位置的[可用區域 ID (AZ ID)](https://docs.aws.amazon.com/ram/latest/userguide/working-with-az-ids.html) 而有所不同。如果您使用 AWS CloudFormation 建立虛擬私有雲端 (VPCs)，則必須在建立子網路時指定可用區域的名稱或 ID。如果您在多個帳戶中建立 VPCs，則會隨機選取可用區域名稱，這表示子網路在每個帳戶中使用不同的可用區域。 

若要跨帳戶使用相同的可用區域，您必須將每個帳戶中的可用區域名稱對應至相同的可用區域 ID。例如，下圖顯示 `use1-az6` AZ ID 在 `us-east-1a` AWS 帳戶 A 和 AWS 帳戶 Z `us-east-1c`中命名。

![AWS 帳戶 A 中的 use1-az6 AZ ID 名為 us-east-1a，AWS 帳戶 Z 中的 us-east-1c。](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/9954e7f9-d6ce-44bd-af99-0c6bb7cd3cb0/images/23c8a37b-2408-4534-a1e0-bccfa4d7fbe3.png)


 

此模式透過提供跨帳戶、可擴展的解決方案，以便在子網路中使用相同的可用區域，協助確保區域一致性。區域一致性可確保您的跨帳戶網路流量避免跨可用區域網路路徑，這有助於降低資料傳輸成本並降低工作負載之間的網路延遲。

此模式是 AWS CloudFormation [AvailabilityZoneId 屬性](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet.html#cfn-ec2-subnet-availabilityzoneid)的替代方法。

## 先決條件和限制
<a name="use-consistent-availability-zones-in-vpcs-across-different-aws-accounts-prereqs"></a>

**先決條件**
+ 同一 AWS 區域中至少有兩個作用中的 AWS 帳戶。
+ 評估需要多少可用區域來支援區域中的 VPC 需求。
+ 識別並記錄您需要支援的每個可用區域的 AZ ID。如需詳細資訊，請參閱 [AWS Resource Access Manager 文件中的 AWS 資源可用區域 IDs](https://docs.aws.amazon.com/ram/latest/userguide/working-with-az-ids.html)。 
+ 以逗號分隔的有序 AZ IDs 清單。例如，您清單中的第一個可用區域會映射為 `az1`，第二個可用區域會映射為 `az2`，而此映射結構會持續到您的逗號分隔清單完全映射為止。沒有可映射的 AZ IDs 數目上限。 
+ 來自 GitHub [多帳戶可用區域映射](https://github.com/aws-samples/multi-account-az-mapping/)儲存庫`az-mapping.yaml`的檔案，複製到您的本機電腦

## Architecture
<a name="use-consistent-availability-zones-in-vpcs-across-different-aws-accounts-architecture"></a>

下圖顯示部署在 帳戶中並建立 AWS Systems Manager 參數存放區值的架構。當您在帳戶中建立 VPC 時，會使用這些參數存放區值。

![為每個 AZ ID 建立 Systems Manager 參數存放區值並存放 AZ 名稱的工作流程。](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/9954e7f9-d6ce-44bd-af99-0c6bb7cd3cb0/images/f1168464-55f8-4efc-9b28-6a0cda668b9e.png)


該圖顯示以下工作流程：

1. 此模式的解決方案會部署到需要 VPC 區域一致性的所有帳戶。 

1. 解決方案會為每個 AZ ID 建立參數存放區值，並存放新的可用區域名稱。 

1. AWS CloudFormation 範本使用存放在每個參數存放區值中的可用區域名稱，這可確保區域一致性。

下圖顯示使用此模式的解決方案建立 VPC 的工作流程。

 

![工作流程會提交 CloudFormation 範本，以建立具有正確 AZ IDs VPC。](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/9954e7f9-d6ce-44bd-af99-0c6bb7cd3cb0/images/cd859430-ac25-479f-b56a-21da24cddf21.png)


 

該圖顯示以下工作流程：

1. 提交範本以建立 VPC 至 AWS CloudFormation。

1. AWS CloudFormation 會解析每個可用區域的參數存放區值，並傳回每個可用區域 ID 的可用區域名稱。

1. 使用區域一致性所需的正確 AZ IDs 建立 VPC。

部署此模式的解決方案之後，您可以建立參考參數存放區值的子網路。如果您使用 AWS CloudFormation，您可以從下列 YAML 格式的範例程式碼參考可用區域映射參數值：

```
Resources:
    PrivateSubnet1AZ1: 
        Type: AWS::EC2::Subnet 
        Properties: 
            VpcId: !Ref VPC
            CidrBlock: !Ref PrivateSubnetAZ1CIDR
            AvailabilityZone: 
                !Join 
                    - ''
                    - - '{{resolve:ssm:/az-mapping/az1:1}}'
```

此範例程式碼包含在 GitHub [多帳戶可用區域映射](https://github.com/aws-samples/multi-account-az-mapping/)儲存庫的 `vpc-example.yaml `檔案中。它說明如何建立符合參數存放區值的 VPC 和子網路，以實現區域一致性。

**技術堆疊**
+ AWS CloudFormation
+ AWS Lambda
+ AWS Systems Manager 參數存放區

**自動化和擴展**

您可以使用 AWS CloudFormation StackSets 或 Customizations for AWS Control Tower 解決方案，將此模式部署到所有 AWS 帳戶。如需詳細資訊，請參閱 [ AWS CloudFormation 文件中的使用 AWS CloudFormation StackSets](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html)，以及 AWS 解決方案程式庫中的 [AWS Control Tower 自訂](https://aws.amazon.com/solutions/implementations/customizations-for-aws-control-tower/)。 

部署 AWS CloudFormation 範本之後，您可以更新範本以使用參數存放區值，並在管道中或根據您的需求部署 VPCs。 

## 工具
<a name="use-consistent-availability-zones-in-vpcs-across-different-aws-accounts-tools"></a>

**AWS 服務**
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可協助您建立模型並設定 AWS 資源、快速一致地佈建資源，以及在整個生命週期中進行管理。您可以使用範本來描述資源及其相依性，並將它們一起啟動和設定為堆疊，而不是個別管理資源。您可以管理和佈建跨多個 AWS 帳戶和 AWS 區域的堆疊。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一種運算服務，支援執行程式碼，無需佈建或管理伺服器。Lambda 只有在需要時才會執行程式碼，可自動從每天數項請求擴展成每秒數千項請求。只需為使用的運算時間支付費用，一旦未執行程式碼，就會停止計費。
+ [AWS Systems Manager 參數存放區](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html)是 AWS Systems Manager 的功能。它為組態資料管理和秘密管理提供安全的階層式儲存。

**Code**

此模式的程式碼會在 GitHub [多帳戶可用區域映射](https://github.com/aws-samples/multi-account-az-mapping/)儲存庫中提供。

## 史詩
<a name="use-consistent-availability-zones-in-vpcs-across-different-aws-accounts-epics"></a>

### 部署 az-mapping.yaml 檔案
<a name="deploy-the-az-mapping-yaml-file"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 判斷區域所需的可用區域。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/use-consistent-availability-zones-in-vpcs-across-different-aws-accounts.html) | 雲端架構師 | 
| 部署 az-mapping.yaml 檔案。 | 使用 `az-mapping.yaml` 檔案在所有必要的 AWS 帳戶中建立 AWS CloudFormation 堆疊。在 `AZIds` 參數中，使用您先前建立的逗號分隔清單。 <br />我們建議您使用 [AWS CloudFormation StackSets](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html) 或 [Customizations for AWS Control Tower Solution](https://aws.amazon.com/solutions/implementations/customizations-for-aws-control-tower/)。 | 雲端架構師 | 

### 在帳戶中部署 VPCs
<a name="deploy-the-vpcs-in-your-accounts"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 自訂 AWS CloudFormation 範本。 | 當您使用 AWS CloudFormation 建立子網路時，自訂範本以使用您先前建立的參數存放區值。<br />如需範例範本，請參閱 GitHub [多帳戶可用區域映射](https://github.com/aws-samples/multi-account-az-mapping/)儲存庫中的 `vpc-example.yaml` 檔案。 | 雲端架構師 | 
| 部署 VPCs。 | 將自訂的 AWS CloudFormation 範本部署到您的帳戶。區域中的每個 VPC 接著會在子網路使用的可用區域中具有區域一致性 | 雲端架構師 | 

## 相關資源
<a name="use-consistent-availability-zones-in-vpcs-across-different-aws-accounts-resources"></a>
+ [AWS 資源的可用區域 IDs ](https://docs.aws.amazon.com/ram/latest/userguide/working-with-az-ids.html) (AWS Resource Access Manager 文件）
+ [AWS::EC2::Subnet](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet.html) (AWS CloudFormation 文件）