Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

explicit-logic/python-module-14.1

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.):

import boto3

ec2_client = boto3.client('ec2', region_name="eu-west-3")
reservations = ec2_client.describe_instances()

for reservation in reservations['Reservations']:
  instances = reservation['Instances']
  for instance in instances:
    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:

import boto3

ec2_client = boto3.client('ec2', region_name="eu-west-3")

statuses = ec2_client.describe_instance_status()
for status in statuses['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:

import boto3
import schedule
import time

ec2_client = boto3.client('ec2', region_name="eu-west-3")

def check_instance_status():
  statuses = ec2_client.describe_instance_status(
    IncludeAllInstances=True
  )
  for status in statuses['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)

while True:
  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

Watchers

Forks

Contributors

Morty Proxy This is a proxified and sanitized view of the page, visit original site.