'AWS/Cloudformation: How to export/import parameter value to another stack (YAML)

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.



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

we cannot use import value here as cloudformation does not allow to use intrinsic values in the parameter. But there is an option of using SSM (AWS System Management parameter store ) parameters in AWS which allows us to use the parameter in stack B which is created in stack A

Please check the link below article from AWS knowledge center

https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-systems-manager-parameter/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Olli
Solution 2 crazyguyjk
Solution 3 Ramya Sinduri Kondepudy