Skip to content

Repository files navigation

Module 14 - Automation with Python

This repository contains a demo project created as part of my DevOps studies in the TechWorld with Nana – DevOps Bootcamp.

Demo Project: Health Check: EC2 Status Checks

Technologies used: Python, Boto3, AWS, Terraform

Project Description:

  • Create EC2 Instances with Terraform
  • Write a Python script that fetches statuses of EC2 Instances and prints to the console
  • Extend the Python script to continuously check the status of EC2 Instances in a specific interval

Prerequisites

Install Python dependencies with uv:

uv sync

Configure AWS credentials at ~/.aws/credentials:

[default]aws_access_key_id = AKIA...
aws_secret_access_key = ...

And the default region at ~/.aws/config:

[default]region = us-east-1

Create a Terraform variables file from the provided example:

cp terraform.tfvars.example terraform.tfvars

Create EC2 Instances with Terraform

  1. Initialize Terraform to download the required providers:

    terraform init
  2. Review the execution plan and apply it:

    terraform plan
    terraform apply --auto-approve

Fetch EC2 Instance Statuses with Python

Option 1 — basic instance state

Use describe_instances to list every instance along with its current state (running, stopped, terminated, etc.):

importboto3ec2_client=boto3.client('ec2', region_name="eu-west-3")
reservations=ec2_client.describe_instances()
forreservationinreservations['Reservations']:
instances=reservation['Instances']
forinstanceininstances:
print(f"Instance {instance['InstanceId']} is {instance['State']['Name']}")

Run the script: main.py

To verify that the script reflects infrastructure changes, remove the third instance from main.tf and re-apply:

terraform apply --auto-approve

Then run the script again: main.py

Option 2 — extended status checks

describe_instance_status returns the lifecycle state together with the EC2 system and instance reachability checks, giving a more complete view of instance health:

importboto3ec2_client=boto3.client('ec2', region_name="eu-west-3")
statuses=ec2_client.describe_instance_status()
forstatusinstatuses['InstanceStatuses']:
ins_status=status['InstanceStatus']['Status']
sys_status=status['SystemStatus']['Status']
state=status['InstanceState']['Name']
print(f"Instance {status['InstanceId']} is {state} with instance status is {ins_status} and system status {sys_status}")

Continuously Monitor EC2 Status on a Schedule

Make sure the schedule module is installed:

uv sync

The script below polls every 5 seconds and prints the state and both reachability checks for each instance. Passing IncludeAllInstances=True ensures stopped instances are reported as well:

importboto3importscheduleimporttimeec2_client=boto3.client('ec2', region_name="eu-west-3")
defcheck_instance_status():
statuses=ec2_client.describe_instance_status(
IncludeAllInstances=True
)
forstatusinstatuses['InstanceStatuses']:
ins_status=status['InstanceStatus']['Status']
sys_status=status['SystemStatus']['Status']
state=status['InstanceState']['Name']
print(f"Instance {status['InstanceId']} is {state} with instance status is {ins_status} and system status {sys_status}")
print("###############################\n")
schedule.every(5).seconds.do(check_instance_status)
whileTrue:
schedule.run_pending()
time.sleep(1)

Once you are done, tear down the infrastructure to avoid unnecessary AWS charges:

terraform destroy

About

Health Check: EC2 Status Checks

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages