Navjot Singh
Navjot Singh

Reputation: 764

Using importValue inside If Condition in aws cloudformation templates

I'm trying to find a way to use ImportValue inside If function but can't find a proper syntax. Any help is appreciated.

Below the code I'm trying:

SomeTaskdefinition:
  Type: AWS::ECS::TaskDefinition
  Properties:
    Family: 'FamilyName'
    ContainerDefinitions:
    - Name: ContainerName
      Image: 'imagename:net/v2/'
      Environment:
      - Name: ENV_VARIABLE_1
        Value:
          Fn::If:
            Fn::Equals:
              Fn::ImportValue:
                !Sub "${ImportStackname}-ECSCluster"
              ''
            'notpresent'
            'present'

Upvotes: 5

Views: 7729

Answers (4)

Angelo Tartaglia
Angelo Tartaglia

Reputation: 21

Using a combination of intrinsic function I've obtained the result:

parameter1: !If
    - condition1
    - !ImportValue 
      Fn::Sub: "${name}-dev-parameter"
    - AWS::NoValue

Make sure to use the sequence of intrinsic function as I've listed. It seems to be the only way.

Upvotes: 2

lapkritinis
lapkritinis

Reputation: 2814

I come across here with similar problem.. my idea was to either be able specify DatabaseHost as parameter, if left empty - value should be taken from DatabaseStack export. Here is my sample code - it uses !ImportValue inside !If function. You will get the idea (instead of constructing only Value - construct whole Name Value list object)

Conditions:
  DatabaseHostPresent: !Not [ !Equals [ !Ref DatabaseHost, ""]]

Resources:
 ...
       ContainerDefinitions:
        - Name: !Sub ${ApplicationName}-web-${EnvironmentName}
          Environment:
            - !If
              - DatabaseHostPresent
              - Name: DB_HOST
                Value: !Ref DatabaseHost
              - Name: DB_HOST
                Value: !ImportValue
                  Fn::Sub: ${DatabaseStack}-EndpointAddress

Upvotes: 6

claytond
claytond

Reputation: 1099

Another way to work around this is to use a nested template:

  • Add a Parameter to the Child template
  • In the Parent template, use the Import to populate the Parameter
  • In the Child template, you can do everything you'd want including using it in the Conditions section

Untested example:

# in Parent
Resources:
  ChildStack:
    Type: 'AWS::CloudFormation::Stack'
    Properties:
      Parameters:
        Stackname: {'Fn::ImportValue': !Sub "${ImportStackname}-ECSCluster"}
      TemplateURL: './child.yaml'

# in Child
Parameters:
  Stackname:
    Type: String
    Default: ''
Conditions:
  HasStack: !Not [!Equals [!Ref Stackname, '']]
Resources:
  SomeTaskdefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: 'FamilyName'
      ContainerDefinitions:
      - Name: ContainerName
        Image: 'imagename:net/v2/
        Environment:
        - Name: ENV_VARIABLE_1
          Value: !If [HasStack, 'present', 'notpresent']

Upvotes: 3

Milan Cermak
Milan Cermak

Reputation: 8074

I don't believe this is possible. You cannot use ImportValue inside an Equals function.

Upvotes: 2

Related Questions