All about Cloud, mostly about Amazon Web Services (AWS)

CloudFormation WaitCondition Resources

 2017-07-22 /  611 words /  3 minutes

When using AWS CloudFormation, sometimes resources need time to initialize. For example, an Amazon Elastic Compute Cloud (EC2) instance using UserData to install multiple software packages could take several minutes. WaitCondition resources in conjunction with WaitHandle resources and the cfn-signal script signal completion, but it can be complex to setup. This post explains how to do it!

NOTE: AWS recommends using CloudFormation CreationPolicy attributes whenever possible. CreationPolicy attributes are simpler, but only work with Amazon EC2 and Auto Scaling resource types. See “” for more details.

WaitCondition resources also work with non-AWS resources and support the need for multiple resources.

WaitHandle The WaitHandle resource is very simple but a little obscure. It has no properties. A WaitHandle actually creates an HTTP accessible endpoint linked to the WaitCondition. This HTTP endpoint is called by the cfn-signal script and the WaitCondition resource is evaluated.

An example WaitHandle would be:

WaitHandleName: Type: “AWS::CloudFormation::WaitConditionHandle”

1 2 3 WaitHandleName: Type: “AWS::CloudFormation::WaitConditionHandle”

In this example, the handle is called “WaitHandleName“.

WaitCondition The WaitCondition resource ties everything together. It uses DependsOn to identify the CloudFormation resource that is the target of the wait condition.

It supports three properties:

The Count property defines the number of signals needed to satisfy the condition. The Handle property refers to the associated WaitHandle resource. The Timeout property defines the maximum duration to wait for the signal before the WaitCondition fails. An example declaration would be:

WaitConditionName: Type: “AWS::CloudFormation::WaitCondition” DependsOn: “AppServerGroup” Properties: Handle: Ref: “WaitHandleName” Timeout: “300” Count: 5 1 2 3 4 5 6 7 8 9 WaitConditionName: Type: “AWS::CloudFormation::WaitCondition” DependsOn: “AppServerGroup” Properties: Handle: Ref: “WaitHandleName” Timeout: “300” Count: 5 In this example, the condition is called “WaitConditionName” and is associated with the “AppServerGroup” resource, meaning that the “AppServerGroup” will not be complete until the condition is met, or a timeout occurs. The condition is associated with the handle called “WaitHandleName“. The timeout is 300 seconds and there needs to be 5 signals sent before the condition is met.

cfn-signal The final part of the puzzle is the cfn-signal script. cfn-signal is one of the CloudFormation Helper Scripts. It applies to WaitCondition resources and CreationPolicy attributes so the full syntax is a little complex. The AWS documentation does include specific syntax for use with WaitCondition resources:

cfn-signal –success|-s signal.to.send –reason|-r resource.status.reason –data|-d data –id|-i unique.id –exit-code|-e exit.code waitconditionhandle.url 1 2 3 4 5 6 7 cfn-signal –success|-s signal.to.send –reason|-r resource.status.reason –data|-d data –id|-i unique.id –exit-code|-e exit.code waitconditionhandle.url Many of these options are irrelevant, or infrequently needed:

If the –exit-code (-e) option is used then the –success (-s) option is ignored . The –reason (-r) option defaults to “Configuration Failed”. It is only used in the event of a failure. The –id (-i) option ensures that multiple signals do not apply to the same resource. The id can be any alphanumeric string, and defaults to the ID of the Amazon EC2 instance or the machine’s Fully Qualified Domain Name (FQDN). This is adequate for most use cases. The –data (-d) option returns arbitrary data. As a result, the simplest possible call becomes:

cfn-signal -e

Within a UserData element the following syntax should be used:

UserData: !Base64
    'Fn::Join':
    - ''
    - - |
    #!/bin/bash
    - '/opt/aws/bin/cfn-init -s '
    - !Ref 'AWS::StackName'
    - ' -r MyInstance '
    - ' --region '
    - !Ref 'AWS::Region'
    - |+
    - '/opt/aws/bin/cfn-signal -e $? '
    - !Ref 'WaitHandleName'

1 2 3 4 5 6 7 8 9 10 11 12 13 UserData: !Base64 ‘Fn::Join’: - ’’ - - | #!/bin/bash - ‘/opt/aws/bin/cfn-init -s ' - !Ref ‘AWS::StackName’ - ’ -r MyInstance ' - ’ –region ' - !Ref ‘AWS::Region’ - |+ - ‘/opt/aws/bin/cfn-signal -e $? ' - !Ref ‘WaitHandleName’


Tags:  AWS  AWS CloudFormation  Amazon Elastic Compute Cloud (EC2)  CreationPolicy  WaitCondition  WaitHandle  cfn-signal
Categories:  AWS  AWS CloudFormation

See Also

 Top Ten Tags

AWS (43)   Kinesis (9)   Streams (8)   AWS Console (5)   Go (5)   Analytics (4)   Data (4)   database (4)   Amazon DynamoDB (3)   Amazon Elastic Compute Cloud (EC2) (3)  


All Tags (173)

Disclaimer

All data and information provided on this site is for informational purposes only. cloudninja.cloud makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis.

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. My opinions may change over time.