【问题标题】:AWS Cloudformation how to use conditional resource as DependsOn for other resourceAWS Cloudformation 如何将条件资源用作其他资源的 DependsOn
【发布时间】:2021-06-03 08:27:28
【问题描述】:

我正在尝试使用 IAMInstanceprofile 的可选值创建 EC2 实例。 当 createiam 为 False 时,我希望 ec2stack 在没有 iam 的情况下创建,当它为 True 时,它​​应该等待 iamstack 并使用它的值。

     "Parameters": {
        "createiam" : {
            "Type" : "String",
            "Default" : "False"
        }
     },

     "Conditions" : {
           "Create_iam" : {"Fn::Equals" : [{"Ref" : "createiam"}, "True"]}
       },

    "Resources" : {
        "iamstack" : {
            "Type" : "AWS::CloudFormation::Stack",
            "Condition": "Create_iam"
        },
        "ec2stack": {
            "Type" : "AWS::CloudFormation::Stack",
            "DependsOn" : "iamstack"
         }
      }

在为 crateiam 使用选项 False 运行堆栈时收到错误消息,提示未找到 iamstack 资源。有没有办法为 Dependson 值添加条件?

【问题讨论】:

  • 我认为它可以工作。您可以将“createiam”设置为参数。在您的示例中,您正在设置 ec2stack -> DependsOn -> iamstack -> Condition -> Create_iam (此处重复)。我不明白你的想法。
  • 这个想法很简单,如果我不想创建它应该忽略的 IAM 角色(iamstack -> Condition -> Create_iam),否则它应该等待(ec2stack -> DependsOn -> iamstack)要完成的 IAM 角色
  • 什么是Conditions?是输入参数吗?您能否提供完整的示例来展示您正在尝试做的事情?
  • @Marcin 已编辑特定类型的问题。

标签: amazon-web-services amazon-cloudformation


【解决方案1】:

有没有办法为 Dependson 值添加条件? -> 是的,有。

在这个例子中 (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html),它起作用了。

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  EnvType:
    Description: Environment type.
    Default: test
    Type: String
    AllowedValues:
      - prod
      - test
    ConstraintDescription: must specify prod or test.
Conditions:
  CreateProdResources: !Equals 
    - !Ref EnvType
    - prod
Resources:
  EC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: ami-05b891753d41ff88f #ami-0ff8a91507f77f867
  MountPoint:
    Type: 'AWS::EC2::VolumeAttachment'
    Condition: CreateProdResources
    Properties:
      InstanceId: !Ref EC2Instance
      VolumeId: !Ref NewVolume
      Device: /dev/sdh
  NewVolume:
    Type: 'AWS::EC2::Volume'
    Condition: CreateProdResources
    Properties:
      Size: 11
      AvailabilityZone: !GetAtt
        - EC2Instance
        - AvailabilityZone
  Bucket:
    Type: 'AWS::S3::Bucket'
    Condition: CreateProdResources
    DependsOn: MountPoint

在你的情况下,你需要分享你的完整代码。

【讨论】:

    【解决方案2】:

    我可以使用 WaitConditionHandle 来实现这一点。

    在这里找到参考资料。

    1. Stack overflow question
    2. Blog link with explination

    现在的代码流:

    
    "Parameters": {
            "createiam" : {
                "Type" : "String",
                "Default" : "False"
            }
         },
    
    "Conditions" : {
               "Create_iam" : {"Fn::Equals" : [{"Ref" : "createiam"}, "True"]}
           },
           
    "Resources" : {
        "IAMWaithandle": {
            "Condition": "Create_iam",
            "DependsOn": "iamstack",
            "Type": "AWS::CloudFormation::WaitConditionHandle"
        },
        "WaitHandle": {
            "Type": "AWS::CloudFormation::WaitConditionHandle"
        },
        "IAMWaitcondiftion": {
            "Type": "AWS::CloudFormation::WaitCondition",
            "Properties": {
                "Handle": {
                    "Fn::If": [
                        "Create_iam",
                        {
                            "Ref": "IAMWaithandle"
                        },
                        {
                            "Ref": "WaitHandle"
                        }
                    ]
                },
                "Timeout": "1",
                "Count": 0
            }
        },
        "iamstack" : {
            "Type" : "AWS::CloudFormation::Stack",
            "Condition": "Create_iam"
        },
        "ec2stack": {
            "Type" : "AWS::CloudFormation::Stack",
            "DependsOn" : "IAMWaitcondiftion"
         }
        }
    
    

    【讨论】:

      猜你喜欢
      • 2019-02-22
      • 2018-03-10
      • 2019-11-30
      • 1970-01-01
      • 2018-10-25
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      相关资源
      最近更新 更多