I have a use-case where my bash script needs to wait until AWS CloudFormation completes Creating or Updating the stacks.
I found the following commands can be used to do so:
aws cloudformation wait stack-create-complete --stack-name STACK_NAME
aws cloudformation wait stack-update-complete --stack-name STACK_NAME
Following is a snippet of the script -
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_NAME
But I am not able to do so and I get the following error:
{
"StackId": "arn:aws:cloudformation:ap-southeast-1:someAwsAcId:stack/someStackName/xxxx-xxx-xx-xxx-xxxxx"
}
Waiter StackCreateComplete failed: Waiter encountered a terminal failure state
And instead of waiting, the script goes to the next line, causing things to break.
Wait for a new stack creation -- aws cloudformation wait stack-create-complete
Wait for an existing stack update -- 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
}
Function call:
wait_stack_create ${TEST_SERVICE_STACK}
The wait command expect the stack arn
add to the create stack command jq -r '.StackId'
Something like:
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')
And then you can do
aws cloudformation wait stack-create-complete --stack-name "${STACKID}"
The error message indicates that the stack reached a terminal failure state. As with anything, if what you're doing fails, you shouldn't continue.
You can get the status of your stack after reaching a terminal state:
aws cloudformation describe-stacks --stack-name STACK_NAME --query 'Stacks[].StackStatus' --output text
If the status isn't CREATE_COMPLETE or UPDATE_COMPLETE you should print an error message and exit your script.