AWS operators (S3, Redshift, EMR) in Apache Airflow - Time & Space Complexity
When using AWS operators in Airflow, it's important to understand how the number of tasks affects execution time.
We want to know how the time to run grows as we add more AWS operations like S3 uploads or Redshift queries.
Analyze the time complexity of the following operation sequence.
from airflow import DAG
from airflow.providers.amazon.aws.operators.s3 import S3CreateObjectOperator
from airflow.providers.amazon.aws.operators.redshift import RedshiftSQLOperator
from airflow.providers.amazon.aws.operators.emr import EmrCreateJobFlowOperator
with DAG('aws_batch_dag') as dag:
for i in range(n):
s3_task = S3CreateObjectOperator(task_id=f's3_upload_{i}', ...)
redshift_task = RedshiftSQLOperator(task_id=f'redshift_query_{i}', ...)
emr_task = EmrCreateJobFlowOperator(task_id=f'emr_job_{i}', ...)
This sequence runs n sets of AWS tasks: uploading to S3, running a Redshift query, and starting an EMR job.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Each iteration runs three AWS API calls: one to S3, one to Redshift, and one to EMR.
- How many times: Each of these calls happens once per iteration, so 3 times n in total.
As you increase n, the number of AWS calls grows directly with n.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 30 (3 calls x 10) |
| 100 | 300 (3 calls x 100) |
| 1000 | 3000 (3 calls x 1000) |
Pattern observation: The total operations increase steadily and directly with the number of iterations.
Time Complexity: O(n)
This means the total time grows in a straight line as you add more AWS tasks.
[X] Wrong: "Adding more AWS tasks won't affect total time much because they run in the cloud."
[OK] Correct: Each AWS call takes time and resources, so more tasks mean more total time, even if they run in parallel.
Understanding how task count affects execution helps you design efficient workflows and explain your choices clearly.
"What if we changed the tasks to run in parallel instead of sequentially? How would the time complexity change?"
Practice
S3CreateObjectOperator in Airflow?Solution
Step 1: Understand the operator's name and function
The nameS3CreateObjectOperatorsuggests it creates or uploads an object to S3.Step 2: Match the operator to its main action
This operator is designed to create or upload files to an S3 bucket, not delete or list buckets.Final Answer:
To create or upload an object (file) to an S3 bucket -> Option CQuick Check:
S3CreateObjectOperator = Upload file to S3 [OK]
- Confusing object creation with bucket deletion
- Thinking it lists objects instead of creating
- Assuming it moves objects between buckets
RedshiftSQLOperator in Airflow?Solution
Step 1: Identify correct parameter names for RedshiftSQLOperator
The operator expectssqlfor the query andredshift_conn_idfor the connection ID.Step 2: Check each option for correct parameters
RedshiftSQLOperator(sql='SELECT * FROM table', redshift_conn_id='my_conn') usessqlandredshift_conn_id, which is correct. Others use wrong parameter names.Final Answer:
RedshiftSQLOperator(sql='SELECT * FROM table', redshift_conn_id='my_conn') -> Option DQuick Check:
Usesqlandredshift_conn_idfor RedshiftSQLOperator [OK]
- Using 'query' instead of 'sql' parameter
- Using 'aws_conn_id' instead of 'redshift_conn_id'
- Misspelling parameter names
EmrCreateJobFlowOperator:
emr_task = EmrCreateJobFlowOperator(
task_id='start_emr_cluster',
job_flow_overrides={
'Name': 'TestCluster',
'ReleaseLabel': 'emr-6.3.0',
'Instances': {'InstanceCount': 3, 'KeepJobFlowAliveWhenNoSteps': True}
},
aws_conn_id='aws_default'
)
What will be the state of the EMR cluster after this task runs successfully?Solution
Step 1: Analyze job_flow_overrides parameters
The cluster is named 'TestCluster', uses EMR release 6.3.0, has 3 instances, and is set to keep alive when no steps run.Step 2: Understand KeepJobFlowAliveWhenNoSteps effect
This setting keeps the cluster running after creation, waiting for steps to be added.Final Answer:
A running EMR cluster named 'TestCluster' with 3 instances, waiting for steps -> Option AQuick Check:
KeepJobFlowAliveWhenNoSteps=True means cluster stays running [OK]
- Assuming cluster terminates immediately
- Ignoring instance count setting
- Thinking ReleaseLabel is missing
RedshiftToS3Operator:
copy_task = RedshiftToS3Operator(
task_id='copy_data',
redshift_conn_id='redshift_default',
s3_bucket='my-bucket',
s3_key='data/output/',
sql='SELECT * FROM users'
)
But the task fails with an error about missing parameters. What is the likely cause?Solution
Step 1: Check operator purpose and name
RedshiftToS3Operatorcopies data from Redshift to S3, but the task description says copying from S3 to Redshift.Step 2: Identify correct operator for S3 to Redshift copy
The correct operator for copying data from S3 to Redshift isS3ToRedshiftOperator.Final Answer:
The operator name is incorrect; it should be S3ToRedshiftOperator -> Option BQuick Check:
RedshiftToS3Operator copies Redshift to S3, not reverse [OK]
- Mixing up RedshiftToS3Operator and S3ToRedshiftOperator
- Assuming sql parameter works for all operators
- Incorrect S3 URI format
Solution
Step 1: Identify the logical order of tasks
First, upload the CSV to S3 usingS3CreateObjectOperator.Step 2: Load data into Redshift using COPY command via
After the file is in S3, run the COPY command in Redshift to load data.RedshiftSQLOperatorStep 3: Start EMR cluster with
Finally, start the EMR cluster to process the loaded data.EmrCreateJobFlowOperatorfor processingFinal Answer:
S3CreateObjectOperator -> RedshiftSQLOperator -> EmrCreateJobFlowOperator -> Option AQuick Check:
Upload -> Load -> Process = A [OK]
- Running Redshift load before uploading to S3
- Starting EMR before data is loaded
- Mixing operator order
Ready to prove it?
Pass the test and this pattern moves unknown
