Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
139 lines (121 loc) · 4.78 KB
/
Copy pathapp.py
File metadata and controls
139 lines (121 loc) · 4.78 KB
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
131
132
133
134
135
136
137
138
139
#!/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, Maturity
# https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.App.html
app = App()
application_id = app.node.try_get_context("_application_id")
if not application_id:
raise ValueError("Use the Makefile commands when working with this project!")
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")
maturity = Maturity(maturity)
maturity_description = "" if maturity == Maturity.PROD else f"(maturity={maturity.value}) "
Tags.of(app).add("Maturity", maturity.value)
# 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=f"{maturity_description}The base stack for all ContainerManager leaf 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")
# Only default the container-id if you have a file-name to derive it from:
if not container_id:
if file_path.startswith(("http", "https")):
raise ValueError("If you set config-file to a URL, you must also use 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.isalnum()) #pylint: disable=invalid-name
stack_tags = {
"ContainerId": container_id,
"StackId": f"{application_id}-{container_id_alpha}",
"FilePath": file_path,
}
### Create the Base Stack Domain for ALL leaf stacks:
domain_stack = DomainStack(
app,
f"{application_id}-{container_id_alpha}-Domain",
description=f"{maturity_description}The HostedZone for '{container_id}'.",
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,
# No "Sub-Id", it's the main stack:
f"{application_id}-{container_id_alpha}",
description=f"{maturity_description}For managing, and automatically 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=f"{maturity_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,
config=leaf_config,
)
for key, val in stack_tags.items():
Tags.of(start_system_stack).add(key, val)
app.synth()