Timothy Clotworthy
Timothy Clotworthy

Reputation: 2502

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?

Upvotes: 0

Views: 36

Answers (2)

Timothy Clotworthy
Timothy Clotworthy

Reputation: 2502

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}"

Upvotes: 0

fa44
fa44

Reputation: 502

You can try following:

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

Fn::ImportValue documentation:

Important

You can't use the short form of !ImportValue when it contains the short form of !Sub.

# do not use
!ImportValue
  !Sub '${NetworkStack}-SubnetID'

Instead, you must use the full function name, for example:

Fn::ImportValue:
  !Sub "${NetworkStack}-SubnetID"

Upvotes: 0

Related Questions