Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Tutorial di avvio rapido per Amazon Managed Workflows per Apache Airflow
Questo tutorial di avvio rapido utilizza un AWS CloudFormation modello che crea insieme l'infrastruttura Amazon VPC, un bucket Amazon S3 con una dags cartella e un ambiente Amazon Managed Workflows for Apache Airflow.
Argomenti
In questo tutorial
Usa questo tutorial per caricare un DAG su Amazon S3, eseguire il DAG in Apache Airflow e accedere ai log in utilizzando tre () comandi. CloudWatch AWS Command Line Interface AWS CLI Infine, imparerai a creare una policy IAM per un team di sviluppo di Apache Airflow.
Nota
Il CloudFormation modello in questa pagina crea un ambiente Amazon Managed Workflows for Apache Airflow per l'ultima versione di Apache Airflow disponibile in. CloudFormation L'ultima versione disponibile è Apache Airflow v3.2.1.
Il CloudFormation modello crea quanto segue:
-
Infrastruttura VPC. Il modello utilizzaRouting pubblico su Internet. Utilizza il server Modalità di accesso alla rete pubblica web Apache Airflow in.
WebserverAccessMode: PUBLIC_ONLY -
Bucket Amazon S3. Il modello crea un bucket Amazon S3 con una cartella.
dagsÈ configurato per bloccare tutti gli accessi pubblici, con Bucket Versioning abilitato, come definito in. Crea un bucket Amazon S3 per Amazon MWAA -
Ambiente Amazon MWAA. Il modello crea un ambiente Amazon MWAA associato alla
dagscartella nel bucket Amazon S3, un ruolo di esecuzione con autorizzazione per i AWS servizi utilizzati da Amazon MWAA e l'impostazione predefinita per la crittografia tramite una chiave di proprietà, come definito in. AWS Crea un ambiente Amazon MWAA -
CloudWatch Log. Il modello attiva gli accessi di Apache Airflow CloudWatch a livello INFO e attivo per il gruppo di log dello scheduler Airflow, il gruppo di log del server web Airflow, il gruppo di log dei lavoratori Airflow, il gruppo di log di elaborazione Airflow DAG e il gruppo di log delle attività Airflow, come definito in. Accesso ai log Airflow in Amazon CloudWatch
In questo tutorial, completerai le seguenti attività:
-
Carica ed esegui un DAG. Carica il tutorial DAG di Apache Airflow per l'ultima versione di Apache Airflow supportata da Amazon MWAA su Amazon S3, quindi eseguilo nell'interfaccia utente di Apache Airflow, come definito in. Aggiungere o aggiornare DAGs
-
Registri di accesso. Accedere al gruppo di log del server web Airflow in CloudWatch Logs, come definito in. Accesso ai log Airflow in Amazon CloudWatch
-
Crea una politica di controllo degli accessi. Crea una policy di controllo degli accessi in IAM per il tuo team di sviluppo di Apache Airflow, come definito in. Accesso a un ambiente Amazon MWAA
Nota
Nel VPC che ospita l'ambiente Amazon MWAA, impostato su assignIpv6AddressOnCreation per true tutte le sottoreti collegate. Questa impostazione garantisce l'assegnazione automatica degli indirizzi IPv6 (Internet Protocol version 6) alle risorse all'interno di queste sottoreti.
Prerequisiti
AWS Command Line Interface (AWS CLI) è uno strumento open source che puoi utilizzare per interagire con i AWS servizi utilizzando i comandi nella shell della riga di comando. Per completare i passaggi indicati in questa pagina, è necessario quanto segue:
Fase uno: salva il CloudFormation modello localmente
-
Copia il contenuto del seguente modello e salvalo localmente con nome
mwaa-public-network.yml. Puoi anche scaricare il modello.AWSTemplateFormatVersion: "2010-09-09" Parameters: EnvironmentName: Description: An environment name that is prefixed to resource names Type: String Default: MWAAEnvironment VpcCIDR: Description: The IP range (CIDR notation) for this VPC Type: String Default: 10.192.0.0/16 PublicSubnet1CIDR: Description: The IP range (CIDR notation) for the public subnet in the first Availability Zone Type: String Default: 10.192.10.0/24 PublicSubnet2CIDR: Description: The IP range (CIDR notation) for the public subnet in the second Availability Zone Type: String Default: 10.192.11.0/24 PrivateSubnet1CIDR: Description: The IP range (CIDR notation) for the private subnet in the first Availability Zone Type: String Default: 10.192.20.0/24 PrivateSubnet2CIDR: Description: The IP range (CIDR notation) for the private subnet in the second Availability Zone Type: String Default: 10.192.21.0/24 MaxWorkerNodes: Description: The maximum number of workers that can run in the environment Type: Number Default: 2 DagProcessingLogs: Description: Log level for DagProcessing Type: String Default: INFO SchedulerLogsLevel: Description: Log level for SchedulerLogs Type: String Default: INFO TaskLogsLevel: Description: Log level for TaskLogs Type: String Default: INFO WorkerLogsLevel: Description: Log level for WorkerLogs Type: String Default: INFO WebserverLogsLevel: Description: Log level for WebserverLogs Type: String Default: INFO Resources: ##################################################################################################################### # CREATE VPC ##################################################################################################################### VPC: Type: AWS::EC2::VPC Properties: CidrBlock: !Ref VpcCIDR EnableDnsSupport: true EnableDnsHostnames: true Tags: - Key: Name Value: MWAAEnvironment InternetGateway: Type: AWS::EC2::InternetGateway Properties: Tags: - Key: Name Value: MWAAEnvironment 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: VpcId: !Ref VPC GroupDescription: !Sub "Security Group for Amazon MWAA Environment ${AWS::StackName}-MwaaEnvironment" GroupName: !Sub "airflow-security-group-${AWS::StackName}-MwaaEnvironment" SecurityGroupIngress: Type: AWS::EC2::SecurityGroupIngress Properties: GroupId: !Ref SecurityGroup IpProtocol: "-1" SourceSecurityGroupId: !Ref SecurityGroup EnvironmentBucket: Type: AWS::S3::Bucket Properties: VersioningConfiguration: Status: Enabled PublicAccessBlockConfiguration: BlockPublicAcls: true BlockPublicPolicy: true IgnorePublicAcls: true RestrictPublicBuckets: true ##################################################################################################################### # CREATE MWAA ##################################################################################################################### MwaaEnvironment: Type: AWS::MWAA::Environment DependsOn: MwaaExecutionPolicy Properties: Name: !Sub "${AWS::StackName}-MwaaEnvironment" SourceBucketArn: !GetAtt EnvironmentBucket.Arn ExecutionRoleArn: !GetAtt MwaaExecutionRole.Arn DagS3Path: dags/ NetworkConfiguration: SecurityGroupIds: - !GetAtt SecurityGroup.GroupId SubnetIds: - !Ref PrivateSubnet1 - !Ref PrivateSubnet2 WebserverAccessMode: PUBLIC_ONLY MaxWorkers: !Ref MaxWorkerNodes LoggingConfiguration: DagProcessingLogs: LogLevel: !Ref DagProcessingLogs Enabled: true SchedulerLogs: LogLevel: !Ref SchedulerLogsLevel Enabled: true TaskLogs: LogLevel: !Ref TaskLogsLevel Enabled: true WorkerLogs: LogLevel: !Ref WorkerLogsLevel Enabled: true WebserverLogs: LogLevel: !Ref WebserverLogsLevel Enabled: true MwaaExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: - airflow-env.amazonaws.com - airflow.amazonaws.com Action: - "sts:AssumeRole" Path: "/service-role/" MwaaExecutionPolicy: DependsOn: EnvironmentBucket Type: AWS::IAM::ManagedPolicy Properties: Roles: - !Ref MwaaExecutionRole PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: airflow:PublishMetrics Resource: - !Sub "arn:aws:airflow:${AWS::Region}:${AWS::AccountId}:environment/${EnvironmentName}" - Effect: Deny Action: s3:ListAllMyBuckets Resource: - !Sub "${EnvironmentBucket.Arn}" - !Sub "${EnvironmentBucket.Arn}/*" - Effect: Allow Action: - "s3:GetObject*" - "s3:GetBucket*" - "s3:List*" Resource: - !Sub "${EnvironmentBucket.Arn}" - !Sub "${EnvironmentBucket.Arn}/*" - Effect: Allow Action: - logs:DescribeLogGroups Resource: "*" - Effect: Allow Action: - logs:CreateLogStream - logs:CreateLogGroup - logs:PutLogEvents - logs:GetLogEvents - logs:GetLogRecord - logs:GetLogGroupFields - logs:GetQueryResults - logs:DescribeLogGroups Resource: - !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:airflow-${AWS::StackName}*" - Effect: Allow Action: cloudwatch:PutMetricData Resource: "*" - Effect: Allow Action: - sqs:ChangeMessageVisibility - sqs:DeleteMessage - sqs:GetQueueAttributes - sqs:GetQueueUrl - sqs:ReceiveMessage - sqs:SendMessage Resource: - !Sub "arn:aws:sqs:${AWS::Region}:*:airflow-celery-*" - Effect: Allow Action: - kms:Decrypt - kms:DescribeKey - "kms:GenerateDataKey*" - kms:Encrypt NotResource: !Sub "arn:aws:kms:*:${AWS::AccountId}:key/*" Condition: StringLike: "kms:ViaService": - !Sub "sqs.${AWS::Region}.amazonaws.com" 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 MwaaApacheAirflowUI: Description: MWAA Environment Value: !Sub "https://${MwaaEnvironment.WebserverUrl}"
Fase due: Crea la pila usando AWS CLI
-
Nel prompt dei comandi, accedi alla directory in cui
mwaa-public-network.ymlè archiviato. Ad esempio:cd mwaaproject -
Usa il
aws cloudformation create-stackcomando per creare lo stack usando AWS CLI.aws cloudformation create-stack --stack-name mwaa-environment-public-network --template-body file://mwaa-public-network.yml --capabilities CAPABILITY_IAMNota
Sono necessari più di 30 minuti per creare l'infrastruttura Amazon VPC, il bucket Amazon S3 e l'ambiente Amazon MWAA.
Fase tre: carica un DAG su Amazon S3 ed eseguilo nell'interfaccia utente di Apache Airflow
-
Copia il contenuto del
tutorial.pyfile per l'ultima versione supportata di Apache Airflow e salva localmente con nome.tutorial.py -
Nel prompt dei comandi, accedi alla directory in cui
tutorial.pyè memorizzato. Ad esempio:cd mwaaproject -
Usa il comando seguente per elencare tutti i tuoi bucket Amazon S3.
aws s3 ls -
Usa il seguente comando per elencare i file e le cartelle nel bucket Amazon S3 per il tuo ambiente.
aws s3 ls s3://YOUR_S3_BUCKET_NAME -
Usa il seguente script per caricare il
tutorial.pyfile nella tuadagscartella. Sostituisci il valore del campione inamzn-s3-demo-bucket.aws s3 cp tutorial.py s3://amzn-s3-demo-bucket/dags/ -
Apri la pagina
Ambienti sulla console Amazon MWAA. -
Scegli un ambiente.
-
Scegli Open Airflow UI.
-
Nell'interfaccia utente di Apache Airflow, dall'elenco dei DAG disponibili, scegli il tutorial DAG.
-
Nella pagina dei dettagli del DAG, scegli l'interruttore DAG accanto al nome del Pause/Unpause DAG per riattivare il DAG.
-
Scegli Trigger DAG.
Fase quattro: accedere ai log in CloudWatch Logs
È possibile accedere ai log di Apache Airflow nella CloudWatch console per tutti i log di Apache Airflow attivati dallo stack. CloudFormation La sezione seguente spiega come accedere ai log per il gruppo di log del server web Airflow.
-
Apri la pagina
Ambienti sulla console Amazon MWAA. -
Scegli un ambiente.
-
Scegliete il gruppo di log del server web Airflow nel pannello Monitoraggio.
-
Scegli l'
webserver_console_ipaccesso Log streams.
Fasi successive
-
Scopri di più su come caricare i DAG, specificare le dipendenze di Python in a
requirements.txte i plugin personalizzati in un input.plugins.zipLavorare con DAGs Amazon MWAA -
Scopri di più sulle best practice che consigliamo per ottimizzare le prestazioni del tuo ambiente. Ottimizzazione delle prestazioni per Apache Airflow su Amazon MWAA
-
Crea una dashboard di monitoraggio per il tuo ambiente inDashboard di monitoraggio e allarmi su Amazon MWAA.
-
Esegui alcuni esempi di codice DAG inEsempi di codice per Amazon Managed Workflows for Apache Airflow.