From 8ca0b8a54278503d5502bfc5dc1ffcf783646b48 Mon Sep 17 00:00:00 2001 From: hiroTochigi Date: Wed, 31 Jul 2024 22:41:48 -0500 Subject: [PATCH] Enhance up function to handle terminated EC2 instances - Added check for terminated EC2 instance state - Execute instance creation steps when instance state is terminated - Ensure consistent behavior for creating and running EC2 instances when necessary --- src/aws/up.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/aws/up.sh b/src/aws/up.sh index 21994cc1..c22bfb39 100644 --- a/src/aws/up.sh +++ b/src/aws/up.sh @@ -188,7 +188,13 @@ function up { instanceId=$(checkInstance) if [ -z "$instanceId" ]; then - instanceId=$(createEc2 | getValueByKeyword InstanceId ) + instanceState="" + else + instanceState=$(checkInstanceState $instanceId) + fi + + if [ -z "$instanceId" ] || [ "$instanceState" = "terminated" ]; then + instanceId=$(createEc2 | getValueByKeyword InstanceId) echo "Creating and running EC2 instance..." echo "Instance id is $instanceId" @@ -206,7 +212,6 @@ function up { storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $importedKeyName $instanceId $publicIp $groupNameaws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses else - instanceState=$(checkInstanceState $instanceId) if [ "$instanceState" = "running" ]; then echo "EC2 instance is already running." elif [ "$instanceState" = "stopped" ]; then