Here is how to run a DAG on an AWS MWAA instance using a Node.js Lambda function without changing the api.auth_backend
setting:
const axios = require('axios');
const { MWAAClient, CreateCliTokenCommand } = require('@aws-sdk/client-mwaa');
const client = new MWAAClient({ region: 'us-east-2' });
const getAirflowCliToken = async (environmentName) => {
const command = new CreateCliTokenCommand({ Name: environmentName });
const { CliToken, WebServerHostname } = await client.send(command);
return { token: CliToken, host: WebServerHostname };
};
const triggerDag = async (environmentName, dagName, payload) => {
// Get an Airflow CLI token.
const { token, host } = await getAirflowCliToken(environmentName);
const url = `https://${host}/aws_mwaa/cli`;
const formattedPayload = JSON.stringify(payload);
const data = `dags trigger -c '${formattedPayload}' ${dagName}`;
const options = {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'text/plain'
}
};
await axios.post(url, data, options);
};
const handler = async (event) => {
const environmentName = 'example-environment'
const dagName = 'example_dag';
const payload = {
task_payload: 'FOOBAR'
};
// Trigger Airflow DAG.
await triggerDag(environmentName, dagName, payload);
return {
statusCode: 200,
body: 'OK'
};
};
module.exports.handler = handler;
auth_backend = airflow.api.auth.backend.basic_auth
because the default option can bedeny_all
as described in airflow.apache.org/docs/apache-airflow/stable/security/api.html (I don't remember when we changed the default) Also - consider migrating to 2.0 as 1.10.X version will reach EOL on 17th of June this year. – Fingerstallapi.auth_backend
is blocklisted by AWS. – Newmown