aws-cloudformation / rain

A development workflow tool for working with AWS CloudFormation.
Apache License 2.0
771 stars 69 forks source link

The format command does not support yaml anchors when converting to json #257

Closed ericzbeard closed 7 months ago

ericzbeard commented 7 months ago

YAML anchors are supported by Go, but when trying to convert to JSON with rain fmt -j, we get an error:

yaml: unknown anchor 'fooval' referenced

Resources:
  MyLambda:
    Type: AWS::Lambda::Function
    Properties:
      Code: &code
        ZipFile: "print(\"Hello World!\")\n"
      FunctionName: my-lambda
      Handler: index.lambda_handler
      Role: !ImportValue 'MyLambdaRoleArn'
      Runtime: python3.8
      Tags:
        - Key: foo
          Value: &fooval myval
        - Key: bar
          Value: *fooval
    Metadata:
      Comment: *code
ericzbeard commented 7 months ago

The handleScalar function in cft/format/json.go looks at each node independently, without knowledge of the rest of the template.

ericzbeard commented 7 months ago

We could fix the code to handle anchors better for JSON conversion, but we will not be able to round-trip it, since JSON doesn't have an equivalent to anchors. I assume that would be ok for any use case where someone wants to use a YAML feature not supported by CloudFormation.

I wonder if the fmt command should resolve the anchors and get rid of them for YAML formatting as well.

j5nb4l commented 7 months ago

Our company is moving to rain as a replacement for cfn-flip since it is no longer actively maintained. cfn-flip supported anchors, so it would be great if rain could maintain that support. I hope this enhancement will be considered.

I wonder if the fmt command should resolve the anchors and get rid of them for YAML formatting as well.

I don't know about this one. It makes sense for the anchors to be resolved when transforming to JSON, but resolving them when all you wanted to do was format your template may catch users unaware. I think it would be fine if the rain fmt command is executed without the -w flag, but should probably be avoided otherwise. In addition, if the anchors are going to be resolved, rain should explicitly notify the user of that fact, both in the help messages but also in the output of the rain fmt command

ericzbeard commented 7 months ago

I could add a new flag to the command, like rain fmt --anchor to explicitly resolve them.

ericzbeard commented 7 months ago

Resolving anchors should probably be done by rain pkg instead of rain fmt.

ericzbeard commented 7 months ago

Apparently the AWS CLI does understand anchors, using the package command.

aws cloudformation package --template-file test/templates/anchors.yaml --s3-bucket my-bucket

I'll add this to rain pkg so you can pipe the output to rain fmt

j5nb4l commented 7 months ago

Great idea. That should provide a better UE.