输出和输出之间的区别出口 [英] Difference between an Output & an Export

查看:76
本文介绍了输出和输出之间的区别出口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 CloudFormation 中,我们能够从模板中输出一些值,以便其他进程、堆栈等可以检索它们.这通常是某物的名称,可能是 URL 或堆栈创建(部署)期间生成的某物等

In CloudFormation we have the ability to output some values from a template so that they can be retrieved by other processes, stacks, etc. This is typically the name of something, maybe a URL or something generated during stack creation (deployment), etc.

我们还可以从模板导出".将值作为输出"返回与作为导出"返回有什么区别?

We also have the ability to 'export' from a template. What is the difference between returning a value as an 'output' vs as an 'export'?

推荐答案

常规输出值不能被其他堆栈引用.当您链接嵌套您的堆栈并且它们的范围/可见性是本地的时,它们会很有用.导出的输出在帐户和区域内全局可见,并且可供您将来要部署的任何堆栈使用.

Regular output values can't be references from other stacks. They can be useful when you chain or nest your stacks and their scope/visibility is local. Exported outputs are visible globally within account and region, and can be used by any future stack you are going to deploy.

链接

当您链接堆栈时,您部署一个堆栈,将其输出,并将其用作您要部署的第二个堆栈的输入参数.

When you chain your stacks, you deploy one stack, take it outputs, and use as input parameters to the second stack you are going to deploy.

例如,假设您有两个模板,名为 instance.yamleip.yaml.instance.yaml 输出其实例 ID(不导出),而 eip.yaml 将实例 ID 作为输入参数.

For example, let's say you have two templates called instance.yaml and eip.yaml. The instance.yaml outputs its instance-id (no export), while eip.yaml takes instance id as an input parameter.

要同时部署它们,您必须将它们链接起来:

To deploy them both, you have to chain them:

  1. 部署 instance.yaml 并等待其完成.
  2. 注意它输出值(即实例 ID) - 通常以编程方式完成,而不是手动完成.
  3. 部署 eip.yaml 并传递 instance-id 作为其输入参数.
  1. Deploy instance.yaml and wait for its completion.
  2. Note it outputs values (i.e. instance-id) - usually done programmatically, not manually.
  3. Deploy eip.yaml and pass instance-id as its input parameter.

嵌套

当您嵌套堆栈时,您将拥有一个父模板和一个子模板.将从父堆栈内部创建子堆栈.在这种情况下,子堆栈将产生一些输出(不是导出)供父堆栈使用.

When you nest stacks you will have a parent template and a child template. Child stack will be created from inside of the parent stack. In this case the child stack will produce some outputs (not exports) for the parent stack to use.

例如,让我们再次使用 instance.yamleip.yaml.但是这次 eip.yaml 将是父级,而 instance.yaml 将是子级.另外 eip.yaml 不接受任何输入参数,但 instance.yaml 输出其实例 ID(非导出)

For example, lets use again instance.yaml and eip.yaml. But this time eip.yaml will be parent and instance.yaml will be child. Also eip.yaml does not take any input parameters, but instance.yaml outputs its instance-id (not export)

在这种情况下,要部署它们,请执行以下操作:

In this case, to deploy them you do the following:

  1. 上传父模板 (eip.yaml) 到 s3
  2. eip.yaml 中,使用 AWS::CloudFormation::Stack 和步骤 1 中的 s3 网址.
  1. Upload parrent template (eip.yaml) to s3
  2. In eip.yaml create the child instance stack using AWS::CloudFormation::Stack and the s3 url from step 1.

这样 eip.yaml 将能够使用 GetAtt 从嵌套堆栈的输出中访问实例 ID.

This way eip.yaml will be able to access the instance-id from the outputs of the nested stack using GetAtt.

交叉引用

当您交叉引用堆栈时,您有一个堆栈可以导出它的输出,以便它们可以被同一区域和帐户中的任何其他堆栈使用.

When you cross-reference stacks, you have one stack that exports it outputs so that they can be used by any other stack in the same region and account.

例如,让我们再次使用 instance.yamleip.yaml.instance.yaml 将导出其输出(instance-id).要使用实例 ID eip.yaml 必须使用 ImportValue 在其模板中,无需任何输入参数或嵌套堆栈.

For example, lets use again instance.yaml and eip.yaml. instance.yaml is going to export its output (instance-id). To use the instance-id eip.yaml will have to use ImportValue in its template without the need for any input parameters or nested stacks.

在这种情况下,要部署它们,请执行以下操作:

In this case, to deploy them you do the following:

  1. 部署 instance.yaml 并等待它完成.
  2. 部署 eip.yaml,它将导入实例 ID.
  1. Deploy instance.yaml and wait till it completes.
  2. Deploy eip.yaml which will import the instance-id.

尽管交叉引用看起来非常有用,但它有一个主要问题,即很难更新或删除交叉引用的堆栈:

Altough cross-referencing seems very useful, it has one major issue, which is that its very difficult to update or delete cross-referenced stacks:

另一个堆栈导入输出值后,您不能删除正在导出输出值的堆栈或修改导出的输出值.必须先删除所有导入,然后才能删除导出堆栈或修改输出值.

After another stack imports an output value, you can't delete the stack that is exporting the output value or modify the exported output value. All of the imports must be removed before you can delete the exporting stack or modify the output value.

如果您正在开始设计并且您的模板可能经常更改,这将是非常有问题的.

This is very problematic if you are starting your design and your templates can change often.

何时使用哪个?

当您有一些全局资源将在给定区域和帐户中的许多堆栈之间共享时,请使用交叉引用(导出值).此外,它们不应该经常更改,因为它们很难修改.常见示例有:用于集中日志记录位置的全局存储区、VPC.

Use cross-references (exported values) when you have some global resources that are going to be shared among many stacks in a given region and account. Also they should not change often as they are difficult to modify. Common examples are: a global bucket for centralized logging location, a VPC.

当您有一些经常部署的通用组件时,请使用嵌套堆栈(而不是导出的输出),但每次都可能有所不同.例如:ALB、堡垒主机实例、vpc 接口端点.

Use nested stack (not exported outputs) when you have some common components that you often deploy, but each time they can be a bit different. Examples are: ALB, a bastion host instance, vpc interface endpoint.

最后,链式堆栈(不导出输出)对于设计松耦合模板很有用,您可以在其中根据新需求混合和匹配模板.

Finally, chained stacks (not exported outputs) are useful for designing loosely-coupled templates, where you can mix and match templates based on new requirements.

这篇关于输出和输出之间的区别出口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆