Airflow in ECS with Redis - Part 1: Overview

I'm Mariano, I am from 🇦🇷 and live in 🇳🇱. I am a Cloud Engineer 9-5 and many other things 5-9.
Search for a command to run...

I'm Mariano, I am from 🇦🇷 and live in 🇳🇱. I am a Cloud Engineer 9-5 and many other things 5-9.
No comments yet. Be the first to comment.
Here is how to deploy Airflow in ECS in different ways, from Terraform to docker compose native integration.
Previously in Deploying Airflow in ECS using S3 as DAG storage via Terraform, I described how to deploy all components in AWS ECS using a hybrid EC2/Fargate launch type and S3 as DAG storage. Now let's do the same, but with three main differences: ...
Add Infra and application code, mix it up, profit

A tote bag phrase applied to Terraform code

Or how to get richer information about someone's experience by asking differently

I like the CLI and a black background console when it comes to checking on things and debugging, it makes me feel more confident in what I'm seeing and closer to the basics. A big chunk of my recent years has been all about Kubernetes. Kubernetes th...

Wait, what? Don't panic, SOPS will encrypt them for you. Big NO NO Secrets (a.k.a. sensitive values), such as passwords or any other potentially harmful information must be protected, we all know that. That's why you should avoid sharing those or pu...

How to set up a containerised Airflow installation in AWS ECS using Redis as its queue orchestrator.
A few years ago I joined a Data team where we processed a lot of analytics information coming from online search engines. This ETL process consisted of three main stages: fetch raw data from external APIs, transform it into something meaningful for our applications and load that data into a database.
All this was carried on with in-house developed DAG scripts that were orchestrated via Airflow. The problem was that this intake was so lengthy and unstable that it was getting really painful for our daily performance.
Having previous ECS experience, I thought we could try improving the process using two main changes:
Decouple the Airflow controller from the runners, using ECS.
Add a queue orchestrator for improved parallelism that could shorten the processing times, using Airflow's native Celery executor (integration) with Redis.
Airflow is a DAG orchestrator, a platform that provides management for workflows.
You can schedule, monitor and even create workflows to execute a set of tasks in a predefined order and dependent on each other. Airflow will control these tasks execution while giving insights and a UI where this can all be monitored, among many other features.
Have a look at Airflow documentation for more information.
AWS ECS is a container orchestrator to deploy, manage and scale your containerised applications. It integrates with a lot of AWS services, which makes it very flexible and easy to use if your infrastructure is already in AWS.
Check out a few ECS uses cases.
Is an in-memory data store, used as a database, message broker, cache, etc.
AWS ElastiCache offers Redis as one of its available engines.
A bit more on Redis.

Airflow has many executor integrations. An executor is the mechanism by which tasks get run, either locally or remotely. So you can configure Airflow to have it locally executed - for small, standalone machine installations - or remotely if you are planning to run a considerable amount of tasks per workflow and have access to a pool of resources, like in this case, ECS.
One of the remote executors is Celery, which allows you to span out to many workers and supports different backends, such as RabbitMQ and Redis.
In this decentralised pattern, every worker has to have access to the scripts (DAGs) to be able to run their assigned task. To do so, you can mount an S3 bucket - containing all the scripts - as a local volume to all ECS tasks, using drivers such as s3fs or s3fs-fuse, rexray/s3fs, etc.
This S3 access is possible by adding the required IAM permissions to the ECS tasks, allowing both scheduler and worker to interact with the mounted bucket.
The whole idea of this pattern is to separate the brain from the muscle. Using ECS services, you can create a service for the scheduler (controller) and another for the worker tasks, controlled by the executor.
The former would run with a minimum of 1 task to keep things running, along with access to its webserver (UI). The latter will scale up/down depending on the scheduler sent workload.
The IAM credentials are fetched from AWS SecretsManager or Parameter Store and mounted as environment variables on each task.
The S3 mounting driver installation is added to each ECS EC2 instance userData so it gets installed, initialised and mounted.
Use a git repository as DAGs datastore and have each runner download that repo in startup time.
Docker added an ECS integration for its docker compose. This is done by previously configuring Docker context to ECS.
With this integration, there is no need to create ECS tasks definitions, you can directly work with Airflow's docker-compose.yaml file and create all infrastructure resources in AWS (via native CloudFormation integration).
This integration was added in 2020 (way after my experience), so I will also include this new deployment approach in the next posts of this series.
Explore Airflow's Kubernetes executor.
I will be sharing a working example of the original implementation and an improved version using docker compose ECS integration.
Thank you for stopping by! Do you know other ways to do this? Please let me know in the comments, I always like to learn how to do things differently.