

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Apache Airflow ウェブサーバーのカスタムドメインの設定
<a name="configuring-custom-domain"></a>

Amazon Managed Workflows for Apache Airflow (Amazon MWAA) を使用すると、マネージド Apache Airflow ウェブサーバーのカスタムドメインを設定できます。カスタムドメインを使用すると、Apache Airflow UI、Apache Airflow CLI、または Apache Airflow ウェブサーバーを使用して、環境の Amazon MWAA マネージド Apache Airflow ウェブサーバーにアクセスできます。

**注記**  
カスタムドメインは、インターネットアクセスのないプライベートウェブサーバーでのみ使用できます。

**Amazon MWAA のカスタムドメインのユースケース**

1. AWS のクラウドアプリケーション間でウェブサーバードメインを共有する — カスタムドメインを使用すると、生成されたサービスドメイン名の代わりに、ウェブサーバーにアクセスするためのユーザーフレンドリーな URL を定義できます。このカスタムドメインを保存し、アプリケーション内の環境変数として共有できます。

1. プライベートウェブサーバーへのアクセス — インターネットアクセスなしで VPC 内のウェブサーバーのアクセスを設定する場合、カスタムドメインを使用すると、URL リダイレクトのワークフローが簡素化されます。

**Topics**
+ [カスタムドメインを設定する](#create-environment-with-custom-domain)
+ [ネットワークインフラストラクチャを設定する](#set-up-networking-for-custom-domain)

## カスタムドメインを設定する
<a name="create-environment-with-custom-domain"></a>

カスタムドメイン機能を設定するには、Amazon MWAA 環境を作成または更新するときに、`webserver.base_url` Apache Airflow 設定を通してカスタムドメイン値を指定する必要があります。カスタムドメイン名には、次の制約が適用されます。
+ 値は、プロトコルやパスのない完全修飾ドメイン名 (FQDN) である必要があります。例えば、`your-custom-domain.com` です。
+ Amazon MWAA は URL 内のパスを許可しません。例えば、`your-custom-domain.com/dags/` は有効なカスタムドメイン名ではありません。
+ URL の長さは、255 文字の ASCII 文字に制限されています。
+ 空の文字列を指定すると、デフォルトで環境は Amazon MWAA によって生成されたウェブサーバー URL で作成されます。

次の例を使用して、AWS CLI を使用してカスタムウェブサーバードメイン名を持つ環境を作成します。

```
aws mwaa create-environment \
--name my-mwaa-env \
--source-bucket-arn arn:aws:s3:::amzn-s3-demo-bucket \
--airflow-configuration-options '{"webserver.base_url":"my-custom-domain.com"}' \
--network-configuration '{"SubnetIds":["subnet-0123456789abcdef","subnet-fedcba9876543210"]}' \
--execution-role-arn arn:aws:iam::123456789012:role/my-execution-role
```

環境を作成または更新したら、カスタムドメインを介してプライベートウェブサーバーにアクセスするために、AWS アカウント のネットワークインフラストラクチャを設定する必要があります。

サービスによって生成されたデフォルトの URL に戻すには、プライベート環境を更新し、`webserver.base_url` 設定オプションを削除します。

## ネットワークインフラストラクチャを設定する
<a name="set-up-networking-for-custom-domain"></a>

次の手順を使用して、AWS アカウント のカスタムドメインで使用するために必要なネットワークインフラストラクチャを設定します。

1. Amazon VPC エンドポイントネットワークインターフェイス (ENI) の IP アドレスを取得します。これを行うには、まず [https://awscli.amazonaws.com/v2/documentation/api/2.9.6/reference/mwaa/get-environment.html](https://awscli.amazonaws.com/v2/documentation/api/2.9.6/reference/mwaa/get-environment.html) を使用して環境の `WebserverVpcEndpointService` を検索します。

   ```
   aws mwaa get-environment --name your-environment-name
   ```

   成功すると、次のような出力が表示されます。

   ```
   {
     "Environment": {
       "AirflowConfigurationOptions": {},
       "AirflowVersion": "latest-version",
       "Arn": "environment-arn",
       "CreatedAt": "2024-06-01T01:00:00-00:00",
       "DagS3Path": "dags",
       .
       .
       .
       "WebserverVpcEndpointService": "web-server-vpc-endpoint-service",
       "WeeklyMaintenanceWindowStart": "TUE:21:30"
     }
   }
   ```

   `WebserverVpcEndpointService` 値を書き留め、次の Amazon EC2 `describe-vpc-endpoints` コマンドの `web-server-vpc-endpoint-service` に使用します。次のコマンドの `--filters Name=service-name,Values=web-server-vpc-endpoint-service-id`。

1. Amazon VPC エンドポイントの詳細を取得します。このコマンドは、特定のサービス名に一致する Amazon VPC エンドポイントの詳細を取得し、エンドポイント ID と関連するネットワークインターフェイス ID をテキスト形式で返します。

   ```
   aws ec2 describe-vpc-endpoints \
    --filters Name=service-name,Values=web-server-vpc-endpoint-service \
    --query 'VpcEndpoints[*].{EndpointId:VpcEndpointId,NetworkInterfaceIds:NetworkInterfaceIds}' \
    --output text
   ```

1. ネットワークインターフェイスの詳細を取得します。このコマンドは、前のステップで識別された Amazon VPC エンドポイントに関連付けられた各ネットワークインターフェイスのプライベート IP アドレスを取得します。

   ```
   for eni_id in $(
     aws ec2 describe-vpc-endpoints \
      --filters Name=service-name,Values=service-id \
      --query 'VpcEndpoints[*].NetworkInterfaceIds' \
      --output text
    ); do
    aws ec2 describe-network-interfaces \
     --network-interface-ids $eni_id \
     --query 'NetworkInterfaces[*].PrivateIpAddresses[*].PrivateIpAddress' \
     --output text
   						done
   ```

1. `create-target-group` を使用して、新しい対象グループを作成します。このターゲットグループを使用して、ウェブサーバーの Amazon VPC エンドポイントの IP アドレスを登録します。

   ```
   aws elbv2 create-target-group \
   --name new-target-group-namne \
   --protocol HTTPS \
   --port 443 \
   --vpc-id web-server-vpc-id \
   --target-type ip \
   --health-check-protocol HTTPS \
   --health-check-port 443 \
   --health-check-path / \
   --health-check-enabled \
   --matcher 'HttpCode="200,302"'
   ```

   `register-targets` コマンドを使用して IP アドレスを登録します。

   ```
   aws elbv2 register-targets \
   --target-group-arn target-group-arn \
   --targets Id=ip-address-1 Id=ip-address-2
   ```

1. ACM 証明書をリクエストします。既存の証明書を使用している場合は、このステップをスキップします。

   ```
   aws acm request-certificate \
   --domain-name my-custom-domain.com \
   --validation-method DNS
   ```

1. Application Load Balancer を設定します。まず、ロードバランサーを作成し、ロードバランサーのリスナーを作成します。前のステップで作成した ACM 証明書を指定します。

   ```
   aws elbv2 create-load-balancer \
   --name my-mwaa-lb \
   --type application \
   --subnets subnet-id-1 subnet-id-2
   ```

   ```
   aws elbv2 create-listener \
   --load-balancer-arn load-balancer-arn \
   --protocol HTTPS \
   --port 443 \
   --ssl-policy ELBSecurityPolicy-2016-08 \
   --certificates CertificateArn=acm-certificate-arn \
   --default-actions Type=forward,TargetGroupArn=target-group-arn
   ```

   プライベートサブネットで Network Load Balancer を使用する場合は、ウェブサーバーにアクセスするための [踏み台ホスト](tutorials-private-network-bastion.md) または [Site-to-Site VPN トンネル](tutorials-private-network-vpn-client.md) を設定します。

1. ドメインの Route 53 を使用してホストゾーンを作成します。

   ```
   aws route53 create-hosted-zone --name my-custom-domain.com \
   --caller-reference 1
   ```

   ドメインの A レコードを作成します。AWS CLI を使用してこれを行うには、`list-hosted-zones-by-name` を使用してホストゾーン ID を取得し、`change-resource-record-sets` でレコードを適用します。

   ```
   HOSTED_ZONE_ID=$(aws route53 list-hosted-zones-by-name \
   --dns-name my-custom-domain.com \
   --query 'HostedZones[0].Id' --output text)
   ```

   ```
   aws route53 change-resource-record-sets \
   --hosted-zone-id $HOSTED_ZONE_ID \
   --change-batch '{
     "Changes": [
       {
         "Action": "CREATE",
         "ResourceRecordSet": {
           "Name": "my-custom-domain.com",
           "Type": "A",
           "AliasTarget": {
             "HostedZoneId": "load-balancer-hosted-zone-id>",
             "DNSName": "load-balancer-dns-name",
             "EvaluateTargetHealth": true
           }
         }
       }
     ]
   }'
   ```

1. Application Load Balancer が配置されているパブリックサブネットからの HTTPS トラフィックのみを許可することで、最小特権の原則に従うようにウェブサーバーの Amazon VPC エンドポイントのセキュリティグループルールを更新します。次の JSON をローカルに保存します。例えば、`sg-ingress-ip-permissions.json` などにします。

   ```
   [
     {
       "IpProtocol": "tcp",
       "FromPort": 443,
       "ToPort": 443,
       "UserIdGroupPairs": [
         {
           "GroupId": "load-balancer-security-group-id"
         }
       ],
       "IpRanges": [
         {
           "CidrIp": "public-subnet-1-cidr"
         },
         {
           "CidrIp": "public-subnet-2-cidr"
         }
       ]
     }
   ]
   ```

   次の Amazon EC2 コマンドを実行して、セキュリティグループの Ingress ルールを更新します。`--ip-permissions` の JSON ファイルを指定します。

   ```
   aws ec2 authorize-security-group-ingress \
   --group-id <security-group-id> \
   --ip-permissions file://sg-ingress-ip-permissions.json
   ```

   次の Amazon EC2 コマンドを実行して、Egress ルールを更新します。

   ```
   aws ec2 authorize-security-group-egress \
   --group-id webserver-vpc-endpoint-security-group-id \
   --protocol tcp \
   --port 443 \
   --source-group load-balancer-security-group-id
   ```

Amazon MWAA コンソールを開き、Apache Airflow UI に移動します。ここで使用する Application Load Balancer ではなく、プライベートサブネットに Network Load Balancer を設定する場合は、次のいずれかのオプションを使用してウェブサーバーにアクセスする必要があります。
+ [チュートリアル: Linux 踏み台ホストを使用したプライベートネットワークアクセスの設定](tutorials-private-network-bastion.md)
+ [チュートリアル: AWS Client VPN を使用したプライベートネットワークアクセスの設定](tutorials-private-network-vpn-client.md)