AWS CloudFormation Template Flip

1.3.0 · active · verified Thu Apr 09

cfn-flip is a Python library and command-line tool for converting AWS CloudFormation templates between JSON and YAML formats, making use of YAML's short function syntax where possible. The library is currently at version 1.3.0, with the last release in 2021, suggesting a maintenance rather than active rapid development cadence for the core library, though the CLI usage is deprecated.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `to_yaml` and `to_json` functions to convert CloudFormation templates between JSON and YAML formats within a Python script. It shows conversion from a JSON string to YAML and from a YAML string to JSON.

from cfn_flip import to_yaml, to_json

json_template = '''
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Example JSON template",
  "Resources": {
    "MyS3Bucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "BucketName": "my-unique-example-bucket"
      }
    }
  }
}
'''

yaml_output = to_yaml(json_template)
print("--- JSON to YAML ---")
print(yaml_output)

yaml_template = '''
AWSTemplateFormatVersion: '2010-09-09'
Description: Example YAML template
Resources:
  MyLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.handler
      Role: !GetAtt MyLambdaRole.Arn
      Code:
        S3Bucket: my-code-bucket
        S3Key: my-code.zip
      Runtime: python3.9
'''

json_output = to_json(yaml_template)
print("\n--- YAML to JSON ---")
print(json_output)

view raw JSON →