1

Suppose I'm defined two stacks, A and B.

In stack A, I define a VPC and a subnet and I output that subnet's id

t = Template()
Subnet = t.add_resource(Subnet .....)
SubnetId = t.add_output(Output('SubnetId', Value=Ref(Subnet))

In Stack B, I have these instances that I want to live within the subnet defined in Stack A. How can I achieve that?

mingxiao
  • 1,712
  • 4
  • 21
  • 33

2 Answers2

1

You'll need to pass them in to Stack B as Parameters. A decent example of this is in stacker, an open source project I maintain (I also help w/ troposphere).

The bastion.py blueprint takes many Parameters that come from the vpc.py blueprint (both found here - sorry, I tried to post links to each of the blueprints, but Stackoverflow won't let me post more than 2 links.). Stacker handles passing parameters from one stack to another for you, which is the biggest reason I wrote it.

Let me know if you have any questions.

phobologic
  • 424
  • 3
  • 5
  • cool library, but I'd prefer not to use another library. Could you provide a barebone example? – mingxiao Jul 17 '15 at 19:48
  • I see that vpc.py has output 'VpcId' and bastion.py has that in its PARAMETERS, but its not apparent to me how that is passed. – mingxiao Jul 17 '15 at 20:00
  • Well, that's part of the magic that stacker provides. If you want to just use troposphere, then you need to find a way to do what you want in CloudFormation or write code (like stacker) to take the outputs from one stack and add them to the parameters of another. Remember: troposphere is just a python implementation of the Cloudformation template language. One other option, though I'm not a huge fan of it, is to use substacks. Then you can Ref() the outputs of one substack in the Parameters of another substack all in the master stack where they are defined. That's tricky though. – phobologic Jul 18 '15 at 01:57
  • Basically Cloudformation doesn't provide any method to reference a different stack in your Parameters, unless you are using substacks - so troposphere is the same. – phobologic Jul 18 '15 at 01:58
  • Ok, I was able to use substacks and pass the output from one as a parameter to another. – mingxiao Jul 23 '15 at 17:37
  • stacker looks like a cool solution. So far I could only find the blueprints... I would like to see a complete example assembling a stack from multiple blueprints, extracting the CF template etc. – moin moin Mar 15 '17 at 19:44
  • @mark we're actually working on putting up a series of tutorial blog posts. You can check out the first, which is a sort of introduction at http://engineering.remind.com/introduction-to-stacker/ Hoping ot have the next sometime this week/early next. – phobologic Mar 16 '17 at 20:55
  • 1
    @mingxiao, would you be able to post your example of substack parameters as an answer? – hsteckylf Jul 11 '17 at 04:47
1

You can use troposphere.GetAtt. Here's an example where I'm passing VpcId and PublicSubnetId from my VPC stack to my Apache Airflow stack:

# t is a Template, keyname_param is a Parameter
vpc_stack = t.add_resource(
    Stack(
        'VpcStack',
        TemplateURL=vpc_template_url
    )
)
airflow_stack = t.add_resource(
    Stack(
        'AirflowStack',
        TemplateURL=airflow_stack_url,
        Parameters={
            'KeyName': Ref(keyname_param),
            'VpcId': GetAtt(vpc_stack.title, 'Outputs.VpcId'),
            'PublicSubnetId': GetAtt(vpc_stack.title, 'Outputs.PublicSubnetId')
        }
    )
)
lfk
  • 2,423
  • 6
  • 29
  • 46