📖 Scenario: You work as a cloud engineer managing multiple cloud resources. Your goal is to identify resources that cost more than a certain threshold and mark them for review to optimize costs.
🎯 Goal: Build an Airflow DAG that loads a list of cloud resources with their costs, sets a cost threshold, filters resources exceeding this threshold, and prints the list of expensive resources for review.
📋 What You'll Learn
Create a dictionary of cloud resources with their monthly costs
Set a cost threshold variable
Filter resources with costs above the threshold using a comprehension
Print the filtered list of expensive resources
💡 Why This Matters
🌍 Real World
Cloud engineers often need to monitor and optimize cloud resource costs to avoid overspending.
💼 Career
This project teaches how to filter and identify costly cloud resources programmatically, a key skill in cloud cost management roles.
Progress0 / 4 steps
1
Create the cloud resources cost dictionary
Create a dictionary called cloud_resources with these exact entries: 'VM1': 120, 'DB1': 80, 'Storage1': 45, 'VM2': 200, 'Cache1': 30 representing resource names and their monthly costs.
Apache Airflow
Hint
Use curly braces to create a dictionary with keys as resource names and values as costs.
2
Set the cost threshold
Create a variable called cost_threshold and set it to 100 to represent the monthly cost limit for resources.
Apache Airflow
Hint
Just assign the number 100 to the variable cost_threshold.
3
Filter expensive resources
Use a dictionary comprehension to create a new dictionary called expensive_resources that contains only the resources from cloud_resources with costs greater than cost_threshold. Use name and cost as the loop variables.
Apache Airflow
Hint
Use a dictionary comprehension with for name, cost in cloud_resources.items() and an if condition.
4
Print the expensive resources
Write a print statement to display the expensive_resources dictionary.
Apache Airflow
Hint
Use print(expensive_resources) to show the filtered dictionary.
Practice
(1/5)
1. What is the main goal of cost optimization for cloud resources in Airflow?
easy
A. To increase the number of cloud resources used
B. To run all tasks continuously without stopping
C. To use cloud resources only when needed to save money
D. To avoid monitoring cloud resource usage
Solution
Step 1: Understand cost optimization meaning
Cost optimization means using resources efficiently to reduce expenses.
Step 2: Relate to Airflow usage
Airflow helps automate resource usage only when needed, saving money.
Final Answer:
To use cloud resources only when needed to save money -> Option C
Quick Check:
Cost optimization = Use resources only when needed [OK]
Hint: Cost optimization means saving money by using resources smartly [OK]
Common Mistakes:
Thinking more resources always mean better performance
Ignoring the need to stop unused resources
Believing monitoring is unnecessary
2. Which Airflow feature helps automate starting and stopping cloud resources on a schedule?
easy
A. DAG scheduling
B. Manual triggering only
C. Static resource allocation
D. Ignoring resource states
Solution
Step 1: Identify Airflow automation features
DAG scheduling allows tasks to run automatically at set times.
Step 2: Connect scheduling to resource control
Scheduling can start or stop cloud resources based on time, saving costs.
Final Answer:
DAG scheduling -> Option A
Quick Check:
DAG scheduling = Automate resource start/stop [OK]
Hint: Airflow DAGs run tasks on schedule to control resources [OK]
Common Mistakes:
Thinking manual triggers automate resource control
Confusing static allocation with automation
Ignoring the role of scheduling in cost saving
3. Given this Airflow DAG snippet, what happens to cloud resources?
D. Missing comma between bash_command and trigger_rule
Solution
Step 1: Check syntax of BashOperator parameters
Parameters must be separated by commas; here comma is missing after bash_command.
Step 2: Identify cause of syntax error
Missing comma causes Python syntax error, preventing DAG parsing.
Final Answer:
Missing comma between bash_command and trigger_rule -> Option D
Quick Check:
Comma missing = Syntax error [OK]
Hint: Check commas between parameters in Python code [OK]
Common Mistakes:
Thinking trigger_rule is invalid parameter
Assuming BashOperator can't run shell commands
Believing task_id naming causes error
5. You want to optimize cloud costs by running an Airflow DAG that starts instances only on weekdays at 9 AM and stops them at 6 PM. Which schedule_interval and task setup is best?
hard
A. schedule_interval='0 9 * * 1-5' to start, and a separate DAG with '0 18 * * 1-5' to stop
B. schedule_interval='@daily' for both start and stop tasks in one DAG
C. schedule_interval='0 9 * * *' to start and stop in same DAG immediately
D. No schedule_interval, run tasks manually when needed
Solution
Step 1: Choose schedule for weekdays only
'0 9 * * 1-5' runs at 9 AM Monday to Friday, matching requirement.
Step 2: Separate start and stop times
Stopping at 6 PM requires a different schedule '0 18 * * 1-5', best in separate DAG for clarity.
Final Answer:
schedule_interval='0 9 * * 1-5' to start, and a separate DAG with '0 18 * * 1-5' to stop -> Option A
Quick Check:
Separate schedules for start and stop on weekdays [OK]
Hint: Use cron for weekdays and separate DAGs for start/stop [OK]
Common Mistakes:
Using '@daily' runs every day, not weekdays only
Starting and stopping immediately in one DAG
Relying on manual runs instead of automation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.