Using cloudformation condition to create Root volume if development enviornment is choosen

Viewed 43

I am trying to create a root volume is production env. is chosen from parameters, which works fine, but if dev is chosen, it throws an error. "Value of property BlockDeviceMappings must be of type List"

Parameters:
  Enviornment:
    Type: String
    Description: Enter the enviornment where you want the instance deployed
    AllowedValues:
      - Production
      - Development
Conditions: 
  isProduction: !Equals [!Ref Enviornment, Production]
Resources:
  Ec2Instace:
    Type: AWS::EC2::Instance
    Properties:
      AvailabilityZone: !Ref AvailabilityZone
      ImageId: !Ref AmiId
      InstanceType: !Ref InstanceType
      KeyName: !Ref Ec2KeyPair
      SecurityGroupIds:
        - !GetAtt SecurityGp.GroupId
      SubnetId: !Ref SubnetId
      BlockDeviceMappings:
        !If
            - isProduction
            -
              - DeviceName: /dev/xvda
                Ebs:
                      VolumeType: gp2 
                      VolumeSize: 21
            - !Ref Enviornment

What am I doing wrong that's causing the Production scenario to work but development to throw error.

1 Answers

!Ref Enviornment is incorrect. I guess you want to have the following:

Resources:
  Ec2Instace:
    Type: AWS::EC2::Instance
    Properties:
      AvailabilityZone: !Ref AvailabilityZone
      ImageId: !Ref AmiId
      InstanceType: !Ref InstanceType
      KeyName: !Ref Ec2KeyPair
      SecurityGroupIds:
        - !GetAtt SecurityGp.GroupId
      SubnetId: !Ref SubnetId
      BlockDeviceMappings:
        !If
            - isProduction
            -
              - DeviceName: /dev/xvda
                Ebs:
                      VolumeType: gp2 
                      VolumeSize: 21
            - !Ref "AWS::NoValue"
Related