0

I am trying to create EC2 instance with an optional value for IAMInstanceprofile. When createiam is False I am expecting ec2stack to create without iam, when its True it should wait for iamstack and use its value.

     "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"
         }
      }

While running stack with option False for crateiam getting an error saying iamstack resource not found. Is there a way to add a condition for Dependson value?

SNR
  • 460
  • 5
  • 20
  • I think it can work. You can set "createiam" as Parameters. In your example, you are setting ec2stack -> DependsOn -> iamstack -> Condition -> Create_iam (duplicated here). I don't get your idea. – Franxi Hidro Mar 04 '21 at 17:20
  • The idea is simple if I don't want to create an IAM role it should ignore(iamstack -> Condition -> Create_iam) otherwise it should wait(ec2stack -> DependsOn -> iamstack ) for the IAM role to complete – SNR Mar 04 '21 at 18:11
  • What is `Conditions`? Is it input parameter? Can you provide complete example demonstrating what you are trying to do? – Marcin Mar 05 '21 at 00:05
  • @Marcin Edited question with specific types. – SNR Mar 09 '21 at 06:50

2 Answers2

1

I am able to achieve this with the WaitConditionHandle.

Found the references here.

  1. Stack overflow question
  2. Blog link with explination

Code flow now:


"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"
     }
    }

SNR
  • 460
  • 5
  • 20
0

Is there a way to add a condition for Dependson value? -> Yes, there is.

In this example (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html), It worked.

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

In your case, you need to share your complete code.

Franxi Hidro
  • 505
  • 4
  • 18