Skip to content
Snippets Groups Projects
Commit 8cdea5bc authored by abir.chebbi's avatar abir.chebbi
Browse files

modify config.ini, create_instance.py

parent 3942f089
No related branches found
No related tags found
No related merge requests found
[aws] [aws]
aws_access_key_id = AKIAVEKYIBTQFZV3M3OP aws_access_key_id =
aws_secret_access_key = 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 aws_secret_access_key =
region = us-east-1 region =
[opensearch] [opensearch]
endpoint = v9b2tkt2ccr61qs9tw7e.us-east-1.aoss.amazonaws.com endpoint =
index_name = cloud index_name =
...@@ -2,45 +2,53 @@ ...@@ -2,45 +2,53 @@
import boto3 import boto3
import base64 import base64
import argparse
# Function to read the content of config.ini # Function to read the content of config.ini
def get_config_content(filepath): def get_config_content(filepath):
with open(filepath, 'r') as file: with open(filepath, 'r') as file:
return file.read() 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment