Starting from Airflow version 2.9, MWAA has open-sourced the original Docker image used in production deployments. You can refer to MWAA open-source image repository at https://github.com/aws/amazon-mwaa-docker-images to create a local environment identical to that of MWAA.
This repository provides a command line interface (CLI) utility that replicates an Amazon Managed Workflows for Apache Airflow (MWAA) environment locally.
Please note: MWAA/AWS/DAG/Plugin issues should be raised through AWS Support or the Airflow Slack #airflow-aws channel. Issues here should be focused on this local-runner repository.
Please note: The dynamic configurations which are dependent on the class of an environment are
aligned with the Large environment class in this repository.
About the CLI
The CLI builds a Docker container image locally that’s similar to a MWAA production image. This allows you to run a local Apache Airflow environment to develop and test DAGs, custom plugins, and dependencies before deploying to MWAA.
The following section describes where to add your DAG code and supporting files. We recommend creating a directory structure similar to your MWAA environment.
DAGs
Add DAG code to the dags/ folder.
To run the sample code in this repository, see the example_dag_with_taskflow_api.py file.
Requirements.txt
Add Python dependencies to requirements/requirements.txt.
To test a requirements.txt without running Apache Airflow, use the following script:
./mwaa-local-env test-requirements
Let’s say you add aws-batch==0.6 to your requirements/requirements.txt file. You should see an output similar to:
Learn how to upload the DAG code to the dags folder in your Amazon S3 bucket in Adding or updating DAGs.
Learn more about how to upload the plugins.zip file to your Amazon S3 bucket in Installing custom plugins.
FAQs
The following section contains common questions and answers you may encounter when using your Docker container image.
Can I test execution role permissions using this repository?
You can setup the local Airflow’s boto with the intended execution role to test your DAGs with AWS operators before uploading to your Amazon S3 bucket. To setup aws connection for Airflow locally see Airflow | AWS Connection
To learn more, see Amazon MWAA Execution Role.
How do I add libraries to requirements.txt and test install?
A requirements.txt file is included in the /requirements folder of your local Docker container image. We recommend adding libraries to this file, and running locally.
What if a library is not available on PyPi.org?
If a library is not available in the Python Package Index (PyPi.org), add the --index-url flag to the package in your requirements/requirements.txt file. To learn more, see Managing Python dependencies in requirements.txt.
Troubleshooting
The following section contains errors you may encounter when using the Docker container image in this repository.
My environment is not starting
If you encountered the following error: process fails with "dag_stats_table already exists", you’ll need to reset your database using the following command:
./mwaa-local-env reset-db
If you are moving from an older version of local-runner you may need to run the above reset-db command, or delete your ./db-data folder. Note, too, that newer Airflow versions have newer provider packages, which may require updating your DAG code.
Fernet Key InvalidToken
A Fernet Key is generated during image build (./mwaa-local-env build-image) and is durable throughout all
containers started from that image. This key is used to encrypt connection passwords in the Airflow DB.
If changes are made to the image and it is rebuilt, you may get a new key that will not match the key used when
the Airflow DB was initialized, in this case you will need to reset the DB (./mwaa-local-env reset-db).
Note
Starting from Airflow version 2.9, MWAA has open-sourced the original Docker image used in production deployments. You can refer to MWAA open-source image repository at https://github.com/aws/amazon-mwaa-docker-images to create a local environment identical to that of MWAA.
Airflow 3.x versions would be suported via amazon-mwaa-docker-images repo.
About aws-mwaa-local-runner
This repository provides a command line interface (CLI) utility that replicates an Amazon Managed Workflows for Apache Airflow (MWAA) environment locally.
Please note: MWAA/AWS/DAG/Plugin issues should be raised through AWS Support or the Airflow Slack #airflow-aws channel. Issues here should be focused on this local-runner repository.
Please note: The dynamic configurations which are dependent on the class of an environment are aligned with the Large environment class in this repository.
About the CLI
The CLI builds a Docker container image locally that’s similar to a MWAA production image. This allows you to run a local Apache Airflow environment to develop and test DAGs, custom plugins, and dependencies before deploying to MWAA.
What this repo contains
Prerequisites
mwaa-local-env. Please follow Windows Subsystem for Linux Installation (WSL) and Using Docker in WSL 2, to get started.Get started
Step one: Building the Docker image
Build the Docker container image using the following command:
Note: it takes several minutes to build the Docker image locally.
Step two: Running Apache Airflow
Local runner
Runs a local Apache Airflow environment that is a close representation of MWAA by configuration.
To stop the local environment, Ctrl+C on the terminal and wait till the local runner and the postgres containers are stopped.
Step three: Accessing the Airflow UI
By default, the
bootstrap.shscript creates a username and password for your local Airflow environment.admintestAirflow UI
Step four: Add DAGs and supporting files
The following section describes where to add your DAG code and supporting files. We recommend creating a directory structure similar to your MWAA environment.
DAGs
dags/folder.example_dag_with_taskflow_api.pyfile.Requirements.txt
requirements/requirements.txt.Let’s say you add
aws-batch==0.6to yourrequirements/requirements.txtfile. You should see an output similar to:For example usage see Installing Python dependencies using PyPi.org Requirements File Format Option two: Python wheels (.whl).
Custom plugins
requirements/requirements.txt.Note: this step assumes you have a DAG that corresponds to the custom plugin. For example usage MWAA Code Examples.
Startup script
startup.shlocated in a directory at the root of this repository calledstartup_script.startup.shscript.startup.shwithout running Apache Airflow, use the following script:What’s next?
FAQs
The following section contains common questions and answers you may encounter when using your Docker container image.
Can I test execution role permissions using this repository?
docker/config/.env.localrunnerenv file. To learn more about AWS environment variables, see Environment variables to configure the AWS CLI and Using temporary security credentials with the AWS CLI. Simply set the relevant environment variables in.env.localrunnerand./mwaa-local-env start.How do I add libraries to requirements.txt and test install?
requirements.txtfile is included in the/requirementsfolder of your local Docker container image. We recommend adding libraries to this file, and running locally.What if a library is not available on PyPi.org?
--index-urlflag to the package in yourrequirements/requirements.txtfile. To learn more, see Managing Python dependencies in requirements.txt.Troubleshooting
The following section contains errors you may encounter when using the Docker container image in this repository.
My environment is not starting
process fails with "dag_stats_table already exists", you’ll need to reset your database using the following command:./db-datafolder. Note, too, that newer Airflow versions have newer provider packages, which may require updating your DAG code.Fernet Key InvalidToken
A Fernet Key is generated during image build (
./mwaa-local-env build-image) and is durable throughout all containers started from that image. This key is used to encrypt connection passwords in the Airflow DB. If changes are made to the image and it is rebuilt, you may get a new key that will not match the key used when the Airflow DB was initialized, in this case you will need to reset the DB (./mwaa-local-env reset-db).Security
See CONTRIBUTING for more information.
License
This library is licensed under the MIT-0 License. See the LICENSE file.