Home > Software design >  Using cloudformation condition to create Root volume if development enviornment is choosen
Using cloudformation condition to create Root volume if development enviornment is choosen

Time:05-19

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.

CodePudding user response:

!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