0

I found ytt which is a tool to template YAML and appears to be heavily used in the k8s space which has gobs for YAML configuration.

The problem I have is that I would like to:

a) organize my SAM deployment definition into multiple files; and

b) parameterize my AWS SAM template.yml (description of how to deploy serverless applications in AWS via CloudFormation) from the "outside" without just using sed.

Is there a way I can keep the ! prefixed directives like !Ref, !Sub, !Join, etc in the output YAML via ytt? I attempted to use backslash to no avail.

I have a subdir named etc/ which has this file in it:

#@ load("@ytt:data", "data")
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: My REST API

#! Parameters

Parameters:

  DeployEnv:
    Type: String
    Default: #@ data.values.deploy_env
  UsagePlanType:
    Type: String
    Default: PER_API

#! Conditions

#! Globals

Globals:
  HttpApi:
    Auth:
      ApiKeyRequired: true
      UsagePlan:
        CreateUsagePlan: !Ref UsagePlanType

#! Resources

Resources:


  RestApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      StageName: !Ref DeployEnv


#! Outputs

Here is values.yml:

#@data/values
---
deploy_env: dev

The following is what it produces:

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: My REST API
Parameters:
  DeployEnv:
    Type: String
    Default: dev
  UsagePlanType:
    Type: String
    Default: PER_API
Globals:
  HttpApi:
    Auth:
      ApiKeyRequired: true
      UsagePlan:
        CreateUsagePlan: UsagePlanType
Resources:
  RestApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      StageName: DeployEnv

You will notice all the !Ref s are removed from the output. How do I let those directives remain in the output while using ytt. I reviewed the examples but I couldn't find the scenario I am describing illustrated.

The command I am using is:

ytt -f ./etc --data-value-yaml deploy_env=${DEPLOY_ENV:-dev} > dist/template.yml

That allows me to override the DeployEnv parameter in the SAM template.yml, but I need to be able to retain the references with the !Ref directives to get all the characteristics I desire from this approach.

My questions are either:

  1. How can I get ytt to retain directives like !Ref, !Sub, !Join, etc in the output template.yml file?
  2. How do other people deploying non-trivial serverless applications into AWS generate their template.yml so that they can organize the resources, globals, parameters, etc into separate files and get a level of parameterization of the template itself?

FWIW I have already read this suggested Q&A but it doesn't resolve my issue: how to include multiple resource files in SAM template.yml

snarflez
  • 1
  • 1

1 Answers1

0

How can I get ytt to retain directives like !Ref, !Sub, !Join, etc in the output template.yml file?

As mentioned in this ytt issue, you can use the more verbose CloudFormation format as follows: {Ref: DeployEnv}. So for your example above, it would be the following:

#@ load("@ytt:data", "data")
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: My REST API

#! Parameters

Parameters:

  DeployEnv:
    Type: String
    Default: #@ data.values.deploy_env
  UsagePlanType:
    Type: String
    Default: PER_API

#! Conditions

#! Globals

Globals:
  HttpApi:
    Auth:
      ApiKeyRequired: true
      UsagePlan:
        CreateUsagePlan: {Ref: UsagePlanType}

#! Resources

Resources:


  RestApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      StageName: {Ref: DeployEnv}


#! Outputs

How do other people deploying non-trivial serverless applications into AWS generate their template.yml so that they can organize the resources, globals, parameters, etc into separate files and get a level of parameterization of the template itself?

Not sure quite how to answer this portion of the question, but the GitHub issue above does have more discussion on this topic when using ytt.

dhelfand
  • 141
  • 1
  • 7