简体   繁体   中英

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.

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 .

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM