Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

!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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading