diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..8b137891791fe96927ad78e64b0aad7bded08bdc --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ + diff --git a/Part2/config.ini b/Part2/config.ini index adf2621a7e61127e6e14b54602cef4783d5cae92..235a33263ceb6809cb68a8b4e9fe0f00aea6d3d9 100644 --- a/Part2/config.ini +++ b/Part2/config.ini @@ -1,10 +1,10 @@ [aws] -aws_access_key_id = AKIAVEKYIBTQFZV3M3OP -aws_secret_access_key = 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 -region = us-east-1 +aws_access_key_id = +aws_secret_access_key = +region = [opensearch] -endpoint = v9b2tkt2ccr61qs9tw7e.us-east-1.aoss.amazonaws.com -index_name = cloud +endpoint = +index_name = diff --git a/Part2/create_instance.py b/Part2/create_instance.py index ac6221e6b65d4c01caafb36558a6ff58902ec441..11746a4bbbd984cb36ae6525246800bb5b5ea2a0 100644 --- a/Part2/create_instance.py +++ b/Part2/create_instance.py @@ -2,45 +2,53 @@ import boto3 import base64 +import argparse # Function to read the content of config.ini def get_config_content(filepath): with open(filepath, 'r') as file: return file.read() -# Load the config content -config_content = get_config_content('config.ini') - - -ec2 = boto3.resource('ec2') - - - -# User code that's executed when the instance starts -script = f"""#!/bin/bash -cat <<EOT > /home/ubuntu/chatbot-lab/Part2/config.ini -{config_content} -EOT -source /home/ubuntu/chatbotlab/bin/activate -## Run the apllication -cd /home/ubuntu/chatbot-lab/Part2 -streamlit run main.py -""" - -encoded_script = base64.b64encode(script.encode()).decode('utf-8') - -# Create a new EC2 instance -instance = ec2.create_instances( - ImageId='ami-05747e7a13dac9d14', - MinCount=1, - MaxCount=1, - InstanceType='t2.micro', - KeyName='group-14-key-pair', - SecurityGroupIds=['sg-06f3ca7153db92958'], - UserData=encoded_script -) -print("Instance created with ID:", instance[0].id) - - - +def create_instance(ami_id, key_pair_name, security_group_id): + + # Load the config content + config_content = get_config_content('config.ini') + ec2 = boto3.resource('ec2') + # User code that's executed when the instance starts + script = f"""#!/bin/bash + cat <<EOT > /home/ubuntu/chatbot-lab/Part2/config.ini + {config_content} + EOT + source /home/ubuntu/chatbotlab/bin/activate + ## Run the application + cd /home/ubuntu/chatbot-lab/Part2 + streamlit run chatbot.py + """ + encoded_script = base64.b64encode(script.encode()).decode('utf-8') + # Create a new EC2 instance + instance = ec2.create_instances( + ImageId=ami_id, + MinCount=1, + MaxCount=1, + InstanceType='t2.micro', + KeyName=key_pair_name, + SecurityGroupIds=[security_group_id], + UserData=encoded_script + ) + return instance + + +def main(ami_id, key_pair_name, security_group_id): + + print("Creating an EC2 instance") + instance = create_instance(ami_id, key_pair_name, security_group_id) + print("Instance created with ID:", instance[0].id) + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description="Create an EC2 instance to run the chatbot") + parser.add_argument("--ami_id", help="The ID of the AMI to use for the instance") + parser.add_argument("--key_pair_name", help="The name of the key pair to use for the instance") + parser.add_argument("--security_group_id", help="The ID of the security group to use for the instance") + args = parser.parse_args() + main(args.ami_id, args.key_pair_name, args.security_group_id) \ No newline at end of file