Containers
Hosting Amazon Managed Workflows for Apache Airflow (MWAA) Local-runner on Amazon ECS Fargate for development and testing
Introduction
Data scientists and engineers have made Apache Airflow a leading open-source tool to create data pipelines due to its active open-source community, familiar Python development as Directed Acyclic Graph (DAG) workflows, and an extensive library of pre-built integrations. Amazon Managed Workflows for Apache Airflow (MWAA) is a managed service for Apache Airflow that makes it easy to run Airflow on AWS without the operational burden of having to manage the underlying infrastructure.
While business needs demand scalability, availability, and security, Airflow development often doesn’t require full production-ready infrastructure. Many DAGs are written locally, and when doing so, developers need to be assured that these workflows function correctly when they’re deployed to their production environment. To that end, the MWAA team created an open-source local-runner that uses many of the same library versions and runtimes as MWAA in a container that can run in a local Docker instance, along with utilities that can test and package Python requirements.
There are times when a full MWAA environment isn’t required, but a local Docker container doesn’t have access to the AWS resources needed to properly develop and test end-to-end workflows. As such, the answer may be to run local-runner on a container on AWS, and by running on the same configuration as MWAA you can closely replicate your production MWAA environment in a light-weight development container. This post covers the topic of launching MWAA local-runner containers on Amazon Elastic Container Service (ECS) Fargate.
Prerequisites
- This tutorial assumes you have an existing Amazon MWAA environment and wish to create a development container with a similar configuration. If you don’t already have an MWAA environment, then you can follow the quick start documentation here to get started.
- Docker on your local desktop.
- AWS Command Line Interface (AWS CLI).
- Terraform CLI (only if using Terraform).
Walkthrough
- Clone the local-runner repository, set the environment variables, and build the image
We’ll start by pulling the latest Airflow version of the Amazon MWAA local-runner to our local machine.
Note: Replace <your_region> with your region and <airflow_version> with the version specified here.
Note: We’re expressly using the latest version of the Amazon MWAA local-runner as it supports the functionality needed for this tutorial.
2. Push your local-runner image to Amazon ECR
Modify the MWAA execution role
For this example, we enable an existing MWAA role to work with Amazon ECS Fargate. As an alternative ,you may also create a new task execution role.
- From the Amazon MWAA console, select the link of the environment whose role you wish to use for your Amazon ECS Fargate local-runner instance.
- Scroll down to Permissions and select the link to open the Execution role.
- Select the Trust relationships tab.
- Choose Edit trust policy.
- Under Statement -> Principal -> Service add ecs-tasks.amazonaws.com.
6. Select Update policy.
7. Choose the Permissions tab.
8. Select the link to the MWAA-Execution-Policy.
9. Choose Edit policy.
10. Choose the JSON tab.
11. In the Statement section describing logs permissions, under Resource, add arn:aws:logs:us-east-1:012345678910:log-group:/ecs/mwaa-local-runner-task-definition:*, where 012345678910 is replaced with your account number and us-east-1 is replaced with your region.
12. We also want to add permissions that allow us to execute commands on the container and pull the image from Amazon ECR.
Note: Ensure that your private subnets have access to AWS Systems Manager (SSM) via Internet Gateway or PrivateLink to “com.amazonaws.us-east-1.ssmmessages” in order to enable command execution
13. Choose Review policy.
14. Choose Save changes.
The creation of the Aurora Postgress Serverless instance and Amazon ECS resources can either be done using AWS CloudFormation or Terraform, as per the following sections. To create the resources required, clone the aws-samples/amazon-mwaa-samples repository.
Take note of the variables from the existing MWAA environment needed to create the Amazon ECS environment (i.e., security groups, subnet IDs, Virtual Private Cloud (VPC) ID, and execution role).
AWS CloudFormation
- Navigate to the ECS CloudFormation directory:
- Update the AWS CloudFormation template input parameters file parameter-values.json in your favorite code editor (e.g., vscode).
- [Optional] Additional AWS CloudFormation template input parameter values can be overridden in either template directly (mwaa-ecs-on-fargate.yml) or supplied in input parameter file in step # 2.
- Deploy the AWS CloudFormation template.
Where …
- Stack-name – AWS CloudFormation Stack name is e.g., mwaa-ecs-sandbox
- Region – where you want to install the stack. It can be sourced from env variable or replaced with the value e.g., ap-east-2, us-west-2
- Template-file – CF template name in subfolder mwaa-on-ecs-fargate.yml
- Parameter – overrides is updated input parameter file with your environment values in step 2
It takes time (up to 40 minutes) to create required Amazon ECS and Amazon Relational Database Service (RDS) resources before showing output on successful completion as …
- To test validate the deployed environment, lets get the output parameters AWS CloudFormation template generated including Load Balancer with AWS CloudFormation describe command as:
- To test validate the local runner on Amazon ECS Fargate, go to Access Airflow Interface Step below after the Terraform steps.
Terraform
- Navigate to the ECS Terraform directory:
- Create the tfvars file that contains all the required parameters. Replace all the parameters with the required parameters for your configuration.
- Initialize the Terraform modules and plan the environment to create the RDS Aurora Serverless database. The subnet IDs and security group IDs of your environment can be retrieved from the previous step.
Note:
- Make use of the existing MWAA Environment subnets, VPC, and security groups.
- The security group also needs to allow traffic to itself.
- The security group needs allow traffic from your local machine on port 80 to access the loadbalancer URL.
Note: you may face the error create: ExpiredToken: The security token included in the request is expired │ status code: 403. If you do face this error, untaint the RDS resource and re-apply.
Access the Airflow user interface
- Direct your browser to the Application Load Balancer (ALB) URL from the AWS Cloudformation/Terraform output, being sure to preface with http (mwaa-local-runner-alb-552640779.us-east-1.elb.amazonaws.com/home).
Note: If you chose an internal ALB, you’ll need to be on your VPC private subnet via VPN or similar.
- When presented with the Airflow user interface, provide the username admin and the default password specified as test1234.
- You now are in a standard Airflow deployment that closely resembles the configuration of MWAA using local-runner.
Updating the environment
- When you stop and restart the Amazon ECS Fargate task, the dags, plugins, and requirements will be re-initialized. This can be done through a forced update:
- If you wish to do so without restarting the task, you may run the command directly via execute-command:
-
- If this is your first time running execute-command then we need to update the service to allow this functionality:
-
- When the AWS Fargate task resumes availability, we need to know the task ID:
-
- This returns a JSON string that contains an ARN with the unique task ID in the format:
In this case 11aa22bb33cc44dd55ee66ff77889900, which we’ll use in the next command:
Note: You may need to install Session Manager in order to execute commands via the AWS CLI.
-
- At this point you can run any activities you wish, such as execute the s3 sync command to update your dags:
Or view your scheduler logs:
-
- When complete, type exit to return to your terminal.
Prerequisites
Cleaning up
- If no longer needed, be sure to delete your AWS Fargate cluster, task definitions, ALB, Amazon ECR repository, Aurora RDS instance, and any other items you do not wish to retain.
- With AWS Cloudformation, delete the stack.
-
- With terraform, run
Important: Terminating resources that aren’t actively being used reduces costs and is a best practice. Not terminating your resources can result in additional charges.
Conclusion
In this post, we showed you how to configure Amazon MWAA open-source local-runner container image on Amazon ECS Fargate containers to provide a development and testing environment, using Amazon Aurora Serverless v2 as the database backend and execute-command on the AWS Fargate task to interact with the system.
To learn more about Amazon MWAA visit the Amazon MWAA documentation. For more blog posts about Amazon MWAA, please visit the Amazon MWAA resources page.