如果条件在BlockdeviceMapping中

时间:2017-10-01 00:22:41

标签: amazon-web-services amazon-cloudformation

如果已经有“可用”卷,我试图在启动配置中添加“if”条件以跳过添加新EBS卷。 所以我想要实现的逻辑是,如果下面的检查变量为Null,则添加新的卷,否则跳过因为我将从用户数据中添加“可用”卷。 $ check = Get-EC2Volume -Filter @ {Name =“status”;值=“可用”}

BlockDeviceMappings:
        - DeviceName: /dev/sda1
          Ebs:
            VolumeType: gp2            
            VolumeSize: '100'
        !if $check              --> not sure how to put if condition here 
        - DeviceName: /dev/sdb   
          Ebs:
            DeleteOnTermination: "false"            
            VolumeSize: '50'
            VolumeType: gp2                 
        - DeviceName: /dev/sdc
          Ebs:
            DeleteOnTermination: "false"            
            VolumeSize: '50'
            VolumeType: gp2        

2 个答案:

答案 0 :(得分:0)

以下是如何添加条件的示例代码

            BlockDeviceMappings:
            - !If
                - IsNeedVoume
                - DeviceName: "/dev/xvdcz"
                  Ebs:
                    VolumeSize: !Ref InstanceDockerVolume
                    VolumeType: 'gp2'
                - !Ref AWS::NoValue

答案 1 :(得分:0)

您可以创建一个条件,即 AddEDrive ,用于检查是否指定了参数 EDriveSize 。如果是,那么它将创建BlockDeviceMapping,否则,什么也不做。

根据documentation

/dev/sda1DeviceName的推荐C:\

对于所有其他附加驱动器,建议/dev/xvd[f-z]

DeviceName


AWSTemplateFormatVersion: '2010-09-09'
Conditions:
  AddCDrive: !Not [!Equals [!Ref CDriveSize, '']]
  AddDDrive: !Not [!Equals [!Ref DDriveSize, '']]
  AddEDrive: !Not [!Equals [!Ref EDriveSize, '']]
Parameters:
  CDriveSize: {Default: '', Type: String}
  DDriveSize: {Default: '', Type: String}
  EDriveSize: {Default: '', Type: String}
Resources:
  Instance:
    Properties:
      BlockDeviceMappings:
        - !If
          - AddCDrive
          - DeviceName: '/dev/sda1'
            Ebs:
              VolumeSize: !Ref CDriveSize
              VolumeType: gp2
          - !Ref AWS::NoValue
        - !If
          - AddDDrive
          - DeviceName: '/dev/xvdf'
            Ebs:
              VolumeSize: !Ref DDriveSize
              VolumeType: gp2
          - !Ref AWS::NoValue
        - !If
          - AddEDrive
          - DeviceName: '/dev/xvdg'
            Ebs:
              VolumeSize: !Ref EDriveSize
              VolumeType: gp2
          - !Ref AWS::NoValue