-
Notifications
You must be signed in to change notification settings - Fork 3
/
configuration.py
168 lines (141 loc) · 5.84 KB
/
configuration.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import re
# Environments (targeted at accounts)
DEPLOYMENT = 'Deployment'
DEV = 'Dev'
TEST = 'Test'
PROD = 'Prod'
# The following constants are used to map to parameter/secret paths
ENVIRONMENT = 'environment'
# Manual Inputs
GITHUB_REPOSITORY_OWNER_NAME = 'Last-Mile-Health'
GITHUB_REPOSITORY_NAME = 'lmd-aws-cdk-pipelines-datalake-infrastructure'
ACCOUNT_ID = '829553079673'
REGION = 'us-east-1'
LOGICAL_ID_PREFIX = 'LMDCDKDataLake'
RESOURCE_NAME_PREFIX = 'lmd-v2'
VPC_CIDR = '10.20.0.0/24'
# Secrets Manager Inputs
GITHUB_TOKEN = 'github_token'
# Used in Automated Outputs
VPC_ID = 'vpc_id'
AVAILABILITY_ZONE_1 = 'availability_zone_1'
AVAILABILITY_ZONE_2 = 'availability_zone_2'
AVAILABILITY_ZONE_3 = 'availability_zone_3'
SUBNET_ID_1 = 'subnet_id_1'
SUBNET_ID_2 = 'subnet_id_2'
SUBNET_ID_3 = 'subnet_id_3'
ROUTE_TABLE_1 = 'route_table_1'
ROUTE_TABLE_2 = 'route_table_2'
ROUTE_TABLE_3 = 'route_table_3'
SHARED_SECURITY_GROUP_ID = 'shared_security_group_id'
S3_KMS_KEY = 's3_kms_key'
S3_ACCESS_LOG_BUCKET = 's3_access_log_bucket'
S3_RAW_BUCKET = 's3_raw_bucket'
S3_CONFORMED_BUCKET = 's3_staging_bucket'
S3_PURPOSE_BUILT_BUCKET = 's3_curated_bucket'
REDSHIFT_DEFAULT_USER = "master"
REDSHIFT_DEFAULT_DATABASE = "liberia"
def get_local_configuration(environment: str) -> dict:
"""
Provides manually configured variables that are validated for quality and safety.
@param: environment str: The environment used to retrieve corresponding configuration
@raises: Exception: Throws an exception if the resource_name_prefix does not conform
@raises: Exception: Throws an exception if the requested environment does not exist
@returns: dict:
"""
local_mapping = {
DEPLOYMENT: {
ACCOUNT_ID: '829553079673',
REGION: 'us-east-1',
GITHUB_REPOSITORY_OWNER_NAME: 'Last-Mile-Health',
# If you use GitHub / GitHub Enterprise, this will be the organization name
GITHUB_REPOSITORY_NAME: 'lmd-aws-cdk-pipelines-datalake-infrastructure',
# Use your forked repo here!
# This is used in the Logical Id of CloudFormation resources
# We recommend capital case for consistency. e.g. DataLakeCdkBlog
LOGICAL_ID_PREFIX: 'LMDCDKDataLake',
# This is used in resources that must be globally unique!
# It may only contain alphanumeric characters, hyphens, and cannot contain trailing hyphens
# E.g. unique-identifier-data-lake
RESOURCE_NAME_PREFIX: 'lmd-v2',
},
DEV: {
ACCOUNT_ID: '002190277880',
REGION: 'us-east-1',
VPC_CIDR: '10.20.0.0/24'
},
TEST: {
ACCOUNT_ID: '576140831944',
REGION: 'us-east-1',
VPC_CIDR: '10.10.0.0/24'
},
PROD: {
ACCOUNT_ID: '301323023124',
REGION: 'us-east-1',
VPC_CIDR: '10.0.0.0/24'
}
}
resource_prefix = local_mapping[DEPLOYMENT][RESOURCE_NAME_PREFIX]
if (
not re.fullmatch('^[a-z|0-9|-]+', resource_prefix)
or '-' in resource_prefix[-1:] or '-' in resource_prefix[1]
):
raise Exception('Resource names may only contain lowercase Alphanumeric and hyphens '
'and cannot contain leading or trailing hyphens')
if environment not in local_mapping:
raise Exception(f'The requested environment: {environment} does not exist in local mappings')
return local_mapping[environment]
def get_environment_configuration(environment: str) -> dict:
"""
Provides all configuration values for the given target environment
@param environment str: The environment used to retrieve corresponding configuration
@return: dict:
"""
cloudformation_output_mapping = {
ENVIRONMENT: environment,
VPC_ID: f'{environment}VpcId',
AVAILABILITY_ZONE_1: f'{environment}AvailabilityZone1',
AVAILABILITY_ZONE_2: f'{environment}AvailabilityZone2',
AVAILABILITY_ZONE_3: f'{environment}AvailabilityZone3',
SUBNET_ID_1: f'{environment}SubnetId1',
SUBNET_ID_2: f'{environment}SubnetId2',
SUBNET_ID_3: f'{environment}SubnetId3',
ROUTE_TABLE_1: f'{environment}RouteTable1',
ROUTE_TABLE_2: f'{environment}RouteTable2',
ROUTE_TABLE_3: f'{environment}RouteTable3',
SHARED_SECURITY_GROUP_ID: f'{environment}SharedSecurityGroupId',
S3_KMS_KEY: f'{environment}S3KmsKeyArn',
S3_ACCESS_LOG_BUCKET: f'{environment}S3AccessLogBucket',
S3_RAW_BUCKET: f'{environment}RawBucketName',
S3_CONFORMED_BUCKET: f'{environment}StagingBucketName',
S3_PURPOSE_BUILT_BUCKET: f'{environment}CuratedBucketName',
}
return {**cloudformation_output_mapping, **get_local_configuration(environment)}
def get_all_configurations() -> dict:
"""
Returns a dict mapping of configurations for all environments.
These keys correspond to static values, CloudFormation outputs, and Secrets Manager (passwords only) records.
@return: dict:
"""
return {
DEPLOYMENT: {
ENVIRONMENT: DEPLOYMENT,
GITHUB_TOKEN: '/DataLake/GitHubToken',
**get_local_configuration(DEPLOYMENT),
},
DEV: get_environment_configuration(DEV),
TEST: get_environment_configuration(TEST),
PROD: get_environment_configuration(PROD),
}
def get_logical_id_prefix() -> str:
"""Returns the logical id prefix to apply to all CloudFormation resources
@return: str:
"""
return get_local_configuration(DEPLOYMENT)[LOGICAL_ID_PREFIX]
def get_resource_name_prefix() -> str:
"""Returns the resource name prefix to apply to all resources names
@return: str:
"""
return get_local_configuration(DEPLOYMENT)[RESOURCE_NAME_PREFIX]