awslabs / goformation

GoFormation is a Go library for working with CloudFormation templates.
Apache License 2.0
841 stars 197 forks source link

Intrinsics and Ref not working #554

Closed scottiedog45 closed 1 year ago

scottiedog45 commented 1 year ago

Given the following code and template, I would expect useast1-foo-qa-resources, but I get useast1-Environment-resources. I am new to the package, so might be using it incorrectly. Or, is this a bug?

package main

import (
    "fmt"

    "github.com/awslabs/goformation/v7"
    "github.com/awslabs/goformation/v7/intrinsics"
)

func main() {
    templ, err := goformation.OpenWithOptions(
        "./cf.yaml",
        &intrinsics.ProcessorOptions{ParameterOverrides: map[string]interface{}{
            "Environment": "qa",
        }},
    )
    if err != nil {
        panic(err)
    }
    res := templ.GetAllSQSQueueResources()["SomeQueue"]
    fmt.Println(*res.QueueName)
}
AWSTemplateFormatVersion: 2010-09-09
Description: The AWS CloudFormation template
Parameters:
  Environment:
    Type: String
    Description: Environment name - dev, qa, or prod
    AllowedValues:
      - dev
      - qa
      - prod
    ConstraintDescription: Must be dev, qa, or prod

Mappings:
  Constant:
    Value:
      Name: foo

Resources:
  SomeQueue:
    Type: AWS::SQS::Queue
    Properties:
      RedrivePolicy:
        maxReceiveCount: 3
      QueueName: !Join
        - '-'
        - - 'useast1'
          - !FindInMap [ Constant, Value, Name ]
          - !Ref Environment
          - 'resources'
xrn commented 1 year ago

Probably duplicates https://github.com/awslabs/goformation/issues/551

drmmarsunited commented 1 year ago

@scottiedog45 The issue here seems to be nested short form tags not working as expected. Also, in the template the Environment parameter doesn't have a default. I tested with the following template:

AWSTemplateFormatVersion: 2010-09-09
Description: The AWS CloudFormation template
Parameters:
  Environment:
    Type: String
    Description: Environment name - dev, qa, or prod
    AllowedValues:
      - dev
      - qa
      - prod
    ConstraintDescription: Must be dev, qa, or prod

Mappings:
  Constant:
    Value:
      Name: foo

Resources:
  SomeQueue:
    Type: AWS::SQS::Queue
    Properties:
      RedrivePolicy:
        maxReceiveCount: 3
      QueueName:
        "Fn::Join":
          - '-'
          - - 'useast1'
            - !FindInMap [ Constant, Value, Name ]
            - !Ref Environment
            - 'resources'

After template resolution with the library, here are my results, which conform to my expectations (after changing the top level join to a long form version):

AWSTemplateFormatVersion: "2010-09-09T00:00:00Z"
Description: The AWS CloudFormation template
Mappings:
    Constant:
        Value:
            Name: foo
Parameters:
    Environment:
        AllowedValues:
            - dev
            - qa
            - prod
        ConstraintDescription: Must be dev, qa, or prod
        Description: Environment name - dev, qa, or prod
        Type: String
Resources:
    SomeQueue:
        Properties:
            QueueName: useast1-foo-resources
            RedrivePolicy:
                maxReceiveCount: 3
        Type: AWS::SQS::Queue

As there is no default value for Environment, it is omitted in the joined output of useast1-foo-resources. It seems as though as long as there is no nesting of short form tags within short form tags, everything remains happy.

rubenfonseca commented 1 year ago

Looking at this now, thank you so much for the great examples that help me debug the problem!

rubenfonseca commented 1 year ago

Can you please try version 7.8.1 that we just released to see if it fixes the problem?