Skip to content

Commit

Permalink
Enhance main.sh to check existing AWS resources before creation
Browse files Browse the repository at this point in the history
- Added checks for existing SSH key on AWS and create if not present
- Implemented verification for Security Group existence and creation if missing
- Included logic to verify EC2 instance existence and state
  - Create and run instance if it does not exist
  - Log state if the instance is running
  - Start the instance if it is stopped
  • Loading branch information
hiroTochigi committed Aug 1, 2024
1 parent fdf58db commit 7b31854
Showing 1 changed file with 65 additions and 42 deletions.
107 changes: 65 additions & 42 deletions src/aws/up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ groupName=luftballons-sg
instanceName=luftballon
checkSSH=~/.ssh/$publickey

checkKeyName() {
keyPairs=($(aws ec2 describe-key-pairs --query "KeyPairs[*].KeyName" --output text))

for key in "${keyPairs[@]}"
do
if [ "$key" == "$keyname" ]; then
echo "Error: Key Pair '$key' matches the specified key name '$keyname'. Exiting..."
exit 1
fi
done
checkSshKey() {
aws ec2 describe-key-pairs --key-names $keyname &> /dev/null
return $?
}

checkSecurityGroup() {
aws ec2 describe-security-groups --group-names $groupName &> /dev/null
return $?
}

checkInstance() {
aws ec2 describe-instances --filters "Name=tag:Name,Values=$instanceName" --query "Reservations[*].Instances[*].InstanceId" --output text
}

checkInstanceState() {
ID=$1
aws ec2 describe-instances --instance-ids $ID --query "Reservations[*].Instances[*].State.Name" --output text
}

function importSshKey()
Expand Down Expand Up @@ -158,38 +165,54 @@ function up {
then
keyname=luftballon
fi


checkKeyName

importedKeyName=$(importSshKey | getValueByKeyword KeyName )

if [ -z $importedKeyName ]
then
exit 1
if ! checkSshKey ; then
importedKeyName=$(importSshKey | getValueByKeyword KeyName )
if [ -z $importedKeyName ]
then
exit 1
fi
echo "Success to add ssh key: $importedKeyName"
else
echo "The key pair $keyname already exists. Please use another key name."

if ! checkSecurityGroup; then
createSecurityGroups
echo "Add security group"
# Add rules to Security Group as needed
else
echo "Security Group already exists."
fi

echo "Success to add ssh key: $importedKeyName"

createSecurityGroups
echo "Add security group"

instanceId=$(createEc2 | getValueByKeyword InstanceId )
echo "Create EC2 Instance"
echo "Instance id is $instanceId"


aws ec2 create-tags --resources $instanceId --tags Key=Name,Value=$instanceName
aws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses


publicIp=$(waitForOutput "getLatestIpAddress $instanceId")
echo "Public IP Address is $publicIp"

echo "Will open ssh tunnel soon"
isOpen=$(waitForOutput "ssh-keyscan -H $publicIp | grep ecdsa-sha2-nistp256")
echo "Opened ssh tunnel"

openSSHTunnel $instanceName $publicIp $portConfigArray

storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $importedKeyName $instanceId $publicIp $groupName
}
instanceId=$(checkInstance)
if [ -z "$instanceId" ]; then
instanceId=$(createEc2 | getValueByKeyword InstanceId )
echo "Creating and running EC2 instance..."

echo "Instance id is $instanceId"
aws ec2 create-tags --resources $instanceId --tags Key=Name,Value=$instanceName
aws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses

publicIp=$(waitForOutput "getLatestIpAddress $instanceId")
echo "Public IP Address is $publicIp"

echo "Will open ssh tunnel soon"
isOpen=$(waitForOutput "ssh-keyscan -H $publicIp | grep ecdsa-sha2-nistp256")
echo "Opened ssh tunnel"

openSSHTunnel $instanceName $publicIp $portConfigArray

storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $importedKeyName $instanceId $publicIp $groupNameaws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
else
instanceState=$(check_instance_state $instanceId)
if [ "$instanceState" = "running" ]; then
echo "EC2 instance is already running."
elif [ "$instanceState" = "stopped" ]; then
echo "Starting stopped EC2 instance..."
start $instanceName
else
echo "EC2 instance is in state: $instanceState."
fi
fi
}

0 comments on commit 7b31854

Please sign in to comment.