【问题标题】:AWS/Cloudformation: How to export/import parameter value to another stack (YAML)AWS/Cloudformation:如何将参数值导出/导入到另一个堆栈 (YAML)
【发布时间】:2017-08-21 08:47:31
【问题描述】:

我有一个简单的问题。我正在测试云形成中值的导出/导入。

问题是:如何根据来自另一个堆栈的链接条件创建资源?

我认为我应该从其他堆栈导入值,但不知道如何......

这是我的“导出测试堆栈”

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

这是我的“导入测试堆栈”

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

这是关于跨堆栈引用,所以我想在“import-test-stack”中部署 Ec2Instance2,只有当我选择在之前的“export-test-stack”中部署三个实例时。如何做到这一点?

所以如果我选择部署三个实例,我想使用“import stack”中的条件来部署另外两个实例,如果我选择部署两个,它只会在“import-stack”中部署一个实例

我知道条件如何工作,但仍然无法找到方法,如何在交叉引用堆栈中使用。

我知道这是个愚蠢的例子,但我只是想在尽可能简单的模板上进行测试。

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    您有两个选择:继续使用单独的堆栈或将它们组合起来以创建 nested stack.

    使用嵌套堆栈,您可以将一个堆栈的输出用作另一个堆栈的输入。

    如果您想继续使用分离的堆栈,请使用Fn::ImportValue 函数导入从另一个堆栈导出的输出值。

    Exporting Stack Output Values 页面已经涵盖了这两个角度。此外,如果您选择使用 Fn::ImportValuecross-stack reference walkthrough 可能会对您有所帮助。

    【讨论】:

    • Fn::ImportValue 对我来说很有意义,但是我仍然没有找到如何导入条件,因为条件必须只是一个简单的字符串对吧?或者您能否提供一个简单的示例,如何导出条件(可能通过输出?)并将该条件导入其他堆栈?
    【解决方案2】:

    这将使您导入正确的值: Fn::ImportValue: EC2Conditions

    您也可以使用规则。您可以根据输出的值制定规则。

    【讨论】:

      猜你喜欢
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2020-04-03
      • 2020-04-12
      • 1970-01-01
      相关资源
      最近更新 更多