CloudFormation

Glossary

Check

aws cloudformation list-stacks --output yaml

You can output as table but the table tends to be very wide and therefore difficult to read

aws cloudformation describe-stacks --output yaml

You can output as table but the table tends to be very wide and therefore difficult to read

If you have forgotten the name of your stack, use this command to help find it...

aws cloudformation describe-stacks --output yaml | grep StackName

For further stack information use...

aws cloudformation describe-stacks --stack-name ${mystack}

aws cloudformation describe-stack-resources --stack-name ${mystack} --output table

See Also

Template

Stack Template Components

Stack Template Notes

AllowedPattern

The idea is to validate correct formatting of parameters using regular expressions. Some examples include...

^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$

^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\/([1-2][6-9]|3[0-2]))?$

^[A-Za-z][a-zA-Z0-9@%$_-]{12,}$

^\s*($|#|\w+\s*=|(\?|\*|(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?(?:,(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?)*)\s+(\?|\*|(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?(?:,(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?)*)\s+(\?|\*|(?:[01]?\d|2[0-3])(?:(?:-|\/|\,)(?:[01]?\d|2[0-3]))?(?:,(?:[01]?\d|2[0-3])(?:(?:-|\/|\,)(?:[01]?\d|2[0-3]))?)*)\s+(\?|\*|(?:0?[1-9]|[12]\d|3[01])(?:(?:-|\/|\,)(?:0?[1-9]|[12]\d|3[01]))?(?:,(?:0?[1-9]|[12]\d|3[01])(?:(?:-|\/|\,)(?:0?[1-9]|[12]\d|3[01]))?)*)\s+(\?|\*|(?:[1-9]|1[012])(?:(?:-|\/|\,)(?:[1-9]|1[012]))?(?:L|W)?(?:,(?:[1-9]|1[012])(?:(?:-|\/|\,)(?:[1-9]|1[012]))?(?:L|W)?)*|\?|\*|(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?(?:,(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?)*)\s+(\?|\*|(?:[0-6])(?:(?:-|\/|\,|#)(?:[0-6]))?(?:L)?(?:,(?:[0-6])(?:(?:-|\/|\,|#)(?:[0-6]))?(?:L)?)*|\?|\*|(?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?(?:,(?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?)*)(|\s)+(\?|\*|(?:|\d{4})(?:(?:-|\/|\,)(?:|\d{4}))?(?:,(?:|\d{4})(?:(?:-|\/|\,)(?:|\d{4}))?)*))$

Note also that for cron scheduling in AWS You cannot use * in both the Day-of-month and Day-of-week fields. If you use it in one, you must use ? in the other. 

Troubleshooting Stack Updates

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/troubleshooting.html 

Check the Status reason column in the Events tab of your Stack

Example: Incorrectly formatted cron schedule...

Status reason shows...

Parameter ScheduleExpression is not valid. (Service: AmazonCloudWatchEvents; Status Code: 400; Error Code: ValidationException; Request ID: 21d36604-8adb-4773-a0b2-d69b59cae404)

Entered string matched template AllowedPattern (validated at regex101).

Google search revealed that "You cannot use * in both the Day-of-month and Day-of-week fields. If you use it in one, you must use ? in the other." In fact one or the other must always be ?. Initial setting was 5 9 * * 4 * working setting is 5 9 ? * 4 * 

https://stackoverflow.com/questions/39482314/parameter-scheduleexpression-is-not-valid https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html 

Delete Stack

From the Cloudformation console...

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-delete-stack.html 

From the AWS CLI...

aws cloudformation delete-stack --stack-name mystack

To delete a stack with dependencies that can't be deleted...

From the Cloudformation console...

From the AWS CLI...

aws cloudformation delete-stack --stack-name my-stack --retain-resources myresource1 myresource2

Drift Detection

aws cloudformation detect-stack-drift --stack-name mystack

Bibliography

https://eu-west-2.console.aws.amazon.com/cloudformation/home?region=eu-west-2#/https://www.observian.com/blog/aws-cloudformation-101-introductionhttps://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-formats.html
https://aws.amazon.com/cloudformation/https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-whatis-howdoesitwork.html https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/best-practices.html https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/gettingstarted.templatebasics.html https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-cli-creating-stack.html https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/troubleshooting.html https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacksets-concepts.html 
https://confluence.atlassian.com/jirakb/use-the-jira-config-properties-file-to-customize-an-aws-quick-start-deployment-993922385.html
https://www.1strategy.com/blog/2020/01/16/leveraging-cloudformation-parameter-constraints-to-enforce-resource-configuration/ https://regex101.com/ 
https://stackoverflow.com/questions/39482314/parameter-scheduleexpression-is-not-valid 
Designerhttps://eu-west-2.console.aws.amazon.com/cloudformation/designer/home
Delete Stackhttps://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-delete-stack.htmlhttps://repost.aws/knowledge-center/cloudformation-stack-delete-failed
Quickstartshttps://aws.amazon.com/quickstart/https://docs.aws.amazon.com/quickstarts/latest/s3backup/step-1-create-bucket.htmlhttps://aws-quickstart.github.io/option1.html
Drift Detectionhttps://dzone.com/articles/introduction-to-aws-cloudformation-drift-detection https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-drift.html 
Logshttps://aws.amazon.com/blogs/devops/view-cloudformation-logs-in-the-console/