Search code examples
amazon-web-servicesaws-cloudformation

Use !ImportValue together with !Sub in a yaml template


I have a CF template where I export a value like this:

Outputs:
  LambdaLogGroup:
    Value: !Ref LambdaLogGroup
  Export:
    Name: !Sub "LambdaLogGroup-${EnvironmentName}${EnvironmentId}"

In another template, I attempt to import the name into a variable like this:

LOG_GROUP_NAME: !ImportValue !Sub "LambdaLogGroup-${EnvironmentName}${EnvironmentId}"

However, this is invalid combination of !ImportValue with !Sub. I have attempted other ways like this:

LOG_GROUP_NAME: !ImportValue 
  LambdaLogGroup-${EnvironmentName}${EnvironmentId}

and this:

LOG_GROUP_NAME: !ImportValue 
  !Sub "LambdaLogGroup-${EnvironmentName}${EnvironmentId}"

which are also invalid either invalid or do not work.

How can I get this dynamically-generated parameter into my LOG_GROUP_NAME variable?


Solution

  • commenter fa44 gave me great direction above (about how you can't use short forms of ImportValue and Sub at the same time). However, their suggestions did not work for me:

    LOG_GROUP_NAME:
      - Fn::ImportValue:
        !Sub "LambdaLogGroup-${EnvironmentName}${EnvironmentId}"
    

    I would still get yaml formatting errors with it.

    However, this did work for me:

    LOG_GROUP_NAME: !ImportValue
      Fn::Sub: "LambdaLogGroup-${EnvironmentName}-${EnvironmentId}"