Search code examples
amazon-web-servicesaws-cloudformationaws-cloudformation-custom-resource

Using Multiple Conditions to create Resources in CloudFormation Template


I have a cloud AWS Formation Template, like this:

Parameters:
   FirewallConditionValue:
    Type: AWS::SSM::Parameter::Value<String>
    Default: "/mlops/deploy/firewallParamValue"

   EcrApiConditionValue:
    Type: AWS::SSM::Parameter::Value<String>
    Default: "/mlops/deploy/ecr.api"

Conditions:
   CreateVpcEndpoint: !Not [!Equals [!Ref FirewallConditionValue, 'ReuseInfra']]
   EcrApiEndpoint: !Equals [!Ref EcrApiConditionValue, 'false']

Resources:
   ECRAPIVPCE:
    Condition: !Or
      - !Condition EcrApiEndpoint
      - !Condition CreateVpcEndpoint
    Type: AWS::EC2::VPCEndpoint
    Properties:
      VpcEndpointType: Interface
      PrivateDnsEnabled: true
      SubnetIds:
        - !Ref PrivateSubnet1
      SecurityGroupIds:
        - !Ref SecurityGroup
      ServiceName: !Sub 'com.amazonaws.${AWS::Region}.ecr.api'
      VpcId: !Ref VPC

I want to create the resources if either of the condition is True, But when I run this, it is giving Template format error: Every Condition member must be a string..

Is something missing here?


Solution

  • Define a new condition for your Or statement in Conditions section, e.g. ErcAPiOrEndponit, Then in your Resources you will referer to that new condition:

    Resources:
       ECRAPIVPCE:
        Condition: ErcAPiOrEndponit