Try ruamel.yaml
package. This is my test code,
import boto3
import sys
from ruamel.yaml import YAML
session = boto3.session.Session(region_name='<region>')
client = session.client('cloudformation')
response = client.get_template(StackName='<stackname>')
yaml = YAML()
result = yaml.load(response['TemplateBody'])
yaml.dump(result, sys.stdout)
and the result is
AWSTemplateFormatVersion: '2010-09-09'
Description: >
AWS CloudFormation template to create a new VPC
or use an existing VPC for ECS deployment
in Create Cluster Wizard. Requires exactly 1
Instance Types for a Spot Request.
Parameters:
EcsClusterName:
Type: String
Description: >
Specifies the ECS Cluster Name with which the resources would be
associated
Default: default
EcsAmiId:
Type: String
Description: Specifies the AMI ID for your container instances.
EcsInstanceType:
Type: CommaDelimitedList
Description: >
Specifies the EC2 instance type for your container instances.
Defaults to m4.large
Default: m4.large
ConstraintDescription: must be a valid EC2 instance type.
...
The result
in my code is not string and even not dict type but it is a dict-like object of ruamel.yaml package. You can parse the element from the result
such as
result['AWSTemplateFormatVersion']
where it gives
2010-09-09