CloudFormation Conditions Query

0

I have a CloudFormation template with me and I want to have a choice of whether to use Existing VPC,Subnet for creating an EC2instance using Input parameter. What I am looking for is something like this:- The template will have a parameter "UseExistingResources", if user selects 'true' and provide existing resources ID in other inputs like "ExistingVpcId" , "ExistingSubnetId" then it should use the existing ones. And if the user selects "false" and then if user left "ExistingVpcId" , "ExistingSubnetId" either empty or provide something then it should be ignored and the template should create new resources.

Can anyone please provide a condition statement to deal with this.

Thanks

질문됨 10달 전194회 조회
1개 답변
0

Hi.

First you need to create a Condition in the template:

AWSTemplateFormatVersion: "2010-09-09"
Parameters:
   UseExistingResources:
   Type: String
   Default: false
    AllowedValues:
      - true
      - false

Conditions:
  DoUseExistingResources: !Equals [!Ref UseExistingResources, true]

....

Then you need to create the Instance using the condition specify either the existing resources or the new, something like this.

EC2Instanace:
   Type: AWS::EC2::Instance
   Properties: 
      ......
      SubnetId:
         Fn::If:
            [DoUseExistingResources, !Ref ExistingSubnetID, !Ref NewCreatedSubnetId]

Hope that helps you.

profile picture
전문가
답변함 10달 전
  • Hi, Thanks for answering, Yeah this is what exactly I am doing. But I also have one requirement that If the user selects "UseExistingResources = False" then the subsequent input parameter which i am using for Asking Existing SubnetID,CVP in case user selects "UseExistingResources = True" should be left empty. Currently my template is working fine if I use "UseExistingResources = False" and provide any random values as input to the Existing resorces parameter. But I need to find a way in which user can left the subsequent input parameter empty if they chooses "UseExistingResources = False".

    If I leave the subsequent input parameter empty then I am getting error as follows: "Parameter validation failed: parameter value for parameter name ExistingSubnetId does not exist, parameter value for parameter name ExistingVpcId does not exist, parameter value for parameter name ExistingSecurityGroupId does not exist. Rollback requested by user."

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠