Chapter 29 · AWS Cloudformation
Subchapter 29.3
references/cloudformation-pre-deploy-validation.script.mdMarkdown13 KBView on GitHub
Deterministic procedure for running CloudFormation’s pre-deployment validation feature. When a change set is created, CloudFormation automatically validates the template against three common failure causes before any resources are provisioned:
Validation errors are exposed through the describe-events API scoped to the change set. This procedure uses call_aws (preferred) or the AWS CLI to invoke these APIs directly. Note: The AWS MCP server is recommended for streamlined API invocation, but all steps can be performed using the AWS CLI alone.
Important: The legacy describe-stack-events API does NOT return validation errors. You MUST use describe-events --change-set-name <arn> to retrieve validation results.
CREATE (new stack) or UPDATE (existing stack).CAPABILITY_IAM, CAPABILITY_NAMED_IAM) if the template creates IAM resources.Constraints for parameter acquisition:
Check which mechanism is available to invoke AWS APIs.
Constraints:
call_aws tool from the AWS MCP Server (preferred for sandboxed execution, audit logging, and observability)aws) available on the user’s system (verify with which aws or aws --version)aws sts get-caller-identity --region <region>). This read-only call is acceptable during verification because it does not modify any resources<platform-specific command>. Do you want me to install it, or would you prefer to install it manually?”aws configure, environment variables, or their preferred credential provider) and MUST NOT proceed until credentials are confirmedCatch issues locally before consuming CloudFormation API quota.
Constraints:
validate-cloudformation-template SOP first to catch cfn-lint syntax and schema errors locallycheck-cloudformation-template-compliance SOP to catch security violations locallyPrepare the template for the change set.
Constraints:
--template-body--template-url because --template-body has a size limit--template-url directlyCreate the change set to trigger pre-deployment validation. Validation runs automatically during change set creation — no opt-in is required.
Constraints:
You MUST use a unique, descriptive change set name (e.g., pre-deploy-validation-<timestamp>)
You MUST use the appropriate --change-set-type (CREATE for new stacks, UPDATE for existing)
You MUST include --capabilities if the template creates IAM resources (e.g., CAPABILITY_IAM, CAPABILITY_NAMED_IAM)
You MUST invoke via call_aws (preferred) or the AWS CLI. Example CLI form:
aws cloudformation create-change-set \
--stack-name <stack_name> \
--template-body file://<path> \
--change-set-name pre-deploy-validation-$(date +%s) \
--change-set-type CREATE \
--region <region> \
--capabilities CAPABILITY_IAMNotes: Use
--template-url s3://...instead of--template-bodyfor templates exceeding 51,200 bytes. Include--capabilitiesonly if the template creates IAM resources.
You MUST capture the returned change set ARN (Id) for the next step
You MUST explain to the user that creating a change set does NOT modify any resources because it only plans the changes and runs validation
You MUST wait for change set creation to reach a terminal status (CREATE_COMPLETE, FAILED) before checking validation results. Use describe-change-set to poll status.
Fetch validation results from the describe-events API.
Constraints:
aws cloudformation describe-events --change-set-name <arn> --region <region> (via call_aws or CLI)describe-stack-events because the legacy stack events API does NOT return validation errors — it only surfaces resource provisioning events after executionEventType equals VALIDATION_ERROR because these are the validation findingsValidationName — one of PROPERTY_VALIDATION, RESOURCE_NAME_CONFLICT, S3_BUCKET_EMPTINESSValidationStatus — FAILED or PASSEDValidationStatusReason — detailed error messageValidationPath — property path in the template where the error occurredValidationFailureMode — FAIL (blocks execution) or WARN (allows execution)VALIDATION_ERROR events are returned, You MUST treat the change set as having passed all validationsReport validation findings grouped by type and help the user fix issues.
Constraints:
ValidationName:
ValidationPath so the user can pinpoint the exact location in their templateFAIL (execution blocked) from WARN (execution allowed) so the user knows what MUST be fixed versus what SHOULD be consideredFAIL-mode failures exist, You MUST recommend fixing the template and creating a new change setFAIL-mode validation failures because CloudFormation will block execution and the change set cannot succeedWARN-mode issues exist, You SHOULD explain the warning and let the user decideGuide the user on next steps after validation.
Constraints:
WARN-mode issues that the user accepts), You MUST ask the user for explicit approval before executing the change setREVIEW_IN_PROGRESS by calling describe-stacksaws cloudformation execute-change-set --change-set-name <arn> --region <region>UPDATE-type change sets: recommend deleting the change set to keep the stack clean: aws cloudformation delete-change-set --change-set-name <arn> --region <region>CREATE-type change sets: You MUST recommend also deleting the stack (after user approval), because it remains in REVIEW_IN_PROGRESS state and will block future creates: aws cloudformation delete-change-set --change-set-name <arn> --region <region> followed by aws cloudformation delete-stack --stack-name <stack_name> --region <region>--change-set-type CREATE, You MUST warn the user that the stack now exists in REVIEW_IN_PROGRESS state. Before retrying with --change-set-type CREATE, the user MUST first delete the stack (with user approval). Alternatively, the user can delete only the failed change set and create a new CREATE change set against the same stack.Change set "pre-deploy-validation-1713580000" created for stack "my-app-stack".
Retrieved via: aws cloudformation describe-events --change-set-name arn:aws:cloudformation:...
Validation results:
✓ PROPERTY_VALIDATION: PASSED
✓ RESOURCE_NAME_CONFLICT: PASSED
✓ S3_BUCKET_EMPTINESS: PASSED
The change set is ready to execute. Would you like to execute it now?Change set "pre-deploy-validation-1713580000" created for stack "my-app-stack".
Retrieved via: aws cloudformation describe-events --change-set-name arn:aws:cloudformation:...
✗ PROPERTY_VALIDATION (FAIL):
ValidationPath: /Resources/MyBucket/Properties/NotificationConfiguration/QueueConfigurations/0
ValidationStatusReason: required key [Event] not found
Fix (Resources/MyBucket/Properties/NotificationConfiguration/QueueConfigurations):
QueueConfigurations:
- Queue: !GetAtt MyQueue.Arn
Event: s3:ObjectCreated:* # Required property was missing
✗ RESOURCE_NAME_CONFLICT (FAIL):
ValidationPath: /Resources/MyDynamoDBTable/Properties/TableName
ValidationStatusReason: A table named "users-table" already exists in this account/region.
Fix: Make the name unique per stack:
TableName: !Sub "${AWS::StackName}-users-table"
⚠ S3_BUCKET_EMPTINESS (WARN):
ValidationPath: /Resources/DataBucket
ValidationStatusReason: Bucket is not empty. Delete may fail.
Options:
- Empty the bucket before stack deletion
- Or set DeletionPolicy: Retain on the bucket resource
2 FAIL-mode issues must be fixed before execution.
Fix the template and create a new change set.The describe-events API (scoped to change sets) requires AWS CLI support for the command. If it is not recognized, update the AWS CLI: pip install --upgrade awscli or brew upgrade awscli. If the command still returns nothing, confirm the change set ARN is correct and the change set has finished creating.
describe-stack-events returns events after the stack begins provisioning. It does NOT include pre-deployment validation errors. You MUST redirect the user to describe-events --change-set-name <arn>.
Use aws cloudformation describe-change-set --change-set-name <arn> to check the status. Wait until it reaches CREATE_COMPLETE or FAILED before calling describe-events.
If describe-change-set shows Status: FAILED with a StatusReason unrelated to validation (e.g., “No updates are to be performed”), the failure is not a pre-deployment validation issue. Investigate the StatusReason directly.
S3 bucket emptiness validation requires s3:ListBucket permission on the buckets being deleted. If this validation is skipped or errors, verify the deploying role has this permission.
Pre-deployment validation catches three common classes of issues but cannot detect all runtime failures (resource limits, service constraints, IAM permissions, invalid AMI IDs). If deployment fails after validation passes, use the troubleshoot-cloudformation-deployment tool or SOP to diagnose the runtime failure.