📖 Scenario: You are working as a DevOps engineer managing workflows in Apache Airflow. You want to automate infrastructure tasks like creating and deleting cloud resources using Airflow's cloud operators. This project will help you understand how cloud operators simplify these tasks by wrapping complex API calls into easy-to-use Python classes.
🎯 Goal: Build a simple Airflow DAG that uses a cloud operator to create a cloud storage bucket and then delete it. This will show how cloud operators reduce manual scripting and make infrastructure automation easier.
📋 What You'll Learn
Create a Python dictionary called default_args with Airflow DAG default parameters
Create a DAG object called cloud_operator_dag with the ID 'cloud_operator_demo'
Use the GCSCreateBucketOperator to create a bucket named 'my-test-bucket'
Use the GCSDeleteBucketOperator to delete the bucket named 'my-test-bucket'
Set task dependencies so the bucket is created before it is deleted
Print the DAG ID at the end
💡 Why This Matters
🌍 Real World
DevOps engineers use cloud operators in Airflow to automate cloud infrastructure tasks without writing complex API code.
💼 Career
Knowing how to use cloud operators helps you automate and manage cloud resources efficiently, a key skill for DevOps and cloud engineering roles.
Progress0 / 4 steps
1
Set up Airflow DAG default arguments
Create a dictionary called default_args with these exact entries: 'owner': 'airflow', 'start_date': datetime(2024, 1, 1), and 'retries': 1. Import datetime from the datetime module.
Import DAG from airflow. Create a DAG object called cloud_operator_dag with the ID 'cloud_operator_demo', using default_args and a schedule interval of '@daily'.
Apache Airflow
Hint
Use cloud_operator_dag = DAG(dag_id='cloud_operator_demo', default_args=default_args, schedule_interval='@daily').
3
Add cloud operators to create and delete a bucket
Import GCSCreateBucketOperator and GCSDeleteBucketOperator from airflow.providers.google.cloud.operators.gcs. Create two tasks inside cloud_operator_dag: create_bucket using GCSCreateBucketOperator to create a bucket named 'my-test-bucket', and delete_bucket using GCSDeleteBucketOperator to delete the same bucket. Set create_bucket to run before delete_bucket.
Apache Airflow
Hint
Use create_bucket = GCSCreateBucketOperator(task_id='create_bucket', bucket_name='my-test-bucket', dag=cloud_operator_dag) and similarly for delete_bucket. Then set create_bucket >> delete_bucket.
4
Print the DAG ID
Write a print statement to display the DAG ID of cloud_operator_dag.
Apache Airflow
Hint
Use print(cloud_operator_dag.dag_id) to show the DAG ID.
Practice
(1/5)
1. What is the main benefit of using cloud operators in Airflow for infrastructure tasks?
easy
A. They automate resource management, reducing manual work and errors.
B. They require manual configuration of every cloud resource.
C. They slow down workflow execution by adding extra steps.
D. They only work with local servers, not cloud platforms.
Solution
Step 1: Understand cloud operators' role
Cloud operators automate managing cloud resources, so you don't do it manually.
Step 2: Identify the benefit
This automation reduces errors and saves time, making workflows simpler and more reliable.
Final Answer:
They automate resource management, reducing manual work and errors. -> Option A
Quick Check:
Automation reduces manual errors [OK]
Hint: Cloud operators automate tasks to reduce errors and save time [OK]
Common Mistakes:
Thinking cloud operators add manual steps
Believing they only work on local servers
Assuming they slow down workflows
2. Which of the following is the correct way to use a cloud operator in an Airflow DAG?
easy
A. my_task = CloudOperator(task_id='run')
B. my_task = CloudOperator('run', 'my-project')
C. my_task = CloudOperator(task='run', project='my-project')
D. my_task = CloudOperator(task_id='run', project_id='my-project')
Solution
Step 1: Check parameter names for cloud operators
Cloud operators require named parameters like task_id and project_id for clarity and correctness.
B. The parameter 'project' is incorrect; it should be 'project_id'.
C. CloudOperator requires no parameters at all.
D. The task_id 'deploy' is a reserved keyword.
Solution
Step 1: Check parameter names for CloudOperator
CloudOperator expects 'project_id', not 'project', so using 'project' causes an error.
Step 2: Validate other parameters
task_id as string is correct; no reserved keyword issue; parameters are required.
Final Answer:
The parameter 'project' is incorrect; it should be 'project_id'. -> Option B
Quick Check:
Correct parameter names avoid errors [OK]
Hint: Use exact parameter names like project_id [OK]
Common Mistakes:
Using 'project' instead of 'project_id'
Thinking task_id must be a number
Assuming CloudOperator needs no parameters
5. You want to simplify your Airflow workflow that creates and deletes cloud resources. Which approach best uses cloud operators to reduce manual infrastructure tasks?
hard
A. Use separate cloud operators for creating and deleting resources, linked in the DAG.
B. Write custom shell scripts and call them with BashOperator instead of cloud operators.
C. Manually create resources outside Airflow and only use Airflow for monitoring.
D. Use a single PythonOperator to run all cloud commands as one script.
Solution
Step 1: Understand cloud operators' purpose
Cloud operators automate cloud resource tasks like create and delete, making workflows simpler.
Step 2: Choose best practice for workflow design
Using separate cloud operators for each task linked in DAG keeps tasks clear, manageable, and reliable.
Final Answer:
Use separate cloud operators for creating and deleting resources, linked in the DAG. -> Option A
Quick Check:
Separate cloud operators simplify tasks [OK]
Hint: Link separate cloud operators for each resource task [OK]
Common Mistakes:
Using BashOperator instead of cloud operators
Doing manual resource management outside Airflow
Combining all commands in one PythonOperator
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.