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à.
Creazione di un token CLI Apache Airflow
L'API REST è più moderna della CLI ed è progettata per l'integrazione programmatica con sistemi esterni. REST è il modo preferito per interagire con Apache Airflow.
Puoi utilizzare i comandi in questa pagina per generare un token CLI e quindi effettuare chiamate API Amazon Managed Workflows for Apache Airflow direttamente nella tua shell dei comandi. Ad esempio, puoi ottenere un token, quindi distribuire i DAG in modo programmatico utilizzando le API Amazon MWAA. La sezione seguente include i passaggi per creare un token CLI Apache Airflow utilizzando uno script curl AWS CLI, uno script Python o uno script bash. Il token restituito nella risposta è valido per 60 secondi.
Il AWS CLI token è inteso come sostituto delle azioni di shell sincrone, non dei comandi API asincroni. Pertanto, la concorrenza disponibile è limitata. Per garantire che il server web rimanga reattivo per gli utenti, consigliamo di non aprire una nuova AWS CLI richiesta finché la precedente non sia stata completata con successo.
Prerequisiti
La sezione seguente descrive i passaggi preliminari necessari per utilizzare i comandi e gli script in questa pagina.
Accesso
AWS CLI
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:
Utilizzo di AWS CLI
L'esempio seguente utilizza il comando create-cli-token AWS CLI per creare un token CLI Apache Airflow.
aws mwaa create-cli-token --name YOUR_ENVIRONMENT_NAME
Usare uno script curl
Utilizzate il seguente script curl per chiamare il comando create-cli-token in per richiamare la CLI AWS CLI di Apache Airflow tramite un endpoint sul server web Apache Airflow e attivare un DAG.
- Apache Airflow v3
-
-
Copia l'istruzione curl dal tuo file di testo e incollala nella shell dei comandi.
Dopo averlo copiato negli appunti, potresti dover usare Modifica > Incolla dal menu della shell.
CLI_JSON=$(aws mwaa --region us-east-1 create-cli-token --name YOUR_ENVIRONMENT_NAME) \
&& CLI_TOKEN=$(echo $CLI_JSON | jq -r '.CliToken') \
&& WEB_SERVER_HOSTNAME=$(echo $CLI_JSON | jq -r '.WebServerHostname') \
&& CLI_RESULTS=$(curl -L --request POST "https://$WEB_SERVER_HOSTNAME/aws_mwaa/cli" \
--header "Authorization: Bearer $CLI_TOKEN" \
--header "Content-Type: text/plain" \
--data-raw "dags trigger YOUR_DAG_NAME --logical-date $(date -u +"%Y-%m-%dT%H:%M:%SZ")") \
&& echo "Output:" \
&& echo $CLI_RESULTS | jq -r '.stdout' | base64 --decode \
&& echo "Errors:" \
&& echo $CLI_RESULTS | jq -r '.stderr' | base64 --decode
-
Sostituiscilo YOUR_DAG_NAME con il nome del tuo DAG, YOUR_ENVIRONMENT_NAME con il nome del tuo ambiente e Regione AWS con la regione del tuo ambiente. Ad esempio, un nome host per una rete pubblica è simile (senza https://):
123456a0-0101-2020-9e11-1b159eec9000.c2.us-east-1.airflow.amazonaws.com
Il prompt dei comandi mostra:
{
"stderr":"<STDERR of the CLI execution (if any), base64 encoded>",
"stdout":"<STDOUT of the CLI execution, base64 encoded>"
}
- Apache Airflow v2
-
-
Copia l'istruzione curl dal tuo file di testo e incollala nella shell dei comandi.
Dopo averlo copiato negli appunti, potresti dover usare Modifica > Incolla dal menu della shell.
CLI_JSON=$(aws mwaa --region us-east-1 create-cli-token --name YOUR_ENVIRONMENT_NAME) \
&& CLI_TOKEN=$(echo $CLI_JSON | jq -r '.CliToken') \
&& WEB_SERVER_HOSTNAME=$(echo $CLI_JSON | jq -r '.WebServerHostname') \
&& CLI_RESULTS=$(curl --request POST "https://$WEB_SERVER_HOSTNAME/aws_mwaa/cli" \
--header "Authorization: Bearer $CLI_TOKEN" \
--header "Content-Type: text/plain" \
--data-raw "dags trigger YOUR_DAG_NAME") \
&& echo "Output:" \
&& echo $CLI_RESULTS | jq -r '.stdout' | base64 --decode \
&& echo "Errors:" \
&& echo $CLI_RESULTS | jq -r '.stderr' | base64 --decode
-
Sostituiscilo YOUR_DAG_NAME con il nome del tuo DAG, YOUR_ENVIRONMENT_NAME con il nome del tuo ambiente e Regione AWS con la regione del tuo ambiente. Ad esempio, un nome host per una rete pubblica è simile (senza https://):
123456a0-0101-2020-9e11-1b159eec9000.c2.us-east-1.airflow.amazonaws.com
Il prompt dei comandi mostra:
{
"stderr":"<STDERR of the CLI execution (if any), base64 encoded>",
"stdout":"<STDOUT of the CLI execution, base64 encoded>"
}
Usare uno script bash
Usa il seguente script bash per chiamare il comando create-cli-token in AWS CLI, creare un token Apache Airflow CLI e attivare un DAG.
- Apache Airflow v3
-
-
Copia il contenuto del seguente esempio di codice e salvalo localmente con nome. get-cli-token.sh
# brew install jq
CLI_TOKEN=$(aws mwaa create-cli-token --name YOUR_ENVIRONMENT_NAME | jq -r '.CliToken') \
&& curl -L --request POST "https://YOUR_HOST_NAME/aws_mwaa/cli" \
--header "Authorization: Bearer $CLI_TOKEN" \
--header "Content-Type: text/plain" \
--data-raw "dags trigger YOUR_DAG_NAME --logical-date $(date -u +"%Y-%m-%dT%H:%M:%SZ")"
-
Sostituiscilo YOUR_ENVIRONMENT_NAME con il nome del tuo ambiente, YOUR_HOST_NAME con il nome host del tuo server web e YOUR_DAG_NAME con il nome del tuo DAG. Ad esempio, un nome host per una rete pubblica è simile (senza https://):
123456a0-0101-2020-9e11-1b159eec9000.c2.us-east-1.airflow.amazonaws.com
(opzionale) Gli utenti macOS e Linux potrebbero dover eseguire il comando seguente per assicurarsi che lo script sia eseguibile.
chmod +x get-cli-token.sh
-
Esegui il seguente script per creare un token CLI di Apache Airflow.
./get-cli-token.sh
- Apache Airflow v2
-
-
Copia il contenuto del seguente esempio di codice e salvalo localmente con nome. get-cli-token.sh
# brew install jq
CLI_TOKEN=$(aws mwaa create-cli-token --name YOUR_ENVIRONMENT_NAME | jq -r '.CliToken') \
&& curl --request POST "https://YOUR_HOST_NAME/aws_mwaa/cli" \
--header "Authorization: Bearer $CLI_TOKEN" \
--header "Content-Type: text/plain" \
--data-raw "dags trigger YOUR_DAG_NAME"
-
Sostituiscilo YOUR_ENVIRONMENT_NAME con il nome del tuo ambiente, YOUR_HOST_NAME con il nome host del tuo server web e YOUR_DAG_NAME con il nome del tuo DAG. Ad esempio, un nome host per una rete pubblica è simile (senza https://):
123456a0-0101-2020-9e11-1b159eec9000.c2.us-east-1.airflow.amazonaws.com
-
(opzionale) Gli utenti macOS e Linux possono eseguire il comando seguente per assicurarsi che lo script sia eseguibile.
chmod +x get-cli-token.sh
-
Esegui il seguente script per creare un token CLI di Apache Airflow.
./get-cli-token.sh
Usare uno script Python
L'esempio seguente utilizza il metodo boto3 create_cli_token in uno script Python per creare un token CLI Apache Airflow e attivare un DAG. Puoi eseguire questo script all'esterno di Amazon MWAA. L'unica cosa che devi fare è installare la libreria boto3. Potresti voler creare un ambiente virtuale per installare la libreria. Presuppone che tu abbia configurato le credenziali di AWS autenticazione per il tuo account.
- Apache Airflow v3
-
-
Copia il contenuto del seguente esempio di codice e salvalo localmente con nome. create-cli-token.py
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import boto3
import json
import requests
import base64
mwaa_env_name = 'YOUR_ENVIRONMENT_NAME'
dag_name = 'YOUR_DAG_NAME'
mwaa_cli_command = 'dags trigger'
client = boto3.client('mwaa')
mwaa_cli_token = client.create_cli_token(
Name=mwaa_env_name
)
mwaa_auth_token = 'Bearer ' + mwaa_cli_token['CliToken']
mwaa_webserver_hostname = 'https://{0}/aws_mwaa/cli'.format(mwaa_cli_token['WebServerHostname'])
raw_data = '{0} {1}'.format(mwaa_cli_command, dag_name)
mwaa_response = requests.post(
mwaa_webserver_hostname,
headers={
'Authorization': mwaa_auth_token,
'Content-Type': 'text/plain'
},
data=raw_data
)
mwaa_std_err_message = base64.b64decode(mwaa_response.json()['stderr']).decode('utf8')
mwaa_std_out_message = base64.b64decode(mwaa_response.json()['stdout']).decode('utf8')
print(mwaa_response.status_code)
print(mwaa_std_err_message)
print(mwaa_std_out_message)
-
Sostituisci i segnaposto con e. YOUR_ENVIRONMENT_NAME YOUR_DAG_NAME
-
Eseguite il seguente script per creare un token CLI di Apache Airflow.
python3 create-cli-token.py
- Apache Airflow v2
-
-
Copia il contenuto del seguente esempio di codice e salvalo localmente con nome. create-cli-token.py
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import boto3
import json
import requests
import base64
mwaa_env_name = 'YOUR_ENVIRONMENT_NAME'
dag_name = 'YOUR_DAG_NAME'
mwaa_cli_command = 'dags trigger'
client = boto3.client('mwaa')
mwaa_cli_token = client.create_cli_token(
Name=mwaa_env_name
)
mwaa_auth_token = 'Bearer ' + mwaa_cli_token['CliToken']
mwaa_webserver_hostname = 'https://{0}/aws_mwaa/cli'.format(mwaa_cli_token['WebServerHostname'])
raw_data = '{0} {1}'.format(mwaa_cli_command, dag_name)
mwaa_response = requests.post(
mwaa_webserver_hostname,
headers={
'Authorization': mwaa_auth_token,
'Content-Type': 'text/plain'
},
data=raw_data
)
mwaa_std_err_message = base64.b64decode(mwaa_response.json()['stderr']).decode('utf8')
mwaa_std_out_message = base64.b64decode(mwaa_response.json()['stdout']).decode('utf8')
print(mwaa_response.status_code)
print(mwaa_std_err_message)
print(mwaa_std_out_message)
-
Sostituisci i segnaposto con e. YOUR_ENVIRONMENT_NAME YOUR_DAG_NAME
-
Eseguite il seguente script per creare un token CLI di Apache Airflow.
python3 create-cli-token.py
Fasi successive