From 297ae8b2f9ee133f9dd9703c5100d5bc0412b9ec Mon Sep 17 00:00:00 2001 From: DigbijayineeClara07 Date: Thu, 25 Sep 2025 07:20:54 +0530 Subject: [PATCH] added-bash-python --- custom/bash/sleep.sh | 6 ++++ custom/python/list_ec2_instances.py | 46 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100755 custom/bash/sleep.sh create mode 100644 custom/python/list_ec2_instances.py diff --git a/custom/bash/sleep.sh b/custom/bash/sleep.sh new file mode 100755 index 0000000..b8eab01 --- /dev/null +++ b/custom/bash/sleep.sh @@ -0,0 +1,6 @@ +#!/bin/bash +# Sleep script for StackGuardian templates + +echo "Sleeping for 5 minutes..." +sleep 300 +echo "Done sleeping!" \ No newline at end of file diff --git a/custom/python/list_ec2_instances.py b/custom/python/list_ec2_instances.py new file mode 100644 index 0000000..27eb0a9 --- /dev/null +++ b/custom/python/list_ec2_instances.py @@ -0,0 +1,46 @@ +from logging import error +import os +import time +import boto3 + + +def check_aws_variables(): + # Check if AWS_DEFAULT_REGION is set in the environment variables + if "AWS_DEFAULT_REGION" not in os.environ: + # If not, show error and exit with 1 + error("AWS_DEFAULT_REGION is not set in the environment variables.") + exit(1) + + +def list_ec2_instances(): + # Create an EC2 client + ec2 = boto3.client("ec2") + + # Fetch instance details + response = ec2.describe_instances() + + # Loop over instance details and format output + for reservation in response.get("Reservations", []): + for instance in reservation.get("Instances", []): + print(f"Instance ID: {instance.get('InstanceId')}") + print(f"Instance Type: {instance.get('InstanceType')}") + print(f"State: {instance.get('State', {}).get('Name')}") + + if "Tags" in instance: + print("Tags:") + for tag in instance["Tags"]: + print(f" > {tag.get('Key')}: {tag.get('Value')}") + + print("---") + # Sleep for 5 minutes (300 seconds) after each instance + print("Sleeping for 5 minutes...") + time.sleep(300) + + +def main(): + check_aws_variables() + list_ec2_instances() + + +if __name__ == "__main__": + main() \ No newline at end of file