How to obtain outputs from another Troposphere stack?

1,177 views
Skip to first unread message

HacksawJim

unread,
Sep 5, 2018, 12:55:50 PM9/5/18
to cloudtools-dev
Hello,

I have two cfn stacks a and b. a sets up many things in the VPC. I have a few "Outputs" from this stack. How do I pull them into the new separate stack b (both Troposphere defined)? I wasn't able to find any examples. I thinking ImportValue() in __init__.py is intended for this in some way?

I would think it's supposed to be something like ImportValue(stack_name, output_name), but I didn't see any parameters like this?

Thanks for any insights. I know how to pull these using the AWS CLI, but I think there is probably a more Troposphere-ic way?

Thanks again!

Marcin Romaszewicz

unread,
Sep 5, 2018, 1:06:26 PM9/5/18
to jason...@gmail.com, cloudto...@googlegroups.com
Use xrefs or rxrefs.

For example, if stack A defines a VPC named "MyVpc" which exports "SubnetId", in Stack B, you can do something like this

{rxref MyVpc::SubnetId}
of
{ref namespace-MyVpc::SubnetId}

The r/xref differ in how they resolve namespaces. You have to fully qualify a stack name with {xref} or you can use relative references with {rxref} if it's in the namespace.

stacks_a.yaml:
namespace: foo
stacks:
  - name: MyVpc

stacks_b.yaml
namespace:foo
stacks:
  - name: SomeResource
  - Subnet: {rxref MyVpc::SubnetId}
  - OtherSubnet: {xref foo-MyVpc::OtherSubnetId}

--
You received this message because you are subscribed to the Google Groups "cloudtools-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloudtools-de...@googlegroups.com.
To post to this group, send email to cloudto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cloudtools-dev/e9a89138-dd3b-4dbd-a26b-fb2e9ed813a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Barrett

unread,
Sep 5, 2018, 1:09:30 PM9/5/18
to Marcin Romaszewicz, jason...@gmail.com, cloudto...@googlegroups.com
Note: That's for doing it in stacker (https://stacker.readthedocs.io/en/latest/), which uses troposphere as it's templating language (primarily).

If you want to do it with just troposphere/the aws cli, you'll want to use ImportValue: https://github.com/cloudtools/troposphere/blob/master/troposphere/__init__.py#L517 so you'll need to understand how that works: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-importvalue.html


Marcin Romaszewicz

unread,
Sep 5, 2018, 1:16:07 PM9/5/18
to lok...@gmail.com, jason...@gmail.com, cloudto...@googlegroups.com
Ha! Epic mis-read, sorry about that. I conflated the two in my head.

HacksawJim

unread,
Sep 5, 2018, 2:04:17 PM9/5/18
to cloudtools-dev
Thanks gents!!!

1. I didn't define Export names for the Outputs; I somehow glossed over the parameter.
2. I forgot that you don't have to specify a stack when importing, because Export names for Outputs are unique within AWS account + region.

For anyone else's reference:

In stack a:

from troposphere import Export, Output

t
.add_output(
   
Output(
       
'snetid', Value=Ref(snet), Export=Export('{}-snetid'.format(stack))))
t
.add_output(
   
Output('sgid', Value=Ref(sg), Export=Export('{}-sgid'.format(stack))))


In stack b:

from troposphere import ImportValue


snetid
= ImportValue('{}-vpc-snetid'.format(stack))
sgid
= ImportValue('{}-vpc-sgid'.format(stack))



And of course you can use functions like Sub() if needed to replace part of the value (parameterized values) on output (export) and import.

HacksawJim

unread,
Sep 5, 2018, 2:13:51 PM9/5/18
to cloudtools-dev
I had a typo, plus here is simpler code for immediate clarity for anyone who might search and find this later.

In stack a:

from troposphere import Export, Output



t
.add_output(Output('snetid', Value=Ref(snet), Export=Export('vpcstack-snetid')) # you can get the stack name from CLI or template param as well, better than hard-coding it
t
.add_output(Output('sgid', Value=Ref(sg), Export=Export('vpcstack-sgid'))

In stack b:

from troposphere import ImportValue


snetid
= ImportValue('vpcstack-snetid')
sgid
= ImportValue('vpcstack-sgid')
Reply all
Reply to author
Forward
0 new messages