Tengo un caso de uso en el que mi script bash debe esperar hasta que AWS CloudFormation complete la creación o actualización de las pilas.
Encontré que los siguientes comandos se pueden usar para hacerlo:
aws cloudformation wait stack-create-complete --stack-name STACK_NAME aws cloudformation wait stack-update-complete --stack-name STACK_NAMEA continuación se muestra un fragmento del guión:
echo "Creating stack ..." aws cloudformation create-stack --stack-name $STACK_NAME \ --parameters ParameterKey=Environment,ParameterValue=Development \ --template-body file://someCfScript.yaml \ --capabilities CAPABILITY_AUTO_EXPAND --profile someProfileName aws cloudformation wait stack-create-complete --stack-name $STACK_NAMEPero no puedo hacerlo y me sale el siguiente error:
{ "StackId": "arn:aws:cloudformation:ap-southeast-1:someAwsAcId:stack/someStackName/xxxx-xxx-xx-xxx-xxxxx" } Waiter StackCreateComplete failed: Waiter encountered a terminal failure stateY en lugar de esperar, el guión pasa a la siguiente línea, lo que hace que las cosas se quiebren.
Espere a que se cree una nueva pila: aws cloudformation wait stack-create-complete
Espere una actualización de pila existente: aws cloudformation wait stack-update-complete
wait_stack_create() { STACK_NAME=$1 echo "Waiting for [$STACK_NAME] stack creation." aws cloudformation wait stack-create-complete \ --region ${REGION} \ --stack-name ${STACK_NAME} status=$? if [[ ${status} -ne 0 ]] ; then # Waiter encountered a failure state. echo "Stack [${STACK_NAME}] creation failed. AWS error code is ${status}." exit ${status} fi }Llamada de función:
wait_stack_create ${TEST_SERVICE_STACK}El comando de espera espera la pila arn
agregue al comando de creación de pila jq -r '.StackId'
Algo como:
ID=$(aws cloudformation create-stack --stack-name $STACK_NAME \ --parameters ParameterKey=Environment,ParameterValue=Development \ --template-body file://someCfScript.yaml \ --capabilities CAPABILITY_AUTO_EXPAND --profile someProfileName| jq -r '.StackId')Y luego puedes hacer
aws cloudformation wait stack-create-complete --stack-name "${STACKID}"El mensaje de error indica que la pila alcanzó un estado de falla de terminal. Como con cualquier cosa, si lo que estás haciendo falla, no deberías continuar.
Puede obtener el estado de su pila después de llegar a un estado terminal:
aws cloudformation describe-stacks --stack-name STACK_NAME --query 'Stacks[].StackStatus' --output text Si el estado no es CREATE_COMPLETE o UPDATE_COMPLETE , debe mostrar un mensaje de error y salir de la secuencia de comandos.