Created
July 25, 2023 03:29
-
-
Save khpraful/1333a5cd743a75f35f1ea331f79c1563 to your computer and use it in GitHub Desktop.
Invoking MWAA DAG from JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const { MWAAClient, CreateCliTokenCommand } = require('@aws-sdk/client-mwaa'); | |
const client = new MWAAClient({ region: 'AWS_REGION' }); | |
const environmentName = 'MWAA_ENV_NAME' | |
const dagName = 'DAG_NAME'; | |
const payload = { | |
} | |
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) => { | |
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); | |
}; | |
triggerDag(environmentName, dagName, payload); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment