Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output the stack create successful info until all the resources are created in the stack

I am trying to create a stack with aws cloudformation create-stack --stack-name ... --template-body file://... to create a stack. It output the stack id as soon as I execute the command. But the resources which are required by the stack are stilling in creating.

I want to output some message until the all the resources are created.

I don't want to describe the stack in a loop. and output the message until got the stack create completed signal.

like image 475
Angle Tom Avatar asked Dec 15 '15 08:12

Angle Tom


People also ask

How do I create a stack with existing resources?

Create a stack from existing resources using the AWS Management Console. Sign in to the AWS Management Console and open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation . On the Stacks page, choose Create stack, and then choose With existing resources (import resources).

What happens when one of the resources in a stack Cannot be created successfully?

Q: What happens when one of the resources in a stack cannot be created successfully? By default, the “automatic rollback on error” feature is enabled. This will direct CloudFormation to only create or update all resources in your stack if all individual operations succeed.

What should you create before creating a stack in AWS?

Before you create a stack, you must have a template that describes what resources AWS CloudFormation will include in your stack. For more information, see Working with AWS CloudFormation templates. To preview the configuration of a new stack, you can use a change set.

How do you reference output from another CloudFormation stack?

To create a cross-stack reference, use the export field to flag the value of a resource output for export. Then, use the Fn::ImportValue intrinsic function to import the value in any stack within the same AWS Region and account. AWS CloudFormation identifies exported values by the names specified in the template.


2 Answers

After the initial create-stack request you will need to request another one:

 aws cloudformation wait stack-create-complete --stack-name $STACK_ID_FROM_CREATE_STACK

From the aws docs http://docs.aws.amazon.com/cli/latest/reference/cloudformation/wait/stack-create-complete.html

Wait until stack status is CREATE_COMPLETE. It will poll every 30 seconds until a successful state has been reached. This will exit with a return code of 255 after 120 failed checks.

like image 58
WooDzu Avatar answered Oct 02 '22 19:10

WooDzu


I also needed a wait in my bash script after running aws cloudformation create-stack. I was hesitant to use the aws cloudformation wait stack-create-complete command as it only polls up to 60 minutes (120 times 30 seconds). Also, I did not want to run tests to see how it would behave if the stack ended up in a state other than "CREATE_COMPLETE". I therefore wrote my own wait in bash as follows:

aws --region ${AWS_REGION} --profile ${AWS_PROFILE} cloudformation create-stack --template-body ${STACK_TEMPLATE} --stack-name ${STACK_NAME}
if [[ $? -eq 0 ]]; then
    # Wait for create-stack to finish
    echo  "Waiting for create-stack command to complete"
    CREATE_STACK_STATUS=$(aws --region ${AWS_REGION} --profile ${AWS_PROFILE} cloudformation describe-stacks --stack-name ${STACK_NAME} --query 'Stacks[0].StackStatus' --output text)
    while [[ $CREATE_STACK_STATUS == "REVIEW_IN_PROGRESS" ]] || [[ $CREATE_STACK_STATUS == "CREATE_IN_PROGRESS" ]]
    do
        # Wait 30 seconds and then check stack status again
        sleep 30
        CREATE_STACK_STATUS=$(aws --region ${AWS_REGION} --profile ${AWS_PROFILE} cloudformation describe-stacks --stack-name ${STACK_NAME} --query 'Stacks[0].StackStatus' --output text)
    done
fi
like image 28
user1638152 Avatar answered Oct 02 '22 18:10

user1638152