Difference between an Output & an Export

Viewed 6066

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'?

2 Answers

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.

Chaining

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.

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. 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.

Nesting

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.

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. 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.

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

Cross-referencing

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.

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. 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.

When to use which?

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.

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.

Short answer from here, use export between stacks, and use output with nested stacks.

Export

To share information between stacks, export a stack's output values. Other stacks that are in the same AWS account and region can import the exported values.

Output

With nested stacks, you deploy and manage all resources from a single stack. You can use outputs from one stack in the nested stack group as inputs to another stack in the group. This differs from exporting values.

Related