Amazon Managed Workflows for Apache Airflow (Amazon MWAA) is a completely managed service that builds upon Apache Airflow, providing its advantages whereas eliminating the necessity so that you can arrange, function, and preserve the underlying infrastructure, lowering operational overhead whereas rising safety and resilience.
At the moment, we’re excited to announce an enhancement to the Amazon MWAA integration with the Airflow REST API. This enchancment streamlines the power to entry and handle your Airflow environments and their integration with exterior techniques, and lets you work together along with your workflows programmatically. The Airflow REST API facilitates a variety of use instances, from centralizing and automating administrative duties to constructing event-driven, data-aware information pipelines.
On this put up, we focus on the enhancement and current a number of use instances that the enhancement unlocks in your Amazon MWAA setting.
Airflow REST API
The Airflow REST API is a programmatic interface that lets you work together with Airflow’s core functionalities. It’s a set of HTTP endpoints to carry out operations comparable to invoking Directed Acyclic Graphs (DAGs), checking activity statuses, retrieving metadata about workflows, managing connections and variables, and even initiating dataset-related occasions, with out instantly accessing the Airflow net interface or command line instruments.
Earlier than in the present day, Amazon MWAA offered the muse for interacting with the Airflow REST API. Although purposeful, the method of acquiring and managing entry tokens and session cookies added complexity to the workflow. Amazon MWAA now helps a simplified mechanism for interacting with the Airflow REST API utilizing AWS credentials, considerably lowering complexity and enhancing total usability.
Enhancement overview
The brand new InvokeRestApi functionality lets you run Airflow REST API requests with a legitimate SigV4 signature utilizing your present AWS credentials. This function is now accessible to all Amazon MWAA environments (2.4.3+) in supported Amazon MWAA AWS Areas. By performing as an middleman, this REST API processes requests on behalf of customers, requiring solely the setting title and API request payload as inputs.
Integrating with the Airflow REST API by way of the improved Amazon MWAA API supplies a number of key advantages:
- Simplified integration – The brand new
InvokeRestApi
functionality in Amazon MWAA removes the complexity of managing entry tokens and session cookies, making it simple to work together with the Airflow REST API. - Improved usability – By performing as an middleman, the improved API delivers Airflow REST API execution outcomes on to the consumer, lowering complexity and enhancing total usability.
- Automated administration – The simplified REST API entry permits automating numerous administrative and administration duties, comparable to managing Airflow variables, connections, slot swimming pools, and extra.
- Occasion-driven architectures – The improved API facilitates seamless integration with exterior occasions, enabling the triggering of Airflow DAGs based mostly on these occasions. This helps the rising emphasis on event-driven information pipelines.
- Information-aware scheduling – Utilizing the dataset-based scheduling function in Airflow, the improved API permits the Amazon MWAA setting to handle the incoming workload and scale sources accordingly, enhancing the general reliability and effectivity of event-driven pipelines.
Within the following sections, we show methods to use the improved API in numerous use instances.
Tips on how to use the improved Amazon MWAA API
The next code snippet reveals the final request format for the improved REST API:
The Title
of the Amazon MWAA setting, the Path
of the Airflow REST API endpoint to be known as, and the HTTP Technique
to make use of are the required parameters, whereas QueryParameters
and Physique
are non-compulsory and can be utilized as wanted within the API calls.
The next code snippet reveals the final response format:
The RestApiStatusCode
represents the HTTP standing code returned by the Airflow REST API name, and the RestApiResponse
incorporates the response payload from the Airflow REST API.
The next pattern code snippet showcases methods to replace the outline subject of an Airflow variable utilizing the improved integration. The decision makes use of the AWS Python SDK to invoke the Airflow REST API for the duty.
To make the invoke_rest_api
SDK name, the calling consumer ought to have an AWS Identification and Entry Administration (IAM) principal of airflow:InvokeRestAPI
hooked up to name the requisite setting. The permission might be scoped to particular Airflow roles (Admin, Op, Consumer, Viewer, or Public) to regulate entry ranges.
This easy but highly effective REST API helps numerous use instances in your Amazon MWAA environments. Let’s assessment some vital ones within the subsequent sections.
Automate administration and administration duties
Previous to this launch, to automate configurations and setup of sources comparable to variables, connections, slot swimming pools, and extra, you needed to develop a prolonged boilerplate code to make API requests to the Amazon MWAA net servers. You needed to deal with the cookie and session administration within the course of. You may simplify this automation with the brand new enhanced REST API help.
For this instance, let’s assume you wish to automate sustaining your Amazon MWAA setting variables. It’s essential carry out API operations comparable to create, learn, replace, and delete on Airflow variables to realize this activity. The next is a straightforward Python consumer to take action (mwaa_variables_client.py
):
Assuming that you’ve got configured your terminal with acceptable AWS credentials, you’ll be able to run the previous Python script to realize the next outcomes:
Let’s additional discover different helpful use instances.
Construct event-driven information pipelines
The Airflow neighborhood has been actively innovating to reinforce the platform’s information consciousness, enabling you to construct extra dynamic and responsive workflows. Once we introduced help for model 2.9.2 in Amazon MWAA, we launched capabilities that enable pipelines to react to modifications in datasets, each inside Airflow environments and in exterior techniques. The brand new simplified integration with the Airflow REST API makes the implementation of data-driven pipelines extra simple.
Think about a use case the place you’ll want to run a pipeline that makes use of enter from an exterior occasion. The next pattern DAG runs a bash command equipped as a parameter (any_bash_command.py)
:
With the assistance of the improved REST API, you’ll be able to create a consumer that may invoke this DAG, supplying the bash command of your alternative as follows (mwaa_dag_run_client.py
):
The next snippet reveals a pattern run of the script:
On the Airflow UI, the trigger_bash_command
activity reveals the next execution log:
You may additional increase this instance to extra helpful event-driven architectures. Let’s increase the use case to run your information pipeline and carry out extract, remodel, and cargo (ETL) jobs when a brand new file lands in an Amazon Easy Storage Service (Amazon S3) bucket in your information lake. The next diagram illustrates one architectural method.
Within the context of invoking a DAG by way of an exterior enter, the AWS Lambda perform would don’t have any data of how busy the Amazon MWAA net server is, doubtlessly resulting in the perform overwhelming the Amazon MWAA net server by processing numerous information in a brief timeframe.
One method to regulate the file processing throughput could be to introduce an Amazon Easy Queue Service (Amazon SQS) queue between the S3 bucket and the Lambda perform, which may also help with price limiting the API requests to the online server. You may obtain this by configuring most concurrency for Lambda for the SQS occasion supply. Nonetheless, the Lambda perform would nonetheless be unaware of the processing capability accessible within the Amazon MWAA setting to run the DAGs.
Along with the SQS queue, to assist afford the Amazon MWAA setting handle the load natively, you should use the Airflow’s data-aware scheduling function utilizing datasets. This method entails utilizing the improved Amazon MWAA REST API to create dataset occasions, that are then utilized by the Airflow scheduler to schedule the DAG natively. This fashion, the Amazon MWAA setting can successfully batch the dataset occasions and scale sources based mostly on the load. Let’s discover this method in additional element.
Configure data-aware scheduling
Think about the next DAG that showcases a framework for an ETL pipeline (data_aware_pipeline.py
). It makes use of a dataset for scheduling.
Within the previous code snippet, a Dataset
object known as datalake is used to schedule the DAG. The get_resources
perform extracts the further
info that incorporates the places of the newly added information within the S3 information lake. Upon receiving dataset occasions, the Amazon MWAA setting batches the dataset occasions based mostly on the load and schedules the DAG to deal with them appropriately. The modified structure to help the data-aware scheduling is introduced beneath.
The next is a simplified consumer that may create a dataset occasion by way of the improved REST API (mwaa_dataset_client.py
):
The next is a code snippet for the Lambda perform within the previous structure to generate the dataset occasion, assuming the perform is configured to deal with one S3 PUT
occasion at a time (dataset_event_lambda.py
):
As new information get dropped into the S3 bucket, the Lambda perform will generate a dataset occasion per file, passing within the Amazon S3 location of the newly added information. The Amazon MWAA setting will schedule the ETL pipeline upon receiving the dataset occasions. The next diagram illustrates a pattern run of the ETL pipeline on the Airflow UI.
The next snippet reveals the execution log of the extract
activity from the pipeline. The log reveals how the Airflow scheduler batched three dataset occasions collectively to deal with the load.
On this means, you should use the improved REST API to create data-aware, event-driven pipelines.
Concerns
When implementing options utilizing the improved Amazon MWAA REST API, it’s vital to contemplate the next:
- IAM permissions – Make sure that the IAM principal making the
invoke_rest_api
SDK name has theairflow:InvokeRestAPI
permission on the Amazon MWAA useful resource. To regulate entry ranges, the permission might be scoped to particular Airflow roles (Admin, Op, Consumer, Viewer, or Public). - Error dealing with – Implement strong error dealing with mechanisms to deal with numerous HTTP standing codes and error responses from the Airflow REST API.
- Monitoring and logging – Arrange acceptable monitoring and logging to trace the efficiency and reliability of your API-based integrations and information pipelines.
- Versioning and compatibility – Monitor the versioning of the Airflow REST API and the Amazon MWAA service to ensure your integrations stay appropriate with any future modifications.
- Safety and compliance – Adhere to your group’s safety and compliance necessities when integrating exterior techniques with Amazon MWAA and dealing with delicate information.
You can begin utilizing the simplified integration with the Airflow REST API in your Amazon MWAA environments with Airflow model 2.4.3 or better, in all at the moment supported Areas.
Conclusion
The improved integration between Amazon MWAA and the Airflow REST API represents a major enchancment within the ease of interacting with Airflow’s core functionalities. This new functionality opens up a variety of use instances, from centralizing and automating administrative duties, enhancing total usability, to constructing event-driven, data-aware information pipelines.
As you discover this new function, contemplate the assorted use instances and greatest practices outlined on this put up. Through the use of the brand new InvokeRestApi
, you’ll be able to streamline your information administration processes, improve operational effectivity, and drive better worth out of your data-driven techniques.
In regards to the Authors
Chandan Rupakheti is a Senior Options Architect at AWS. His most important focus at AWS lies within the intersection of analytics, serverless, and AdTech providers. He’s a passionate technical chief, researcher, and mentor with a knack for constructing progressive options within the cloud. Outdoors of his skilled life, he loves spending time together with his household and mates, and listening to and taking part in music.
Hernan Garcia is a Senior Options Architect at AWS based mostly out of Amsterdam. He has labored within the monetary providers trade since 2018, specializing in software modernization and supporting prospects of their adoption of the cloud with a concentrate on serverless applied sciences.