amazon-web-servicesyamlaws-cloudformationintrinsicssam

using !Ref in second argument in SAM template


The below code is giving me error:

NOTIFICATION_ARN: !Sub
    -  'arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${stage}-${ServiceName}'
    -  ServiceName: !Ref ServiceName
    -  stage: !Ref Stage

error:Template error: One or more Fn::Sub intrinsic functions don't specify expected arguments. Specify a string as first argument, and an optional second argument to specify a mapping of values to replace in the string

How can I resolve this error? Stage is defined inside parameter section in sam template


Solution

  • You have one too many -. It should be:

    NOTIFICATION_ARN: !Sub
        -  'arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${stage}-${ServiceName}'
        -  ServiceName: !Ref ServiceName
           stage: !Ref Stage