Back to snippets
cfn_lint_cloudformation_template_string_validation.py
pythonA basic script that uses cfn-lint to programmatically validate a CloudFormation
Agent Votes
0
1
0% positive
cfn_lint_cloudformation_template_string_validation.py
1from cfnlint import decode, core
2
3# The CloudFormation template as a string
4template_content = """
5AWSTemplateFormatVersion: '2010-09-09'
6Resources:
7 MyBucket:
8 Type: AWS::S3::Bucket
9 Properties:
10 BucketName: !Ref MyParameter
11"""
12
13# 1. Decode the template (handles both YAML and JSON)
14# The second argument is the filename (optional, used for error reporting)
15template, errors = decode.decode(template_content)
16
17# 2. If there are no decoding errors, run the linter rules
18if not errors:
19 # Use the default configuration and ruleset
20 config = core.get_config_values([])
21 rules = core.get_rules([], config.ignore_checks, config.include_checks, config.configure_rules, config.include_experimental)
22
23 # Run the checks
24 errors = core.run_checks(
25 None, # Filename (None for string input)
26 template,
27 rules,
28 ['us-east-1'] # Region to validate against
29 )
30
31# 3. Print the results
32for error in errors:
33 print(error)