modified as userdata script, finishing up part-1

This commit is contained in:
Kaushik Narayan R 2024-02-10 11:00:19 -07:00
parent 2d833a1379
commit 73b1151d1c
4 changed files with 17 additions and 2 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
node_modules/ node_modules/
*.log *.log
*.ps1 *.ps1
*.zip

View File

@ -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

View File

@ -1,6 +1,9 @@
#!/bin/bash #!/bin/bash
# https://stackoverflow.com/a/71648116/7630441
sudo -u ubuntu -i <<'EOF'
# initializing instance # initializing instance
cd /home/ubuntu cd ~
sudo apt update sudo apt update
sudo apt upgrade -y sudo apt upgrade -y
@ -21,3 +24,5 @@ npm i
# Start # Start
pm2 start npm --name webTier -- run prod pm2 start npm --name webTier -- run prod
pm2 save pm2 save
EOF

View File

@ -4,17 +4,21 @@ import boto3
session = boto3.Session(profile_name="dev") session = boto3.Session(profile_name="dev")
dev_ec2_client = session.client("ec2", region_name="us-east-1") 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 # Create capacity reservation of instances
ami_id = "ami-0c7217cdde317cfec" # Ubuntu Server 22.04 LTS, SSD on EBS, 64-bit (x86) ami_id = "ami-0c7217cdde317cfec" # Ubuntu Server 22.04 LTS, SSD on EBS, 64-bit (x86)
reservation = dev_ec2_client.run_instances( reservation = dev_ec2_client.run_instances(
ImageId=ami_id, ImageId=ami_id,
InstanceType="t2.micro",
KeyName='cse546-dev', KeyName='cse546-dev',
MinCount=1, # if available instances are less than min_count, abort with no allocation 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 MaxCount=1, # try to allocate max_count instances. we only need one for now
InstanceType="t2.micro",
TagSpecifications=[ TagSpecifications=[
{"ResourceType": "instance", "Tags": [{"Key": "Name", "Value": "web-instance"}]} {"ResourceType": "instance", "Tags": [{"Key": "Name", "Value": "web-instance"}]}
], ],
UserData=user_data
) )
print("Instances allocated successfully:", "Yes" if reservation else "No") print("Instances allocated successfully:", "Yes" if reservation else "No")