Loading...
Validates AWS CloudFormation templates for syntax errors and best practices
{
"hookConfig": {
"hooks": {
"postToolUse": {
"script": "./.claude/hooks/aws-cloudformation-validator.sh",
"matchers": [
"write",
"edit"
]
}
}
},
"scriptContent": "#!/usr/bin/env bash\n\n# Read the tool input from stdin\nINPUT=$(cat)\nTOOL_NAME=$(echo \"$INPUT\" | jq -r '.tool_name')\nFILE_PATH=$(echo \"$INPUT\" | jq -r '.tool_input.file_path // .tool_input.path // \"\"')\n\nif [ -z \"$FILE_PATH\" ]; then\n exit 0\nfi\n\n# Check if it's a CloudFormation template\nif [[ \"$FILE_PATH\" == *.cf.json ]] || [[ \"$FILE_PATH\" == *.cf.yaml ]] || [[ \"$FILE_PATH\" == *cloudformation*.yaml ]] || [[ \"$FILE_PATH\" == *cloudformation*.json ]]; then\n echo \"☁️ Validating CloudFormation template $FILE_PATH...\" >&2\n \n # Try cfn-lint first (preferred)\n if command -v cfn-lint &> /dev/null; then\n echo \"Running cfn-lint validation...\" >&2\n if cfn-lint \"$FILE_PATH\" 2>&1; then\n echo \"✅ CloudFormation template validation passed\" >&2\n else\n echo \"❌ CloudFormation template validation failed\" >&2\n fi\n elif command -v aws &> /dev/null; then\n echo \"⚠️ cfn-lint not installed, using AWS CLI validation...\" >&2\n if aws cloudformation validate-template --template-body \"file://$FILE_PATH\" 2>/dev/null; then\n echo \"✅ Basic CloudFormation validation passed\" >&2\n else\n echo \"❌ CloudFormation template validation failed\" >&2\n fi\n else\n echo \"⚠️ Neither cfn-lint nor AWS CLI available for validation\" >&2\n fi\nelse\n echo \"File $FILE_PATH is not a CloudFormation template, skipping validation\" >&2\nfi\n\nexit 0"
}.claude/hooks/~/.claude/hooks/{
"hooks": {
"postToolUse": {
"script": "./.claude/hooks/aws-cloudformation-validator.sh",
"matchers": [
"write",
"edit"
]
}
}
}#!/usr/bin/env bash
# Read the tool input from stdin
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [ -z "$FILE_PATH" ]; then
exit 0
fi
# Check if it's a CloudFormation template
if [[ "$FILE_PATH" == *.cf.json ]] || [[ "$FILE_PATH" == *.cf.yaml ]] || [[ "$FILE_PATH" == *cloudformation*.yaml ]] || [[ "$FILE_PATH" == *cloudformation*.json ]]; then
echo "☁️ Validating CloudFormation template $FILE_PATH..." >&2
# Try cfn-lint first (preferred)
if command -v cfn-lint &> /dev/null; then
echo "Running cfn-lint validation..." >&2
if cfn-lint "$FILE_PATH" 2>&1; then
echo "✅ CloudFormation template validation passed" >&2
else
echo "❌ CloudFormation template validation failed" >&2
fi
elif command -v aws &> /dev/null; then
echo "⚠️ cfn-lint not installed, using AWS CLI validation..." >&2
if aws cloudformation validate-template --template-body "file://$FILE_PATH" 2>/dev/null; then
echo "✅ Basic CloudFormation validation passed" >&2
else
echo "❌ CloudFormation template validation failed" >&2
fi
else
echo "⚠️ Neither cfn-lint nor AWS CLI available for validation" >&2
fi
else
echo "File $FILE_PATH is not a CloudFormation template, skipping validation" >&2
fi
exit 0Hook recognizes CloudFormation file but cfn-lint fails
Install cfn-lint: pip install cfn-lint. Verify Python environment active: which python. Check template syntax with: cfn-lint --version. Review cfn-lint logs without 2>&1.
AWS CLI validation requires credentials unexpectedly
Use cfn-lint for offline validation instead. Or configure AWS credentials: aws configure. Use IAM role with minimal permissions. Skip AWS CLI fallback if credentials unavailable.
Template passes validation but hook shows failure message
Check exit code handling in script. Capture command output: OUTPUT=$(cfn-lint file) && echo success. Review conditional logic for success detection. Debug with: set -x in script.
Hook processes YAML files that aren't CloudFormation
Strengthen template detection regex. Check file content for AWSTemplateFormatVersion key: grep -q AWSTemplateFormatVersion file. Add explicit template marker in filename convention.
PostToolUse timing causes validation on incomplete writes
Verify file write completed before validation. Add small sleep: sleep 0.5 before validation. Check file size: [ -s "$FILE_PATH" ]. Use file lock detection if available.
Loading reviews...
Join our community of Claude power users. No spam, unsubscribe anytime.
Automated accessibility testing and compliance checking for web applications following WCAG guidelines
Automatically generates or updates API documentation when endpoint files are modified
Automatically formats code files after Claude writes or edits them using Prettier, Black, or other formatters