-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
85 lines (73 loc) · 3.61 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
#!/usr/bin/service_env python3
import copy
from typing import Dict, Any
import aws_cdk as cdk
from dotenv import dotenv_values
from cdk_fast_api_jwt.cdk_fast_api_jwt_stack import CdkFastApiJwtStack, StackConfig
class MultistackApp:
# Name for our production stack. Staging/dev/other stacks will prepend a prefix:
STACK_NAME_BASE = "CdkFastApiJwtStack"
"""
Base stack configuration. The actual stack stack_config used to build
the stack may add prefixes, and additional runtime environment variables.
If additional services or configuration is needed, add it here, and make
use of it in the stack itself. For example, our environment for the
lambda function and the fargate service. If we end up needing different
environments instead, we'd add the second one here, then reference that one
when creating the associated service in the stack code.
"""
STACK_CONFIG_BASE = StackConfig(
service_env=dotenv_values(".env"),
fargate_service_name="tapestryworlds-service",
lambda_fn_name="lambda-command-lambda_fn",
sqs_queue_name="tapestryworlds-sqs-sqs_queue.fifo",
s3_bucket_name="tapestryworlds-s3-images-bucket",
)
def __init__(self):
""" Constructor for our multistack app """
self.cdk_app = cdk.App()
def create_stacks(self):
"""
Called to create the stacks for this app. The CDK CLI will allow
the user to create one, several, or all stacks available:
"""
stack_env = dict()
self.__create_stack(environment='production', stack_env=stack_env)
self.__create_stack(environment='staging', stack_env=stack_env)
self.__create_stack(environment='test', stack_env=stack_env)
def __create_stack(self, environment: str, stack_env: Dict[str, Any]):
"""
Create a specific stack based on given environment name @public
:param environment: will be used as a prefix (delimited by a dash)
for the stack name. It will also be used as a prefix for the name
values in `STACK_CONFIG_BASE`. Note: in the case where environment
is 'production', there will be no prefix applied.
`service_env.APP_ENV` will also be set to `environment`, so the code
running in the various AWS services can easily determine which under
which environment its stack is running.
:param stack_env: Environment variables to be used when building the stack
on AWS. This is useful if you want to deploy with a different account
and/or to a different region. Example keys: `account`,
`region`.
"""
stack_config = copy.deepcopy(self.STACK_CONFIG_BASE)
if environment == 'production':
stack_name = self.STACK_NAME_BASE
else:
stack_name = f"{environment.capitalize()}{self.STACK_NAME_BASE}"
stack_config.service_env.APP_ENV = environment
stack_config.fargate_service_name = f"{environment}-{stack_config.fargate_service_name}"
stack_config.lambda_fn_name = f"{environment}-{stack_config.lambda_fn_name}"
stack_config.sqs_queue_name = f"{environment}-{stack_config.sqs_queue_name}"
stack_config.s3_bucket_name = f"{environment}-{stack_config.s3_bucket_name}"
CdkFastApiJwtStack(
scope=self.cdk_app,
construct_id=stack_name,
stack_config=stack_config,
stack_name=stack_name,
env=stack_env,
)
# Create instance of our app; then create the stacks:
app = MultistackApp()
app.create_stacks()
app.cdk_app.synth()