Sto utilizzando uno script bash per creare un'istanza AWS tramite CLI e un modello cloudformation. Voglio che il mio script attenda fino al completamento della creazione dell'istanza prima di passare al mio script. In questo momento, sto usando un ciclo while per "descrivere-stack" ogni 5 secondi e uscire dal ciclo quando lo stato = "CREATE_COMPLETE" o qualche stato di errore. Qualcuno sa di un modo più elegante per fare questo?Come attendere nello script bash fino al completamento della creazione dell'istanza AWS
stackStatus="CREATE_IN_PROGRESS"
while [[ 1 ]]; do
echo "${AWS_CLI_PATH}" cloudformation describe-stacks --region "${CfnStackRegion}" --stack-name "${CfnStackName}"
response=$("${AWS_CLI_PATH}" cloudformation describe-stacks --region "${CfnStackRegion}" --stack-name "${CfnStackName}" 2>&1)
responseOrig="$response"
response=$(echo "$response" | tr '\n' ' ' | tr -s " " | sed -e 's/^ *//' -e 's/ *$//')
if [[ "$response" != *"StackStatus"* ]]
then
echo "Error occurred creating AWS CloudFormation stack. Error:"
echo " $responseOrig"
exit -1
fi
stackStatus=$(echo $response | sed -e 's/^.*"StackStatus"[ ]*:[ ]*"//' -e 's/".*//')
echo " StackStatus: $stackStatus"
if [[ "$stackStatus" == "ROLLBACK_IN_PROGRESS" ]] || [[ "$stackStatus" == "ROLLBACK_COMPLETE" ]] || [[ "$stackStatus" == "DELETE_IN_PROGRESS" ]] || [[ "$stackStatus" == "DELETE_COMPLETE" ]]; then
echo "Error occurred creating AWS CloudFormation stack and returned status code ROLLBACK_IN_PROGRESS. Details:"
echo "$responseOrig"
exit -1
elif [[ "$stackStatus" == "CREATE_COMPLETE" ]]; then
break
fi
# Sleep for 5 seconds, if stack creation in progress
sleep 5
done
Niente di sbagliato in questo approccio, basta assicurarsi di non sedersi a dormire per sempre - si può decidere di pausa dopo una lunga * * tempo comunque. Ci sono soluzioni più eleganti, ma probabilmente oltre lo scopo di 'bash' – arco444
Mi piacerebbe anche vedere una soluzione più elegante! (principalmente per evitare spamming aws api ...) – Cyberwiz