Last updated on May 2, 2023
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.
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.
STEP 2: Using the AWS Console, create stack by uploading the CloudFormation template.
STEP 3: Click “Upload a template file” then choose the template file that you have created on your computer. Proceed by clicking “Next”.
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.
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.
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.
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.
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.
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.
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