From 73b1151d1cbb54df4654ad34567cfae6d6661e1c Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Sat, 10 Feb 2024 11:00:19 -0700 Subject: [PATCH] modified as userdata script, finishing up part-1 --- .gitignore | 1 + Project-1/Part-1/README.md | 5 +++++ Project-1/Part-1/instance_run | 7 ++++++- Project-1/Part-1/run_instances.py | 6 +++++- 4 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 Project-1/Part-1/README.md diff --git a/.gitignore b/.gitignore index fe54a0a..ff6b12a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules/ *.log *.ps1 +*.zip diff --git a/Project-1/Part-1/README.md b/Project-1/Part-1/README.md new file mode 100644 index 0000000..92fc5c8 --- /dev/null +++ b/Project-1/Part-1/README.md @@ -0,0 +1,5 @@ +# Part-1: Starting out - EC2 instance, EIP, web tier (lookup) + +- `instance_info.py` describes the instances on the *us-east-1* region +- `run_instances.py` launches one new instance, and associates the EIP 34.232.196.35 with it +- The `instance_run` script is passed as user-data to the launched instance, to be executed upon first boot diff --git a/Project-1/Part-1/instance_run b/Project-1/Part-1/instance_run index e15e3e4..b800094 100644 --- a/Project-1/Part-1/instance_run +++ b/Project-1/Part-1/instance_run @@ -1,6 +1,9 @@ #!/bin/bash +# https://stackoverflow.com/a/71648116/7630441 +sudo -u ubuntu -i <<'EOF' + # initializing instance -cd /home/ubuntu +cd ~ sudo apt update sudo apt upgrade -y @@ -21,3 +24,5 @@ npm i # Start pm2 start npm --name webTier -- run prod pm2 save + +EOF diff --git a/Project-1/Part-1/run_instances.py b/Project-1/Part-1/run_instances.py index ae329fd..c01e9c0 100644 --- a/Project-1/Part-1/run_instances.py +++ b/Project-1/Part-1/run_instances.py @@ -4,17 +4,21 @@ import boto3 session = boto3.Session(profile_name="dev") dev_ec2_client = session.client("ec2", region_name="us-east-1") +with open("instance_run", "r") as setup_f: + user_data = setup_f.read() + # Create capacity reservation of instances ami_id = "ami-0c7217cdde317cfec" # Ubuntu Server 22.04 LTS, SSD on EBS, 64-bit (x86) reservation = dev_ec2_client.run_instances( ImageId=ami_id, + InstanceType="t2.micro", KeyName='cse546-dev', MinCount=1, # if available instances are less than min_count, abort with no allocation MaxCount=1, # try to allocate max_count instances. we only need one for now - InstanceType="t2.micro", TagSpecifications=[ {"ResourceType": "instance", "Tags": [{"Key": "Name", "Value": "web-instance"}]} ], + UserData=user_data ) print("Instances allocated successfully:", "Yes" if reservation else "No")