forked from DualSpark/cloudformation-environmentbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnested_child_stack.py
66 lines (53 loc) · 2.5 KB
/
nested_child_stack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from environmentbase.networkbase import NetworkBase
from environmentbase.template import Template
from environmentbase.environmentbase import EnvConfig
from environmentbase.patterns.bastion import Bastion
from troposphere import ec2
class Controller(NetworkBase):
"""
Class creates a VPC and common network components for the environment
"""
def create_hook(self):
self.add_child_template(ChildTemplate('Child'))
self.add_child_template(Bastion('Bastion'))
class ChildTemplate(Template):
"""
Class creates a VPC and common network components for the environment
"""
# Called from add_child_template() after some common parameters are attached to this instance, see docs for details
def build_hook(self):
self.add_resource(ec2.Instance(
"ChildEC2",
InstanceType="m3.medium",
ImageId="ami-e7527ed7",
KeyName=self.ec2_key,
SubnetId=self.subnets['private'][0],
SecurityGroupIds=[self.common_security_group]
))
self.add_child_template(GrandchildTemplate('Grandchild'))
# When no config.json file exists a new one is created using the 'factory default' file. This function
# augments the factory default before it is written to file with the config values required
@staticmethod
def get_factory_defaults():
return {'my_child_template': {'favorite_color': 'blue'}}
# When the user request to 'create' a new template the config.json file is read in. This file is checked to
# ensure all required values are present. Because MyChildTemplate has additional requirements beyond that of
# EnvironmentBase this function is used to add additional validation checks.
@staticmethod
def get_config_schema():
return {'my_child_template': {'favorite_color': 'str'}}
class GrandchildTemplate(Template):
def build_hook(self):
self.add_resource(ec2.Instance(
"GrandchildEC2",
InstanceType="m3.medium",
ImageId="ami-e7527ed7",
KeyName=self.ec2_key,
SubnetId=self.subnets['private'][0],
SecurityGroupIds=[self.common_security_group]))
if __name__ == '__main__':
# EnvConfig holds references to handler classes used to extend certain functionality
# of EnvironmentBase. The config_handlers list takes any class that implements
# get_factory_defaults() and get_config_schema().
env_config = EnvConfig(config_handlers=[ChildTemplate])
Controller(env_config=env_config)