設定 Apache Airflow Web 伺服器的自訂網域 - Amazon Managed Workflows for Apache Airflow

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

設定 Apache Airflow Web 伺服器的自訂網域

Amazon Managed Workflows for Apache Airflow (Amazon MWAA) 可讓您為受管 Apache Airflow Web 伺服器設定自訂網域。使用自訂網域,您可以使用 Apache Airflow UI、Apache Airflow CLI 或 Apache Airflow Web 伺服器存取您環境的 Amazon MWAA 受管 Apache Airflow Web 伺服器。

注意

您只能將自訂網域與沒有網際網路存取的私有 Web 伺服器搭配使用。

Amazon MWAA 上自訂網域的使用案例
  1. 在 上跨雲端應用程式共用 Web 伺服器網域 AWS - 使用自訂網域可讓您定義存取 Web 伺服器的易用 URL,而不是產生的服務網域名稱。您可以存放此自訂網域,並在應用程式中將其做為環境變數共用。

  2. 存取私有 Web 伺服器:如果您想要在 VPC 中設定沒有網際網路存取的 Web 伺服器的存取,使用自訂網域可簡化 URL 重新導向工作流程。

設定自訂網域

若要設定自訂網域功能,您需要在建立或更新 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 產生的 Web 伺服器 URL 建立。

下列範例顯示使用 AWS CLI 建立具有自訂 Web 伺服器網域名稱的環境。

$ aws mwaa create-environment \ --name my-mwaa-env \ --source-bucket-arn arn:aws:s3:::my-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 帳戶中設定網路基礎設施,以透過自訂網域存取私有 Web 伺服器。

若要還原至預設服務產生的 URL,請更新您的私有環境並移除webserver.base_url組態選項。

設定網路基礎設施

使用下列步驟來設定必要的聯網基礎設施,以搭配您 AWS 帳戶中的自訂網域使用。

  1. 取得 Amazon VPC 端點網路介面 (ENI) 的 IP 地址。若要執行此操作,請先使用 get-environment 為您的環境尋找 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中。

  2. 擷取 Amazon VPC 端點詳細資訊。此命令會擷取符合特定服務名稱的 Amazon VPC 端點詳細資訊,並以文字格式傳回端點 ID 和相關聯的網路介面 IDs。

    $ aws ec2 describe-vpc-endpoints \ --filters Name=service-name,Values=web-server-vpc-endpoint-service \ --query 'VpcEndpoints[*].{EndpointId:VpcEndpointId,NetworkInterfaceIds:NetworkInterfaceIds}' \ --output text
  3. 取得網路介面詳細資訊。此命令會擷取與上一個步驟中識別的 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
  4. 使用 create-target-group 建立新的目標群組。您將使用此目標群組來註冊 Web 伺服器 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
  5. 請求 ACM 憑證。如果您使用現有的憑證,請略過此步驟。

    $ aws acm request-certificate \ --domain-name my-custom-domain.com \ --validation-method DNS
  6. 設定一個 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,請設定堡壘主機AWS VPN 通道來存取 Web 伺服器。

  7. 使用網域的 Route 53 建立託管區域。

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

    為網域建立 A 記錄。若要使用 執行此操作 AWS CLI,請使用 取得託管區域 ID,list-hosted-zones-by-name然後使用 套用記錄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 } } } ] }'
  8. 更新 Web 伺服器 Amazon VPC 端點的安全群組規則,以遵循最低權限原則,僅允許來自 Application Load Balancer 所在公有子網路的 HTTPS 流量。在本機儲存下列 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 命令來更新您的傳入安全群組規則。指定 的 JSON 檔案--ip-permissions

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

    執行下列 Amazon EC2 命令來更新您的輸出規則。

    $ 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。如果您要在私有子網路中設定 Network Load Balancer,而不是此處使用的 Application Load Balancer,則必須使用下列其中一個選項存取 Web 伺服器。