It’s pretty hard to express abstraction and control flow in a format like YAML or XML. You’ll probably find less friction in using <a href="https://aws.amazon.com/cdk/" rel="nofollow noreferrer">https://aws.amazon.com/cdk/</a> to generate arbitrary CloudFormation templates from a mature programming language.<p>I’ve occasionally done the same with Terraform, writing Python or Groovy to generate Terraform-flavored JSON rather than hand-write HCL templates that would be too verbose to understand and maintain (looking at you, DynamoDB autoscaling).