Interview Prep/Python for DevOps/AWS automation with boto3

You are asked to write a script that stops all dev EC2 instances at 8 PM and starts them at 8 AM. How do you build it?

BeginnerScenarioAWS automation with boto3
Answer

Situation read → Classic cost-saving automation; the risk is stopping the wrong instances.

What I'd do →

  1. Select only instances tagged Environment=dev and Schedule=office-hours (opt-in, never "everything not prod").
  2. Use a paginator with Filters for the tags and the current state.
  3. Call stop_instances/start_instances in batches, log IDs, support --dry-run (and DryRun=True to verify permissions).
  4. Run it as a Lambda on EventBridge Scheduler (cron with time zone), with a role allowed only ec2:StopInstances/StartInstances on resources with that tag (IAM condition aws:ResourceTag/Schedule).

Why → Opt-in tags plus IAM conditions make it impossible to stop prod even if the code is wrong.

Interview tip → Mention the IAM tag condition; it is defence in depth beyond the code filter.

All Python for DevOps questions Roadmap: Python for automation

More on AWS automation with boto3