How to Create Wait Conditions in a CloudFormation Template

Wait Conditions, as the name suggests, is a tool used to control the order of creation of the AWS resources in a CloudFormation stack. It can pause the creation of a stack and wait for a signal to ensure that specific resources and configurations were properly launched before resuming the stack creation process.

Use cases:

  • For example, you want to make sure that a specific domain name is working before you can conclude that your application is indeed up and running. Or perhaps, you want to make sure that a particular stack resource can access an external API endpoint (or something external to your stack) before giving a “green light” to the CloudFormation stack to complete its creation.
  • To track the status of a required configuration process in your CloudFormation stack.

This article will show you how to create a wait condition on a CloudFormation template. We’ll see how the stack reacts if we don’t fire a signal to the wait condition. We will also see how the stack behaves if we pass a success value to signal the wait condition.

STEP 1: Create a CloudFormation Template. This template will create an Amazon S3 bucket and an EC2 instance. The last two resources are called WaitConditionHandle and WaitCondition.

The “TdojoHandle” WaitConditionHandle resource does not take any parameters. 

Let’s examine the “TdojoWaitCondition” which is the WaitCondition resource. The DependsOn attribute specifies a certain resource that should be successfully created first before the stack can move forward to create the WaitCondition resource. In our case, the stack will create the EC2 instance before creating the “TdojoWaitCondition” WaitCondition resource.

IT Certification Category (English)728x90

The Properties attribute includes a ”Handle” parameter that defines which handle resource our WaitCondition will be using. The Ref intrinsic function is used to specify “TdojoHandle” as the WaitConditionHandle resource. You can optionally set an additional “Count” parameter, which is the number of success signals that CloudFormation must receive before continuing the stack creation process. It is set to 1 by default if it is not defined. We will set the “Timeout” parameter to 300 seconds. This means that the stack will wait for 300 seconds for the number of signals that the Count property specifies. 

how_to_create_wait_conditions_in_a_cloudformation_template1

STEP 2: Using the AWS Console, create stack by uploading the CloudFormation template.

how_to_create_wait_conditions_in_a_cloudformation_template2
STEP 3: Click “Upload a template file” then choose the template file that you have created on your computer. Proceed by clicking “Next”.

how_to_create_wait_conditions_in_a_cloudformation_template3

STEP 4: Enter your stack name then click “Next”. Just click next on the subsequent windows then create the stack. We will leave everything on their default values. 

 how_to_create_wait_conditions_in_a_cloudformation_template4.jpg

Now that we have initiated the creation of the stack, let us take a look on the stack’s creation progress. You will notice all resources but the “TdojoWaitCondition” has been created successfully. The “TdojoWaitCondition” will remain in CREATE_IN_PROGRESS status until a signal with a SUCCESS status value is sent to the WaitConditionHandle.

how_to_create_wait_conditions_in_a_cloudformation_template5
Let us wait until the WaitCondition’s Timeout expires. Remember that we have set this to 300 seconds or 5 minutes. After the WaitCondition time out, the stack will proceed by deleting the S3 bucket, EC2 instance, and the handle that was initially created. Then, the stack will have ROLLBACK_COMPLETE as its status.

how_to_create_wait_conditions_in_a_cloudformation_template6

Now that we have seen how the stack reacts if the WaitCondition fails to receive a signal, let us see how it will react once a successful signal is sent to WaitCondition by sending an HTTP PUT request to the presigned URL created by the WaitConditionHandle resource.

STEP 1: Delete the previous stack and re-create the process of uploading a template to Cloudformation’s Console. But this time, copy the presigned URL created  by the WaitConditionHandle resource and save it somewhere.

how_to_create_wait_conditions_in_a_cloudformation_template7
STEP 2: Send the JSON structure to the presigned URL using the CURL command line.

curl -X PUT -H ‘Content-Type:’ –data-binary /
‘{“Status” : “SUCCESS”,
“Reason” : “Configuration Complete”,
“UniqueId” : “ID1234”,
“Data” : “Application has completed configuration.”}’ ‘<your-presigned-url>’

The Status can either have a SUCCESS or FAILURE value. A SUCCESS status indicates a success signal while the FAILURE status indicates a failure signal and triggers a failed wait condition and a stack rollback.

Reason can be any string value.

The UniqueId is the identifier to the signal to Cloudformation. Signals must contain a unique value for a particular condition, otherwise, CloudFormation will consider the signal a retransmission of the previously sent signal with the same UniqueId, and it will ignore the signal. 

Data is any information that you want to send back with the signal.

Note that I’m using an environment variable to store the presigned URL value so it won’t be messy. But you can also paste it directly. Don’t forget to wrap it within single quotes. 

how_to_create_wait_conditions_in_a_cloudformation_template8

STEP 3: Go back to the CloudFormation console. You will see that the stack has now been created successfully and has a status of CREATE_COMPLETE.

how_to_create_wait_conditions_in_a_cloudformation_template9

Conclusion

I have shown you how to use the wait condition on a CloudFormation template and upload it to the AWS CloudFormation Console. I’ve also shown the different reactions of the stack’s progress when it failed to receive a signal and when receiving a successful signal. Notice that we manually sent the successful signal to the presigned URL. Usually, the sending of signals is done automatically through scripts. For your exercise, you can try automating the sending of signals. Also, find out what will happen if you send a signal with a FAILURE status value.

References:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-waitcondition.html
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-waitcondition.html

Tutorials Dojo Study Guide and Cheatsheet

Pass your AWS, Azure, and Google Cloud Certifications with the Tutorials Dojo Portal

Tutorials Dojo portal

Our Bestselling AWS Certified Solutions Architect Associate Practice Exams

AWS Certified Solutions Architect Associate Practice Exams

Enroll Now – Our AWS Practice Exams with 95% Passing Rate

AWS Practice Exams Tutorials Dojo

Enroll Now – Our Azure Certification Exam Reviewers

azure reviewers tutorials dojo

Enroll Now – Our Google Cloud Certification Exam Reviewers

Tutorials Dojo Exam Study Guide eBooks

Tutorials Dojo Study Guide and Cheat Sheets-2

Subscribe to our YouTube Channel

Tutorials Dojo YouTube Channel

FREE Intro to Cloud Computing for Beginners

FREE AWS, Azure, GCP Practice Test Samplers

Browse Other Courses

Generic Category (English)300x250

Recent Posts

AWS, Azure, and GCP Certifications are consistently among the top-paying IT certifications in the world, considering that most companies have now shifted to the cloud. Earn over $150,000 per year with an AWS, Azure, or GCP certification!

Follow us on LinkedIn, YouTube, Facebook, or join our Slack study group. More importantly, answer as many practice exams as you can to help increase your chances of passing your certification exams on your first try!

View Our AWS, Azure, and GCP Exam Reviewers

Our Community

~98%
passing rate
Around 95-98% of our students pass the AWS Certification exams after training with our courses.
200k+
students
Over 200k enrollees choose Tutorials Dojo in preparing for their AWS Certification exams.
~4.8
ratings
Our courses are highly rated by our enrollees from all over the world.

What our students say about us?

error: Content is protected !!