geofffranks / spruce

A BOSH template merge tool
MIT License
426 stars 77 forks source link

Use spruce to merge cloudformation yaml templates #377

Open sqqqrly opened 1 year ago

sqqqrly commented 1 year ago

When I try spruce merge test1.yaml test2.yaml > test-all.yaml the result has all of the cloudformation functions (!Ref, !Sub, !GetAtt) silently removed.

Q. Is there a work-around? Seems all spruce needs to do is to leave the cloudformation functions unchanged in values.

Here are links to the official AWS CloudFormation documentation pages for !Ref and !Sub:

Example

# test1.yaml
Resources:
  MyBucket1:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub my-bucket1-${Environment}
  MyTopic1:
    Type: AWS::SNS::Topic
    Properties:
      DisplayName: !Sub My Topic1 - ${Environment}
      TopicName: !Sub my-topic1-${Environment}
Outputs:
  MyBucket1Arn:
    Value: !GetAtt MyBucket1.Arn
  MyTopic1Arn:
    Value: !Ref MyTopic1
# test2.yaml
Resources:
  MyBucket2:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub my-bucket2-${Environment}
  MyTopic2:
    Type: AWS::SNS::Topic
    Properties:
      DisplayName: !Sub My Topic2 - ${Environment}
      TopicName: !Sub my-topic2-${Environment}
Outputs:
  MyBucket2Arn:
    Value: !GetAtt MyBucket2.Arn
  MyTopic2Arn:
    Value: !Ref MyTopic2
╰─➤  spruce merge test1.yaml test2.yaml > test-all.yaml
# test-all.yaml
Outputs:
  MyBucket1Arn:
    Value: MyBucket1.Arn
  MyBucket2Arn:
    Value: MyBucket2.Arn
  MyTopic1Arn:
    Value: MyTopic1
  MyTopic2Arn:
    Value: MyTopic2
Resources:
  MyBucket1:
    Properties:
      BucketName: my-bucket1-${Environment}
    Type: AWS::S3::Bucket
  MyBucket2:
    Properties:
      BucketName: my-bucket2-${Environment}
    Type: AWS::S3::Bucket
  MyTopic1:
    Properties:
      DisplayName: My Topic1 - ${Environment}
      TopicName: my-topic1-${Environment}
    Type: AWS::SNS::Topic
  MyTopic2:
    Properties:
      DisplayName: My Topic2 - ${Environment}
      TopicName: my-topic2-${Environment}
    Type: AWS::SNS::Topic
sqqqrly commented 1 year ago

I have tried using a \!Ref which mostly works, but leaves the backslash in the output yaml. It requires post processing to fix.