Search code examples
amazon-web-servicesaws-cloudformationamazon-ecs

Can't add network configuration in !If statement. "expected type: JSONObject, found: String" error


I'm trying to create a service that targets either Fargate or EC2 depending on the deployment environment (passed as a parameter). This gives me the error mentioned in the title:

expected type: JSONObject, found: String

The configuration works if I hard-code it without the !If conditional. Using a Cloudformation template in Yaml format.

The deployment happens via the AWS CLI v2

Here's the part of my template that I am having the issue with:

  Conditions:
    UseEC2: !Equals
      - dev
      - !Ref ParameterEnvironment
    UseFargate: !Not [
      !Equals [ dev, !Ref ParameterEnvironment ]
    ]
  WebService:
    Properties:
      Cluster: !Ref ClusterName
      DesiredCount: 1
      LaunchType: !If [ UseFargate, FARGATE, EC2 ]
      NetworkConfiguration:
        !If
        - UseFargate
        -
          AwsvpcConfiguration:
            AssignPublicIp: "ENABLED"
            SecurityGroups:
              - !Ref AppDefaultGroup
              - !Ref OtherGroup
            Subnets:
              - subnet-xxxxxxxx
              - subnet-yyyyyyyy
        - AWS::NoValue
      SchedulingStrategy: REPLICA
      PropagateTags: TASK_DEFINITION
      TaskDefinition: !Ref WebTaskDefinition
    Type: 'AWS::ECS::Service'

I'm trying to do this because having an Awsvpc configuration does not work on EC2


Solution

  • Instead of

     - AWS::NoValue
    

    it should be

     - !Ref AWS::NoValue