AWS CloudFormation Template Flip

repository·master·Indexed 21 days ago

https://github.com/awslabs/aws-cfn-template-flip

A utility for converting AWS CloudFormation templates between JSON and YAML formats. It provides a Python API with functions like flip(), to_json(), and to_yaml(), as well as a cfn-flip CLI tool. The utility supports YAML's short function syntax and includes a clean_up parameter for CloudFormation-specific sanitation.

Tokens
1.8K
Snippets
7
Records
7
Agent score
27%

What's inside aws-cfn-template-flip

  1. Install AWS CloudFormation Template Flip

    master

    Install the package using pip. Note that the CLI tool is named cfn-flip (with a hyphen), while the Python package is imported as cfn_flip (with an underscore).

    Note: The CLI usage is deprecated. You should use rain (rain fmt) instead. This deprecation does not affect the Python API.

    pip install cfn-flip
  2. Configure max_col_width

    master

    The max_col_width parameter controls the maximum number of columns before a breakline is inserted. The default value is 200.

    You can configure this via an environment variable or directly in Python.

    # Linux/Unix
    export CFN_MAX_COL_WIDTH=120
    
    # Windows
    SET CFN_MAX_COL_WIDTH=120
    from cfn_tools._config import config
    from cfn_flip import flip, to_yaml, to_json
    
    # Change the default number of columns to break line to 120
    config['max_col_width'] = "120"
    
    # Subsequent calls will use the new config
    some_yaml = to_yaml(some_json)
  3. Use the cfn_flip Python API

    master

    Import flip, to_yaml, or to_json from cfn_flip. All functions expect a string containing serialized data and return a string containing serialized data. They raise an exception if parsing fails.

    • flip(data): Detects format and returns the opposite (JSON $\leftrightarrow$ YAML).
    • to_json(yaml_string): Converts YAML input to JSON output.
    • to_yaml(json_string, clean_up=False): Converts JSON input to YAML output.

    All three functions support the clean_up parameter, which performs opinionated CloudFormation-specific sanitation (e.g., converting Fn::Join to Fn::Sub).

    from cfn_flip import flip, to_yaml, to_json
    
    # Automatically flip format
    some_yaml_or_json = flip(some_json_or_yaml)
    
    # Explicitly convert to JSON
    some_json = to_json(some_yaml)
    
    # Explicitly convert to YAML with cleanup
    clean_yaml = to_yaml(some_json, clean_up=True)
  4. Reference: cfn-flip CLI options

    master

    Available options for the cfn-flip command line tool:

    Options:
      -i, --input [json|yaml]   Specify the input format. Overrides -j and -y flags.
      -o, --output [json|yaml]  Specify the output format. Overrides -j, -y, and -n flags.
      -j, --json                Convert to JSON. Assume the input is YAML.
      -y, --yaml                Convert to YAML. Assume the input is JSON.
      -c, --clean               Performs some opinionated cleanup on your template.
      -l, --long                Use long-form syntax for functions when converting to YAML.
      -n, --no-flip             Perform other operations but do not flip the output format.
      --version                 Show the version and exit.
      --help                    Show this message and exit.
  5. Use the cfn-flip CLI tool

    master

    The cfn-flip CLI converts CloudFormation templates between JSON and YAML. It automatically detects the input format and returns the opposite.

    Note: The CLI is deprecated in favor of rain.

    # Reading from stdin and outputting to stdout
    cat examples/test.json | cfn-flip
    
    # Reading from a file and outputting to stdout
    cfn-flip examples/test.yaml
    
    # Reading from a file and outputting to another file
    cfn-flip examples/test.json output.yaml
    
    # Reading from a file and cleaning up the output
    cfn-flip -c examples/test.json
  6. Reference the AWS CloudFormation Template Flip CLI options

    master

    The following options are available for the flip command:

    --input, -i <json|yaml>    Specify the input format. Overrides -j and -y flags.
    --output, -o <json|yaml>   Specify the output format. Overrides -j, -y, and -n flags.
    --json, -j                  Convert to JSON. Assume the input is YAML.
    --yaml, -y                  Convert to YAML. Assume the input is JSON.
    --clean, -c                 Performs some opinionated cleanup on your template.
    --long, -l                  Use long-form syntax for functions when converting to YAML.
    --no-flip, -n               Perform other operations but do not flip the output format.
    
    ARGUMENTS:
        input                      Input file (defaults to stdin)
        output                     Output file (defaults to stdout)
  7. Use the AWS CloudFormation Template Flip CLI

    master

    The flip command-line tool converts AWS CloudFormation templates between JSON and YAML formats. It is designed to leverage YAML's short function syntax where possible. By default, it attempts to guess the input format based on the file extension. If no input file is provided, it reads from stdin and writes to stdout.

    # Convert a JSON template to YAML
    flip template.json -y
    
    # Convert a YAML template to JSON
    flip template.yaml -j
    
    # Use pipes
    cat template.json | flip -y > template.yaml