

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

# 在 Amazon MWAA 上管理您自己的 Amazon VPC 端点
<a name="vpc-endpoint-management"></a>

亚马逊 MWAA 使用亚马逊 VPC 终端节点与设置 Apache Airflow 环境所需的各种 AWS 服务集成。管理自己的端点有两个主要使用案例：

1. 这意味着，当你使用管理 AWS 账户 多个资源和共享资源时，你可以在共享的 Amazon VPC 中创建 Apache Airfl [AWS Organizations](https://aws.amazon.com/organizations/)ow 环境。

1. 您可以通过将权限范围缩小到使用端点的特定资源，从而执行更严格的访问策略。

如果您选择管理自己的 VPC 端点，则需要负责为环境 RDS for PostgreSQL 数据库和环境 Web 服务器创建自己的端点。

有关 Amazon MWAA 如何在云中部署 Apache Airflow 的更多信息，请参阅 [Amazon MWAA 架构图](what-is-mwaa.md#architecture-mwaa)。

**重要**  
Amazon MWAA 不会验证客户管理的终端节点的 IP 地址类型 (`AddressType`) 选择，因此请务必正确指定`AddressType`（有效选项为 IPv4 或）。 IPv6



## 在共享 Amazon VPC 中创建环境
<a name="configure-shared-vpc"></a>

如果您使用管理多个 AWS 账户 共享资源的资源，则可以[AWS Organizations](https://aws.amazon.com/organizations/)将客户管理的 VPC 终端节点与 Amazon MWAA 配合使用，与组织中的其他账户共享环境资源。

配置共享 VPC 访问权限时，拥有主要 Amazon VPC 的账户（*所有者*）将与属于同一组织的其他账户（*参与者*）共享 Amazon MWAA 所需的两个私有子网。共享这些子网的参与者账户可以查看、创建、修改和删除共享 Amazon VPC 中的环境。

假设您有一个账户 `Owner`，该账户充当组织中的 `Root` 账户并拥有 Amazon VPC 资源，此外您还有一个参与者账户 `Participant`，该账户是同一组织的成员。当 `Participant` 在其与 `Owner` 共享的 Amazon VPC 中创建新的 Amazon MWAA 时，Amazon MWAA 将首先创建服务 VPC 资源，然后进入 [https://docs.aws.amazon.com/mwaa/latest/API/API_Environment.html#mwaa-Type-Environment-Status](https://docs.aws.amazon.com/mwaa/latest/API/API_Environment.html#mwaa-Type-Environment-Status) 状态最长 72 小时。

环境状态从 `CREATING` 变为 `PENDING` 后，代表 `Owner` 的主体将创建所需的端点。为此，Amazon MWAA 会在 Amazon MWAA 控制台中列出数据库和 Web 服务器端点。您也可以调用 []() API 操作来获取服务端点。

**注意**  
如果您用于共享资源的 Amazon VPC 是一个私有 Amazon VPC，则仍必须完成[在 Amazon MWAA 上管理对服务特定 Amazon VPC 端点的访问](vpc-vpe-access.md)中所述的步骤。本主题介绍如何设置与集 AWS 成的其他 AWS 服务（例如 Amazon ECR、Amazon ECS 和 Amazon SQS）相关的另一组 Amazon VPC 终端节点。这些服务对于在云中运行和管理 Apache Airflow 环境至关重要。

### 先决条件
<a name="configure-shared-vpc-prerequisites"></a>

在共享 VPC 中创建 Amazon MWAA 环境前，您需要具备以下资源：
+ 答 AWS 账户`Owner`，用作拥有亚马逊 VPC 的账户。
+ 一个作为*根*创建的 [AWS Organizations](https://aws.amazon.com/organizations/) 组织单位 `MyOrganization`。
+ 第二个 AWS 账户，`Participant`，`MyOrganization`用于为创建新环境的参与者账户提供服务。

此外，我们建议您首先自行了解在 Amazon VPC 中共享资源时，[所有者和参与者的责任和权限](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-sharing.html#vpc-share-limitations)。

### 创建 Amazon VPC
<a name="configure-shared-vpc-create-vpc"></a>

首先，创建一个所有者和参与者账户将会共享的新 Amazon VPC：

1. 使用登录控制台`Owner`，然后打开 CloudFormation 控制台。使用以下模板创建一个堆栈。此堆栈预置了多种网络资源，包括 Amazon VPC，以及这两个账户在此场景中将会共享的子网。

   ```
   AWSTemplateFormatVersion: "2010-09-09"                   
   Description: >-
   This template deploys a VPC, with a pair of public and private subnets spread across two Availability Zones. It deploys an internet gateway, with a default route on the public subnets. It deploys a pair of NAT gateways (one in each AZ), and default routes for them in the private subnets.
    Parameters:
      EnvironmentName:
        Description: An environment name that is prefixed to resource names
        Type: String
        Default: mwaa-
      VpcCIDR:
        Description: Please enter the IP range (CIDR notation) for this VPC
        Type: String
        Default: 10.192.0.0/16
      PublicSubnet1CIDR:
        Description: >-
        Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone
        Type: String
        Default: 10.192.10.0/24
      PublicSubnet2CIDR:
        Description: >-
        Please enter the IP range (CIDR notation) for the public subnet in the	second Availability Zone
        Type: String
        Default: 10.192.11.0/24
      PrivateSubnet1CIDR:
        Description: >-
        Please enter the IP range (CIDR notation) for the private subnet in the first Availability Zone
        Type: String
        Default: 10.192.20.0/24
      PrivateSubnet2CIDR:
        Description: >-
        Please enter the IP range (CIDR notation) for the private subnet in the second Availability Zone
        Type: String
        Default: 10.192.21.0/24
    Resources:
      VPC:
        Type: 'AWS::EC2::VPC'
        Properties:
        CidrBlock: !Ref VpcCIDR
        EnableDnsSupport: true
        EnableDnsHostnames: true
        Tags:
          - Key: Name
          Value: !Ref EnvironmentName
      InternetGateway:
        Type: 'AWS::EC2::InternetGateway'
        Properties:
        Tags:
          - Key: Name
          Value: !Ref EnvironmentName
      InternetGatewayAttachment:
        Type: 'AWS::EC2::VPCGatewayAttachment'
        Properties:
          InternetGatewayId: !Ref InternetGateway
          VpcId: !Ref VPC
      PublicSubnet1:
        Type: 'AWS::EC2::Subnet'
        Properties:
          VpcId: !Ref VPC
          AvailabilityZone: !Select 
            - 0
            - !GetAZs ''
          CidrBlock: !Ref PublicSubnet1CIDR
          MapPublicIpOnLaunch: true
          Tags:
            - Key: Name
            Value: !Sub '${EnvironmentName} Public Subnet (AZ1)'
      PublicSubnet2:
        Type: 'AWS::EC2::Subnet'
        Properties:
   						VpcId: !Ref VPC
          AvailabilityZone: !Select 
            - 1
            - !GetAZs ''
          CidrBlock: !Ref PublicSubnet2CIDR
          MapPublicIpOnLaunch: true
          Tags:
            - Key: Name
            Value: !Sub '${EnvironmentName} Public Subnet (AZ2)'
      PrivateSubnet1:
        Type: 'AWS::EC2::Subnet'
        Properties:
          VpcId: !Ref VPC
          AvailabilityZone: !Select 
            - 0
            - !GetAZs ''
          CidrBlock: !Ref PrivateSubnet1CIDR
          MapPublicIpOnLaunch: false
            Tags:
            - Key: Name
              Value: !Sub '${EnvironmentName} Private Subnet (AZ1)'
      PrivateSubnet2:
        Type: 'AWS::EC2::Subnet'
        Properties:
          VpcId: !Ref VPC
          AvailabilityZone: !Select 
            - 1
            - !GetAZs ''
          CidrBlock: !Ref PrivateSubnet2CIDR
          MapPublicIpOnLaunch: false
          Tags:
            - Key: Name
            Value: !Sub '${EnvironmentName} Private Subnet (AZ2)'
      NatGateway1EIP:
        Type: 'AWS::EC2::EIP'
        DependsOn: InternetGatewayAttachment
        Properties:
          Domain: vpc
      NatGateway2EIP:
        Type: 'AWS::EC2::EIP'
        DependsOn: InternetGatewayAttachment
        Properties:
          Domain: vpc
      NatGateway1:
        Type: 'AWS::EC2::NatGateway'
        Properties:
          AllocationId: !GetAtt NatGateway1EIP.AllocationId
          SubnetId: !Ref PublicSubnet1
      NatGateway2:
        Type: 'AWS::EC2::NatGateway'
        Properties:
          AllocationId: !GetAtt NatGateway2EIP.AllocationId
          SubnetId: !Ref PublicSubnet2
      PublicRouteTable:
        Type: 'AWS::EC2::RouteTable'
        Properties:
          VpcId: !Ref VPC
          Tags:
            - Key: Name
            Value: !Sub '${EnvironmentName} Public Routes'
      DefaultPublicRoute:
        Type: 'AWS::EC2::Route'
        DependsOn: InternetGatewayAttachment
        Properties:
          RouteTableId: !Ref PublicRouteTable
          DestinationCidrBlock: 0.0.0.0/0
          GatewayId: !Ref InternetGateway
      PublicSubnet1RouteTableAssociation:
        Type: 'AWS::EC2::SubnetRouteTableAssociation'
        Properties:
          RouteTableId: !Ref PublicRouteTable
          SubnetId: !Ref PublicSubnet1
      PublicSubnet2RouteTableAssociation:
        Type: 'AWS::EC2::SubnetRouteTableAssociation'
        Properties:
          RouteTableId: !Ref PublicRouteTable
          SubnetId: !Ref PublicSubnet2
      PrivateRouteTable1:
        Type: 'AWS::EC2::RouteTable'
        Properties:
          VpcId: !Ref VPC
          Tags:
            - Key: Name
            Value: !Sub '${EnvironmentName} Private Routes (AZ1)'
      DefaultPrivateRoute1:
        Type: 'AWS::EC2::Route'
        Properties:
          RouteTableId: !Ref PrivateRouteTable1
          DestinationCidrBlock: 0.0.0.0/0
          NatGatewayId: !Ref NatGateway1
      PrivateSubnet1RouteTableAssociation:
        Type: 'AWS::EC2::SubnetRouteTableAssociation'
        Properties:
          RouteTableId: !Ref PrivateRouteTable1
          SubnetId: !Ref PrivateSubnet1
      PrivateRouteTable2:
        Type: 'AWS::EC2::RouteTable'
        Properties:
          VpcId: !Ref VPC
          Tags:
            - Key: Name
            Value: !Sub '${EnvironmentName} Private Routes (AZ2)'
      DefaultPrivateRoute2:
        Type: 'AWS::EC2::Route'
        Properties:
          RouteTableId: !Ref PrivateRouteTable2
          DestinationCidrBlock: 0.0.0.0/0
          NatGatewayId: !Ref NatGateway2
      PrivateSubnet2RouteTableAssociation:
        Type: 'AWS::EC2::SubnetRouteTableAssociation'
        Properties:
          RouteTableId: !Ref PrivateRouteTable2
          SubnetId: !Ref PrivateSubnet2
      SecurityGroup:
        Type: 'AWS::EC2::SecurityGroup'
        Properties:
          GroupName: mwaa-security-group
          GroupDescription: Security group with a self-referencing inbound rule.
          VpcId: !Ref VPC
      SecurityGroupIngress:
        Type: 'AWS::EC2::SecurityGroupIngress'
        Properties:
          GroupId: !Ref SecurityGroup
          IpProtocol: '-1'
          SourceSecurityGroupId: !Ref SecurityGroup
          Outputs:
            VPC:
              Description: A reference to the created VPC
              Value: !Ref VPC
              PublicSubnets:
              Description: A list of the public subnets
              Value: !Join 
                - ','
                - - !Ref PublicSubnet1
                - !Ref PublicSubnet2
            PrivateSubnets:
              Description: A list of the private subnets
              Value: !Join 
                - ','
                - - !Ref PrivateSubnet1
                - !Ref PrivateSubnet2
            PublicSubnet1:
              Description: A reference to the public subnet in the 1st Availability Zone
              Value: !Ref PublicSubnet1
            PublicSubnet2:
              Description: A reference to the public subnet in the 2nd Availability Zone
              Value: !Ref PublicSubnet2
            PrivateSubnet1:
              Description: A reference to the private subnet in the 1st Availability Zone
              Value: !Ref PrivateSubnet1
            PrivateSubnet2:
              Description: A reference to the private subnet in the 2nd Availability Zone
              Value: !Ref PrivateSubnet2
            SecurityGroupIngress:
              Description: Security group with self-referencing inbound rule
              Value: !Ref SecurityGroupIngress
   ```

1. 配置新的 Amazon VPC 资源后，导航到 AWS Resource Access Manager 控制台，然后选择**创建资源共享**。

1. 从可与 `Participant` 共享的可用子网列表中选择您在第一步中创建的子网。

### 创建 环境
<a name="configure-shared-vpc-create-mwaa"></a>

完成以下步骤，以使用客户管理型 Amazon VPC 端点创建 Amazon MWAA 环境。

1. 使用 `Participant` 登录并打开 Amazon MWAA 控制台。完成**第一步：指定详细信息**，为您的新环境指定 Amazon S3 存储桶、DAG 文件夹和依赖项等。有关更多信息，请参阅[开始使用](create-environment.md#create-environment-start-details)。

1. 在**配置高级设置**页面的**联网**下，从共享 Amazon VPC 中选择子网。

1. 在**端点管理**下，从下拉列表中选择**客户**。

1. 保持页面上其余选项的默认值，然后在**检查并创建**页面上选择**创建环境**。

环境开始时处于 `CREATING` 状态，然后会变为 `PENDING` 状态。环境处于 `PENDING` 状态时，使用控制台写下**数据库端点服务名称**和 **Web 服务器端点服务名称**（如果您设置了私有 Web 服务器）。

当您使用 Amazon MWAA 控制台创建新环境时，Amazon MWAA 会创建一个新的安全组，其中包含所需的入站和出站规则。记下安全组 ID。

在下一节中，`Owner` 将使用服务端点和安全组 ID 在共享 Amazon VPC 中创建新的 Amazon VPC 端点。

### 创建 Amazon VPC 端点
<a name="configure-shared-vpc-create-endpoints"></a>

完成以下步骤，为环境创建所需的 Amazon VPC 端点。

1. 登录 “ AWS 管理控制台 使用”`Owner`，“打开” [https://console.aws.amazon.com/vpc/](https://console.aws.amazon.com/vpc/)。

1. 从左侧导航面板中选择**安全组**，然后使用以下入站和出站规则在共享 Amazon VPC 中创建新的安全组：    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/mwaa/latest/userguide/vpc-endpoint-management.html)
**警告**  
`Owner` 账户必须在 `Owner` 账户中设置一个安全组，以允许从新环境到共享 Amazon VPC 的流量。为此，您可以在 `Owner` 中创建一个新安全组，也可以编辑现有的安全组。

1. 选择**端点**，然后使用之前步骤中的端点服务名称为环境数据库和 Web 服务器（如果处于私有模式）创建新的端点。选择共享 Amazon VPC、您用于环境的子网以及环境的安全组。

如果成功，环境将从 `PENDING` 状态变回 `CREATING` 状态，最后变为 `AVAILABLE` 状态。如果为 `AVAILABLE` 状态，则您可以登录到 Apache Airflow 控制台。

### 共享 Amazon VPC 问题排查
<a name="configure-shared-vpc-troubleshooting"></a>

可以使用以下参考来解决您在共享 Amazon VPC 中创建环境时遇到的问题。

**环境在 `PENDING` 状态后进入 `CREATE_FAILED` 状态**  
+ 验证 `Owner` 是在使用 [AWS Resource Access Manager](https://docs.aws.amazon.com/ram/latest/userguide/what-is.html) 与 `Participant` 共享子网。
+ 验证数据库和 Web 服务器的 Amazon VPC 端点是在与环境关联的相同子网中创建的。
+ 验证用于端点的安全组允许来自用于环境的安全组的流量。`Owner` 账户会创建将 `Participant` 中的安全组引用为 `123456789012/security-group-id` 的规则。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/mwaa/latest/userguide/vpc-endpoint-management.html)

  有关更多信息，请参阅[所有者和参与者的责任和权限](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-sharing.html#vpc-share-limitations)。

**环境停滞在 `PENDING` 状态**  
验证每个 VPC 端点的状态，以确保其状态为 `Available`。如果使用私有 Web 服务器配置环境，则还必须为该 Web 服务器创建端点。如果环境停止在 `PENDING` 状态，则可能表明缺少私有 Web 服务器端点。

**收到 `The Vpc Endpoint Service 'vpce-service-name' does not exist` 错误**  
如果您看到以下错误，请验证创建端点的账户是否位于拥有该共享 VPC 的 `Owner` 账户中：  

```
ClientError: An error occurred (InvalidServiceName) when calling the CreateVpcEndpoint operation: 
							
The Vpc Endpoint Service 'vpce-service-name' does not exist
```