From 7e35161c5bf13e894865ce765808a798c69ca3e9 Mon Sep 17 00:00:00 2001 From: dzikafoczka Date: Fri, 20 Dec 2024 21:36:52 +0100 Subject: [PATCH] lb2 --- lb2.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lb2.py b/lb2.py index d7450c5..c58ac06 100644 --- a/lb2.py +++ b/lb2.py @@ -2,6 +2,7 @@ import boto3 import base64 from USER_DATA import user_data import json +import time # Configuration variables PREFIX = "s464863" @@ -246,6 +247,22 @@ def create_ec2_instances(ec2_client, launch_template_id, subnet_id): return instance_ids +def wait_for_instances_to_run(ec2_client, instance_ids): + print("Waiting for EC2 instances to be in 'running' state...") + + while True: + # Get state of instances + response = ec2_client.describe_instances(InstanceIds=instance_ids) + states = [reservation['Instances'][0]['State']['Name'] for reservation in response['Reservations']] + + # Check if all instances are running + if all(state == 'running' for state in states): + print("All EC2 instances are now 'running'.") + break + else: + print("Waiting for EC2 instances to reach 'running' state...") + time.sleep(10) + # Main function def main(): # EC2 client