【问题标题】:EC2 cloudformation template using conditions to add ebs volumeEC2 cloudformation模板使用条件添加ebs卷
【发布时间】:2020-05-20 18:34:13
【问题描述】:

我正在尝试根据是否需要 EBS 卷的条件将 EBS 卷添加到我的 EC2 资源。我怎样才能使条件起作用? 这是代码的sn-p:

Conditions:
  EbsVolumeTrue: !Equals [!Ref EBS, true]
Resources:
  Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-xxxxxx
      !If [EbsVolumeTrue, !Ref BlockDeviceMappings, !Ref "AWS::NoValue"]
      BlockDeviceMappings: 
       - DeviceName: !Ref VolumeName
         Ebs: 
           VolumeType: !Ref VolumeType
           DeleteOnTermination: false
           VolumeSize: !Ref VolumeSize

【问题讨论】:

  • 什么是BlockDeviceMappings
  • Condition Functions - AWS CloudFormation 页面显示了是否使用Condition 创建资源的示例,并显示了如何使用!If 选择值,但我认为您不能包含/exclude 参数以您显示的方式。您可能需要创建两个Instance 资源(一个有磁盘,一个没有),并为它们提供不同的条件,以便只创建一个。 (但我没有尝试过这样的事情。)

标签: amazon-ec2 amazon-cloudformation


【解决方案1】:

这是在条件中使用多行的方式:

Conditions:
  EbsVolumeTrue: !Equals [!Ref EBS, true]
Resources:
  Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-xxxxxx
      Fn::If:
        - EbsVolumeTrue
        - 
          BlockDeviceMappings: 
          - DeviceName: !Ref VolumeName
            Ebs: 
              VolumeType: !Ref VolumeType
              DeleteOnTermination: false
              VolumeSize: !Ref VolumeSize
        - Ref: AWS::NoValue

【讨论】:

    猜你喜欢
    • 2019-11-04
    • 2017-12-05
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 2019-11-18
    • 1970-01-01
    • 2016-01-04
    相关资源
    最近更新 更多