Search code examples
amazon-web-servicesfunctionvariablesaws-lambdasam

Does AWS sam cli print template.yaml config file with all variables resolved


I have switched from serverless to sam cli. One useful function serverless had was serverless print which allowed you to print the output of your yaml file with all the local variables resolved. This was a useful tool for checking if your syntax is correct or if the variables are resolving as you expect.

Is that any way to do this with AWS sam cli?

e.g.

sam print


Solution

  • You can achieve this using Outputs section of SAM template.
    You can check the AWS SAM template anatomy to understand better.

    Outputs (optional)

    The values that are returned whenever you view your stack's properties. For example, you can declare an output for an S3 bucket name, and then call the aws cloudformation describe-stacks AWS Command Line Interface (AWS CLI) command to view the name. This section corresponds directly with the Outputs section of AWS CloudFormation templates.

    You will need to make use of Intrinsic functions within your Outputs section to print out the final resolved value at runtime.

    Outputs:
      BackupLoadBalancerDNSName:
        Description: The DNSName of the backup load balancer
        Value: !GetAtt BackupLoadBalancer.DNSName
        Condition: CreateProdResources
      InstanceID:
        Description: The Instance ID
        Value: !Ref EC2Instance
    

    If you just wish to validate if your SAM is correct or not you could use the following command:

    $ sam validate
    
    Posting the answer here so that it may help others in future!