14.0 Automation with Python Flashcards

(83 cards)

1
Q

What is Python primarily used for in DevOps automation?

A

Automating repetitive tasks such as:
- provisioning
- backups
- cleanup
- monitoring
- tagging
- status checks

Python’s versatility makes it suitable for various automation tasks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is Python useful for automating AWS tasks?

A

It provides full programmatic control, logic branching, loops, scheduling, and dynamic interaction with AWS services.

This allows for complex automation workflows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Python package/library?

A

A reusable chunk of code that you can import into your own Python programs.

Libraries help streamline development by providing pre-written functions that extend Python’s functionality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Boto3?

A

AWS’s official Python SDK used to create, configure, and manage AWS services such as EC2, S3, IAM, and EKS.

Boto3 simplifies interactions with AWS APIs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What must you do before using Boto3?

…besides installing it first

A

Configure AWS credentials

Access Key, Secret Key, optional Session Token

Proper credential management is crucial for security.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What two types of APIs does Boto3 provide to interact with AWS services?

A
  • Resource API (readable, high-level, cleaner code)
  • Client API (more control, low-level, exact AWS API)

Use Resource API when…
- You want clean, readable code
- You’re using common AWS operations
- You want automatic pagination

Use Client API when…
- You need the newest AWS features
- You want exact control and behavior
- You’re following AWS API reference docs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is pagination in programming?

A

The technique of dividing a large dataset into smaller, manageable chunks or “pages”.

It optimizes performance by reducing server load, accelerating API responses, and improves user experience by presenting information incrementally rather than all at once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does pagination optimize performance?

3 things…

A

It presents information incrementally rather than all at once. This…
- reduces server load
- accelerates API responses
- improves user experience

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why do different clouds require different Python libraries?

A

Since each cloud provider has their own set of services, often with unique names and processes, they must have their own SDK (Azure SDK for Python, Google Cloud Python SDK).

This ensures compatibility with specific cloud services.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why is Terraform better for infrastructure provisioning than Python?

A

It is idempotent, maintains state, and ensures desired state matches real infrastructure.

Terraform’s design allows for safer infrastructure changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why is Python less suited for provisioning?

A

Python is imperative and does not track state automatically — developer must handle logic manually.

This can lead to inconsistencies in resource management.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is Python better suited for than Terraform?

A

Complex workflows, conditional logic, maintenance tasks, automation scripts, and operational loops.

Python excels in scenarios requiring dynamic decision-making.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why automate EC2 status checks using Python?

A

To monitor instance health at scale and react to failures automatically.

Automation enhances reliability and reduces downtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What tool do you need for Python to run checks automatically?

A

A scheduler (cron, Airflow, CloudWatch scheduled events, etc.).

Scheduling ensures tasks run at defined intervals without manual intervention.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why automate EC2 tagging?

A

To ensure consistent metadata across hundreds of servers for cost allocation and management.

Proper tagging aids in resource organization and billing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an EC2 volume snapshot?

A

A copy of an EBS volume used for backup and disaster recovery.

Snapshots are essential for data protection in AWS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Why automate EBS snapshot creation?

A

To ensure regular backups for all volumes without manual effort.

Automation minimizes the risk of data loss.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Why automate snapshot cleanup?

A

To avoid accumulating unnecessary snapshots that increase cost.

Regular cleanup helps manage storage expenses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Why automate volume restoration with Python?

A

To recover failed volumes quickly for hundreds of instances.

Speedy recovery is critical for maintaining service availability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Why automate website monitoring with Python?

A

To detect downtime and trigger alerts or auto-fixes (restart containers, restart servers).

Monitoring ensures high availability of web services.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does a website monitoring script typically check?

A

Response code, latency, and availability of the service.

These metrics are vital for assessing service performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What actions might a Python script take when a service is down?

A
  • Restart Docker containers
  • Restart EC2 instances
  • Trigger failover

Automated responses can minimize downtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Why is error handling more important in Python automation than in Terraform?

A

Python lacks built-in state tracking — failures may leave resources in half-created states.

Proper error handling is essential for maintaining infrastructure integrity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the correct approach when snapshot creation fails?

A

Delete the failed snapshot and retry.

This prevents resource clutter and maintains operational efficiency.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is dangerous behavior regarding **failed snapshots**?
Leaving broken snapshots undeleted. ## Footnote This can lead to unnecessary costs and management overhead.
26
What should you always do after catching an **exception**?
Log the error for debugging. ## Footnote Logging helps in diagnosing issues and improving scripts.
27
What is the general best practice for handling **failure** in automated scripts?
Roll back partially created resources to maintain consistency. ## Footnote This ensures that infrastructure remains in a stable state.
28
Why use **Resource API** when automating with Boto3?
For simpler, more object-oriented operations (e.g., `ec2.Instance('id')`). ## Footnote Resource API abstracts some complexities of AWS service interactions.
29
Why use **Client API** in Boto3?
For fine-grained control and AWS feature completeness. ## Footnote Client API allows access to all AWS service features.
30
How do you get all EC2 instances with a specific **tag** using Boto3?
Filter instances using `describe_instances(Filters=[...])`. ## Footnote Tag filtering is crucial for managing resources effectively.
31
Why use Python to automate **EC2 provisioning** instead of clicking in the console?
Repeatability, speed, consistency, cost management, and reduced human error. ## Footnote Automation enhances operational efficiency.
32
Why is Python not **idempotent**?
It executes instructions directly without automatically syncing desired vs actual state. ## Footnote This can lead to unintended changes in infrastructure.
33
How can Python emulate **idempotency**?
By checking the current resource state before performing actions. ## Footnote This approach helps maintain desired infrastructure states.
34
What tools can **schedule Python automation tasks**?
* Cron jobs * AWS Lambda * AWS EventBridge * Airflow * Jenkins scheduled jobs ## Footnote Scheduling tools help automate tasks at regular intervals.
35
Why use scheduled automation for **snapshots or health checks**?
To ensure consistent protection and monitoring without manual execution. ## Footnote Regular scheduling is key to maintaining system health.
36
Why should Python scripts never **hardcode AWS keys**?
Hardcoding keys risks exposure and security breaches. ## Footnote Secure credential management is essential for protecting AWS resources.
37
How should AWS credentials be provided **securely** to Python?
Through IAM Roles, environment variables, or AWS CLI credential store. ## Footnote These methods enhance security and reduce risks.
38
Why should Python logs avoid printing **credentials**?
Logs may be stored or shared; sensitive data must not leak. ## Footnote Protecting sensitive information is crucial for security.
39
What kinds of **maintenance tasks** can be automated with Python?
* Cleanup (snapshots, unused volumes) * Patching * Monitoring * Resource optimization ## Footnote Automation helps maintain system performance and reliability.
40
Why automate in **large organizations**?
Scalability — tasks must run across hundreds of servers and environments. ## Footnote Automation is essential for managing large-scale infrastructures.
41
What is the key difference between **'GET info'** vs **'TAKE ACTION'** automation scripts?
Info scripts read-only; action scripts modify infrastructure and need stronger safeguards. ## Footnote Understanding this distinction is important for script design.
42
Why break automation into multiple **small scripts** instead of one large one?
Easier debugging, better testing, clearer responsibilities. ## Footnote Modular scripts enhance maintainability and clarity.
43
Why might **volume attachment fail**?
Instance not in correct state, wrong AZ, corrupted snapshot, or IAM role missing permissions. ## Footnote Identifying potential failure points is crucial for troubleshooting.
44
Why might **snapshot-based restoration fail**?
Snapshot is incomplete, corrupted, or volume type mismatch. ## Footnote Understanding these issues helps in planning for successful restorations.
45
Why should failures trigger **alerts**?
Automation without visibility can cause silent infrastructure failures. ## Footnote Alerting mechanisms are vital for operational awareness.
46
What is the difference between **boto3.client()** and **boto3.resource()**?
* client() → low-level AWS API access, complete feature set * resource() → object-oriented, simpler for common tasks ## Footnote Understanding the distinction helps in choosing the right method for AWS interactions.
47
Why are **paginators** important in Boto3?
AWS API results are limited (often 1,000 items), so paginators allow retrieving *all* pages of results. ## Footnote This ensures comprehensive data retrieval in automation scripts.
48
What is a **Boto3 Waiter** used for?
Built-in AWS mechanism that waits until a resource reaches a specific state (e.g., instance running). ## Footnote Waiters help manage resource states effectively in automation.
49
Why must you implement **retry logic** in AWS automation?
AWS API throttles requests; retries prevent automation failures during rate limits. ## Footnote This is crucial for maintaining reliability in automation tasks.
50
Why is Python NOT inherently **idempotent** for automation?
Python performs imperative actions directly without tracking resource state. ## Footnote Understanding idempotency is key for safe automation practices.
51
How can Python scripts emulate **idempotency**?
By checking existing resource state before creating/modifying anything. ## Footnote This approach minimizes unintended changes in infrastructure.
52
Why is **plan mode** useful for automation scripts?
It simulates changes before execution, reducing risk. ## Footnote This feature allows for safer testing of automation logic.
53
Why prefer **event-driven automation** over scheduled scripts?
Event-driven triggers act *immediately* and avoid unnecessary runs. ## Footnote This leads to more efficient resource utilization.
54
What tools support **event-driven automation** in AWS?
* Lambda * SNS * SQS * CloudWatch Alarms * EventBridge Rules ## Footnote These tools facilitate responsive automation workflows.
55
What is an example of **event-driven automation**?
Automatically snapshotting a volume when a CloudWatch alarm triggers. ## Footnote This showcases the practical application of event-driven strategies.
56
Why is **parallel execution** important in automation?
Because DevOps often requires working with hundreds of instances at once. ## Footnote Parallel execution enhances efficiency in large-scale operations.
57
What Python module is commonly used for **parallel AWS automation**?
`concurrent.futures.ThreadPoolExecutor`. ## Footnote This module simplifies the implementation of parallel tasks.
58
When is **async programming** useful in AWS automation?
For I/O-heavy tasks such as many API calls or pulling status simultaneously. ## Footnote Async programming optimizes performance in high-latency operations.
59
What is the **Control Script → Worker Scripts** pattern?
A main script orchestrates specialized sub-scripts that process subsets of resources. ## Footnote This pattern enhances modularity and maintainability.
60
What is **serverless automation** in AWS?
Python automation running through Lambda, Step Functions, or EventBridge. ## Footnote Serverless architecture reduces overhead and simplifies deployment.
61
What is the benefit of using **ECS or K8s CronJobs** for Python automation?
They enable long-running or containerized scheduled tasks. ## Footnote This allows for efficient management of recurring automation tasks.
62
Why must Python automation use **structured JSON logging**?
Easier to index and search in logging systems like CloudWatch, ELK, or Loki. ## Footnote Structured logging improves observability and troubleshooting.
63
Why should automation scripts publish **metrics**?
Enables monitoring automation performance and detecting failures. ## Footnote Metrics provide insights into the effectiveness of automation efforts.
64
Why must Python automation send **alerts**?
To notify teams of failures, downtime, or unexpected conditions. ## Footnote Alerts are crucial for maintaining operational awareness.
65
What library is useful for **mocking AWS services** in tests?
`moto` — allows simulating AWS APIs locally. ## Footnote This library is essential for testing without incurring costs.
66
Why test automation in a **staging AWS account** first?
Prevents accidental modification or deletion of production infrastructure. ## Footnote This practice safeguards production environments.
67
Why is **unit testing** essential for automation tools?
Catch logical errors early and prevent infrastructure damage. ## Footnote Unit tests ensure reliability and correctness in automation scripts.
68
Why should automation logic be separated into **multiple scripts/modules**?
Improves readability, maintainability, and reduces risk of large script failures. ## Footnote Modular design enhances collaboration and code quality.
69
Why should automation be **config-driven**?
Increases reusability and prevents hardcoding values. ## Footnote Config-driven automation allows for easier updates and flexibility.
70
What is a good **folder structure** for automation projects?
Separate by domain (ec2/, backups/, monitoring/, common/) with reusable utilities. ## Footnote A well-organized structure facilitates navigation and collaboration.
71
Why should you NEVER **hardcode AWS credentials**?
Credentials leak easily in scripts, repos, logs. ## Footnote Protecting credentials is vital for security.
72
What is the safest way to give a Python script **AWS permissions**?
IAM Roles (EC2 roles, Lambda roles, IRSA for K8s). ## Footnote Using IAM roles enhances security by avoiding credential exposure.
73
Why must scripts avoid **printing secrets** in logs?
Logs are often stored indefinitely and shared. ## Footnote This practice helps prevent accidental leaks of sensitive information.
74
What automation tasks help with **EC2 cost optimization**?
* Auto-stopping idle instances * Removing unused resources * Cleaning snapshots ## Footnote These tasks can significantly reduce AWS costs.
75
What kind of automation benefits **EBS volumes**?
* Snapshot creation * Retention policies * Automated restoration ## Footnote These practices enhance data management and recovery.
76
How can Python automate **EKS/Ops tasks**?
* Retrieving pod logs * Validating deployments * Checking node health * Scaling deployments ## Footnote Automating these tasks improves operational efficiency.
77
What IAM tasks can Python automate?
* Detect unused access keys * Rotate keys * Audit roles & permissions ## Footnote These tasks enhance security and compliance.
78
What should you do if a Python script creates a **half-initialized resource**?
Roll back and clean up before retrying. ## Footnote This approach prevents resource conflicts and maintains system integrity.
79
What might cause **EBS snapshot restore failures**?
* Corrupted snapshot * Wrong AZ * Wrong volume type ## Footnote Identifying these issues is crucial for successful restorations.
80
Why must automation scripts trigger **alerts on failure**?
Silent failures can lead to data loss or outages. ## Footnote Alerts ensure prompt responses to critical issues.
81
When is **Terraform** better than Python?
For provisioning and managing infrastructure state. ## Footnote Terraform excels in infrastructure as code scenarios.
82
When is **Python** better than Terraform?
For operational logic: monitoring, backups, cleanup, reporting, and workflows. ## Footnote Python provides flexibility for complex automation tasks.
83
Why must you combine **Terraform and Python** in DevOps?
Terraform provisions infrastructure; Python automates operations on top of it. ## Footnote This combination leverages the strengths of both tools.