如果选择开发环境,使用 cloudformation 条件创建根卷

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

我正在尝试创建一个根卷是生产环境。从参数中选择,这工作正常,但如果选择 dev,它会抛出错误。 “属性 BlockDeviceMappings 的值必须是列表类型”

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

我做错了什么导致生产场景正常工作但开发抛出错误。

!Ref Enviornment 不正确。我猜你想要以下内容:

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"