Amazon Managed Workflows for Apache Airflow (Amazon MWAA) is a totally managed service that builds upon Apache Airflow, providing its advantages whereas eliminating the necessity so that you can arrange, function, and keep the underlying infrastructure, decreasing operational overhead whereas growing safety and resilience.
Right now, we’re excited to announce an enhancement to the Amazon MWAA integration with the Airflow REST API. This enchancment streamlines the flexibility to entry and handle your Airflow environments and their integration with exterior methods, and means that you can work together together with your workflows programmatically. The Airflow REST API facilitates a variety of use circumstances, from centralizing and automating administrative duties to constructing event-driven, data-aware knowledge pipelines.
On this publish, we focus on the enhancement and current a number of use circumstances that the enhancement unlocks to your Amazon MWAA setting.
Airflow REST API
The Airflow REST API is a programmatic interface that means that you can work together with Airflow’s core functionalities. It’s a set of HTTP endpoints to carry out operations similar to invoking Directed Acyclic Graphs (DAGs), checking process 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 at this time, Amazon MWAA offered the inspiration for interacting with the Airflow REST API. Although useful, 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 decreasing complexity and bettering total usability.
Enhancement overview
The brand new InvokeRestApi functionality means that you can 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 appearing 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 gives 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 easy to work together with the Airflow REST API. - Improved usability – By appearing as an middleman, the improved API delivers Airflow REST API execution outcomes on to the consumer, decreasing complexity and bettering total usability.
- Automated administration – The simplified REST API entry permits automating varied administrative and administration duties, similar 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 primarily based on these occasions. This helps the rising emphasis on event-driven knowledge 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, bettering the general reliability and effectivity of event-driven pipelines.
Within the following sections, we display easy methods to use the improved API in varied use circumstances.
Methods to use the improved Amazon MWAA API
The next code snippet exhibits the overall 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 Methodology
to make use of are the required parameters, whereas QueryParameters
and Physique
are elective and can be utilized as wanted within the API calls.
The next code snippet exhibits the overall response format:
The RestApiStatusCode
represents the HTTP standing code returned by the Airflow REST API name, and the RestApiResponse
accommodates the response payload from the Airflow REST API.
The next pattern code snippet showcases easy methods to replace the outline discipline 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
connected to name the requisite setting. The permission will be scoped to particular Airflow roles (Admin, Op, Consumer, Viewer, or Public) to manage entry ranges.
This straightforward but highly effective REST API helps varied use circumstances to your Amazon MWAA environments. Let’s evaluate some essential ones within the subsequent sections.
Automate administration and administration duties
Previous to this launch, to automate configurations and setup of sources similar 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 possibly can simplify this automation with the brand new enhanced REST API assist.
For this instance, let’s assume you wish to automate sustaining your Amazon MWAA setting variables. You might want to carry out API operations similar to create, learn, replace, and delete on Airflow variables to attain this process. The next is an easy Python consumer to take action (mwaa_variables_client.py
):
Assuming that you’ve configured your terminal with acceptable AWS credentials, you may run the previous Python script to attain the next outcomes:
Let’s additional discover different helpful use circumstances.
Construct event-driven knowledge pipelines
The Airflow neighborhood has been actively innovating to reinforce the platform’s knowledge consciousness, enabling you to construct extra dynamic and responsive workflows. Once we introduced assist for model 2.9.2 in Amazon MWAA, we launched capabilities that permit pipelines to react to adjustments in datasets, each inside Airflow environments and in exterior methods. The brand new simplified integration with the Airflow REST API makes the implementation of data-driven pipelines extra easy.
Take into account a use case the place it’s essential 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 may create a consumer that may invoke this DAG, supplying the bash command of your selection as follows (mwaa_dag_run_client.py
):
The next snippet exhibits a pattern run of the script:
On the Airflow UI, the trigger_bash_command
process exhibits the next execution log:
You possibly can additional increase this instance to extra helpful event-driven architectures. Let’s increase the use case to run your knowledge 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 knowledge 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 a lot of information in a brief timeframe.
One technique to regulate the file processing throughput can be to introduce an Amazon Easy Queue Service (Amazon SQS) queue between the S3 bucket and the Lambda perform, which might help with price limiting the API requests to the net server. You possibly can 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 includes 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 manner, the Amazon MWAA setting can successfully batch the dataset occasions and scale sources primarily based on the load. Let’s discover this method in additional element.
Configure data-aware scheduling
Take into account 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 accommodates the areas of the newly added information within the S3 knowledge lake. Upon receiving dataset occasions, the Amazon MWAA setting batches the dataset occasions primarily based on the load and schedules the DAG to deal with them appropriately. The modified structure to assist the data-aware scheduling is introduced under.
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 exhibits the execution log of the extract
process from the pipeline. The log exhibits how the Airflow scheduler batched three dataset occasions collectively to deal with the load.
On this manner, you should use the improved REST API to create data-aware, event-driven pipelines.
Issues
When implementing options utilizing the improved Amazon MWAA REST API, it’s essential to think about the next:
- IAM permissions – Be sure that the IAM principal making the
invoke_rest_api
SDK name has theairflow:InvokeRestAPI
permission on the Amazon MWAA useful resource. To manage entry ranges, the permission will be scoped to particular Airflow roles (Admin, Op, Consumer, Viewer, or Public). - Error dealing with – Implement sturdy error dealing with mechanisms to deal with varied 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 knowledge 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 adjustments.
- Safety and compliance – Adhere to your group’s safety and compliance necessities when integrating exterior methods with Amazon MWAA and dealing with delicate knowledge.
You can begin utilizing the simplified integration with the Airflow REST API in your Amazon MWAA environments with Airflow model 2.4.3 or larger, 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 circumstances, from centralizing and automating administrative duties, bettering total usability, to constructing event-driven, data-aware knowledge pipelines.
As you discover this new function, take into account the assorted use circumstances and finest practices outlined on this publish. Through the use of the brand new InvokeRestApi
, you may streamline your knowledge administration processes, improve operational effectivity, and drive larger worth out of your data-driven methods.
In regards to the Authors
Chandan Rupakheti is a Senior Options Architect at AWS. His foremost focus at AWS lies within the intersection of analytics, serverless, and AdTech companies. He’s a passionate technical chief, researcher, and mentor with a knack for constructing progressive options within the cloud. Exterior of his skilled life, he loves spending time along with his household and buddies, and listening to and enjoying music.
Hernan Garcia is a Senior Options Architect at AWS primarily based out of Amsterdam. He has labored within the monetary companies trade since 2018, specializing in utility modernization and supporting prospects of their adoption of the cloud with a deal with serverless applied sciences.