-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
130 lines (112 loc) · 4.27 KB
/
app.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
"""
CDK Application for managing containers in AWS
"""
import os
from aws_cdk import (
# Aspects,
App,
Environment,
Tags,
)
# import cdk_nag
from ContainerManager.base_stack import BaseStack
from ContainerManager.leaf_stack_group.domain_stack import DomainStack
from ContainerManager.leaf_stack_group.container_manager_stack import ContainerManagerStack
from ContainerManager.leaf_stack_group.start_system_stack import StartSystemStack
from ContainerManager.utils import load_base_config, load_leaf_config
# https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.App.html
app = App()
application_id = app.node.get_context("_application_id")
APPLICATION_ID_TAG_NAME = "ApplicationId"
### TODO: Finish going through all the cdk_nag checks:
# Aspects.of(app).add(cdk_nag.AwsSolutionsChecks(verbose=True))
Tags.of(app).add(APPLICATION_ID_TAG_NAME, application_id)
### Fact-check the maturity, and save it for leaf stacks:
# (Makefile defaults to prod if not set. We want to fail-fast
# here, so throw if it doesn't exist)
maturity = app.node.get_context("maturity")
supported_maturities = ["devel", "prod"]
assert maturity in supported_maturities, f"ERROR: Unknown maturity. Must be in {supported_maturities}"
# Lets you reference self.account and self.region in your CDK code
# if you need to:
main_env = Environment(
account=os.getenv('CDK_DEFAULT_ACCOUNT'),
region=os.getenv('CDK_DEFAULT_REGION'),
)
us_east_1_env = Environment(
account=main_env.account,
region="us-east-1",
)
##################
### Base Stack ###
##################
base_config = load_base_config("./base-stack-config.yaml")
### Create the Base Stack VPC for ALL leaf stacks:
base_stack = BaseStack(
app,
f"{app.node.get_context('_base_stack_name')}",
description="The base VPC for all other ContainerManage stacks to use.",
cross_region_references=True,
env=main_env,
config=base_config,
application_id_tag_name=APPLICATION_ID_TAG_NAME,
application_id_tag_value=application_id,
)
##################
### Leaf Stack ###
##################
### Create the application for ONE Container:
file_path = app.node.try_get_context("config-file")
if file_path:
leaf_config = load_leaf_config(file_path, maturity=maturity)
# You can override container_id if you need to:
container_id = app.node.try_get_context("container-id")
if not container_id:
container_id = os.path.basename(os.path.splitext(file_path)[0])
container_id = container_id.lower()
# For stack names, turn "minecraft.java.example" into "MinecraftJavaExample":
container_id_alpha = "".join(e for e in container_id.title() if e.isalpha()) # pylint: disable=invalid-name
stack_tags = {
"ContainerId": container_id,
"StackId": f"{application_id}-{container_id_alpha}",
}
### Create the Base Stack Domain for ALL leaf stacks:
domain_stack = DomainStack(
app,
f"{application_id}-{container_id_alpha}-Domain",
description="The base HostedZone for all other ContainerManage stacks to use.",
cross_region_references=True,
env=us_east_1_env,
container_id=container_id,
base_stack=base_stack,
)
for key, val in stack_tags.items():
Tags.of(domain_stack).add(key, val)
container_manager_stack = ContainerManagerStack(
app,
f"{application_id}-{container_id_alpha}-ContainerManager",
description="For automatically managing and spinning down the container.",
cross_region_references=True,
env=main_env,
base_stack=base_stack,
domain_stack=domain_stack,
application_id=application_id,
container_id=container_id,
config=leaf_config,
)
for key, val in stack_tags.items():
Tags.of(container_manager_stack).add(key, val)
start_system_stack = StartSystemStack(
app,
f"{application_id}-{container_id_alpha}-StartSystem",
description="Everything for spinning up the container when someone connects.",
cross_region_references=True,
env=us_east_1_env,
domain_stack=domain_stack,
container_manager_stack=container_manager_stack,
container_id=container_id,
)
for key, val in stack_tags.items():
Tags.of(start_system_stack).add(key, val)
app.synth()