2
votes

I have a simple question. I am testing export/import of values in cloud formation.

Question is: How to create resources based on linked conditions from another stack?

I think I should import the value from other stack, but don't know how....

This is my "export-test-stack"

AWSTemplateFormatVersion: '2010-09-09'

Description: Export

Parameters:
  EnvType: 
    Description: How many Instances you want to deploy?
    Default: two
    Type: String
    AllowedValues: 
      - two
      - three
    ConstraintDescription: must specify number of deployed Instances

Conditions: 
  Deploy3EC2: !Equals [ !Ref EnvType, three ]
       
Resources:
  Ec2Instance1:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      SecurityGroupIds:
      - sg-5d011027
      ImageId: ami-0b33d91d
  Ec2Instance2:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro  
      SecurityGroupIds:
      - sg-5d011027
      ImageId: ami-0b33d91d
  Ec2Instance3:
    Type: AWS::EC2::Instance
    Condition: Deploy3EC2
    Properties:
      InstanceType: t2.micro
      SecurityGroupIds:
      - sg-5d011027
      ImageId: ami-0b33d91d

      
Outputs:
  EC2Conditions:
    Description: Number of deployed instances
    Value: !Ref EnvType
    Export:
      Name: blablabla

This is my "import-test-stack"

AWSTemplateFormatVersion: '2010-09-09'

Description: Import
        
Resources:
  Ec2Instance1:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      SecurityGroupIds:
      - sg-7309dd0a
      ImageId: ami-70edb016
  Ec2Instance2:
    Type: AWS::EC2::Instance
    Condition: ??????  <<<<<<<<<
    Properties:
      InstanceType: t2.micro  
      SecurityGroupIds:
      - sg-7309dd0a
      ImageId: ami-70edb016

It's about cross stack reference, so I want to deploy Ec2Instance2 in "import-test-stack" only if I choose to deploy three Instances in previous "export-test-stack". How to do this?

So if I choose to deploy three instances, I want to use condition in "import stack" to deploy another two instances, if I choose to deploy two, it will deploy only one instance in "import-stack"

I know how conditions working, but still not able to find the way, how to use in cross reference stacks.

I know it's stupid example, but I just wanted to test that on as simple template as possible.

2

2 Answers

0
votes

You have two choices: continue with separated stacks or combine them to create a nested stack.

With nested stacks you can use outputs from one stack as inputs to another stack.

If you want to keep using separated stacks use Fn::ImportValue function to import output values exported from another stack.

The both angles have been covered in Exporting Stack Output Values page. Also, the cross-stack reference walkthrough might help you if you choose to use Fn::ImportValue.

0
votes

This will get you to import the correct value: Fn::ImportValue: EC2Conditions

You can also use rules. You can make the rule be based on the value of your output.