-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
206 lines (168 loc) · 6.77 KB
/
Copy path__init__.py
File metadata and controls
206 lines (168 loc) · 6.77 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from aws_cdk import (
aws_sns as sns,
aws_sns_subscriptions as sns_subscriptions,
aws_lambda as lambda_,
aws_logs as logs,
aws_apigateway as api_gateway,
aws_lambda_event_sources as event_sources,
aws_dynamodb as dynamodb,
core
)
from dataclasses import dataclass
from typing import AnyStr
DEFAULT_LAMBDA_HANDLER = 'main.lambda_handler'
DEFAULT_LAMBDA_RUNTIME = lambda_.Runtime.PYTHON_3_8
DEFAULT_LAMBDA_LOG_RETENTION = logs.RetentionDays.ONE_WEEK
@dataclass
class MongoDBConfiguration:
uri: AnyStr
database: AnyStr
collection: AnyStr
max_page_size: AnyStr
@dataclass
class AccessKeysConfiguration:
geocoding: AnyStr
@dataclass
class ImportedAssetsConfiguration:
table_property: dynamodb.Table
class DataProcessingStack(core.Stack):
def __init__(self, scope: core.Construct, id_: str,
imported_assets_config: ImportedAssetsConfiguration,
mongodb_config: MongoDBConfiguration,
access_keys_config: AccessKeysConfiguration,
**kwargs):
super().__init__(scope, id_, **kwargs)
# LAMBDAS DEFINITIONS
lambda_dispatch_stream = lambda_.Function(
self,
'DispatchStream',
code=lambda_.AssetCode('stack/lambda/dispatch_stream/1.0.0/python/dispatch_stream'),
timeout=core.Duration.seconds(10),
description='',
function_name='DispatchStream',
reserved_concurrent_executions=10,
handler=DEFAULT_LAMBDA_HANDLER,
runtime=DEFAULT_LAMBDA_RUNTIME,
log_retention=DEFAULT_LAMBDA_LOG_RETENTION,
memory_size=128,
retry_attempts=0,
dead_letter_queue_enabled=False
)
lambda_geocode_property = lambda_.Function(
self,
'GeocodeProperty',
code=lambda_.AssetCode('stack/lambda/geocode_property/1.1.3/python/geocode_property'),
timeout=core.Duration.seconds(15),
description='',
function_name='GeocodeProperty',
reserved_concurrent_executions=10,
handler=DEFAULT_LAMBDA_HANDLER,
runtime=DEFAULT_LAMBDA_RUNTIME,
log_retention=DEFAULT_LAMBDA_LOG_RETENTION,
memory_size=128,
retry_attempts=0,
dead_letter_queue_enabled=True
)
lambda_fetch_properties = lambda_.Function(
self,
'FetchProperties',
code=lambda_.AssetCode('stack/lambda/fetch_properties/1.4.0/python/fetch_properties'),
timeout=core.Duration.seconds(10),
description='',
function_name='FetchProperties',
reserved_concurrent_executions=10,
handler=DEFAULT_LAMBDA_HANDLER,
runtime=DEFAULT_LAMBDA_RUNTIME,
log_retention=DEFAULT_LAMBDA_LOG_RETENTION,
memory_size=128,
retry_attempts=0,
dead_letter_queue_enabled=True
)
# LAYERS DEFINITIONS
layer_dispatch_stream = lambda_.LayerVersion(
self,
'DispatchStreamLibs',
code=lambda_.Code.from_asset('stack/lambda/dispatch_stream/1.0.0/'),
description='',
layer_version_name='DispatchStreamLibs',
compatible_runtimes=[DEFAULT_LAMBDA_RUNTIME]
)
layer_geocode_property = lambda_.LayerVersion(
self,
'GeocodePropertyLibs',
code=lambda_.Code.from_asset('stack/lambda/geocode_property/1.1.3/'),
description='',
layer_version_name='GeocodePropertyLibs',
compatible_runtimes=[DEFAULT_LAMBDA_RUNTIME]
)
layer_fetch_properties = lambda_.LayerVersion(
self,
'FetchPropertiesLibs',
code=lambda_.Code.from_asset('stack/lambda/fetch_properties/1.4.0/'),
description='',
layer_version_name='FetchPropertiesLibs',
compatible_runtimes=[DEFAULT_LAMBDA_RUNTIME]
)
# CLOUDWATCH RULES DEFINITIONS
# -
# SQS QUEUES DEFINITIONS
# -
# SNS TOPICS DEFINITIONS
topic_new_properties = sns.Topic(
self,
'NewProperties',
display_name='',
topic_name='NewProperties'
)
# API GATEWAYS
api_gateway_graphql = api_gateway.LambdaRestApi(
self,
'GraphQLApi',
handler=lambda_fetch_properties,
rest_api_name='GraphQLApi',
description='GraphQL API',
cloud_watch_role=True
)
api_gateway_graphql_resource = api_gateway_graphql.root.add_resource('graphql')
api_gateway_graphql_resource.add_method('GET', api_key_required=False)
api_gateway_graphql.add_usage_plan(
'GraphQLUsagePlan',
name='GraphQLUsagePlan',
throttle=api_gateway.ThrottleSettings(
rate_limit=1,
burst_limit=1
)
)
# DYNAMODB PERMISSIONS
lambda_dispatch_stream.add_event_source(event_sources.DynamoEventSource(
table=imported_assets_config.table_property,
starting_position=lambda_.StartingPosition.LATEST,
batch_size=10,
max_batching_window=core.Duration.seconds(30),
parallelization_factor=10,
retry_attempts=0
))
# CLOUDWATCH SCHEDULING RULES
# -
# SQS PERMISSIONS
# -
# SNS PERMISSIONS
topic_new_properties.grant_publish(lambda_dispatch_stream)
topic_new_properties.add_subscription(sns_subscriptions.LambdaSubscription(lambda_geocode_property))
# LAYERS ASSIGNMENTS
lambda_dispatch_stream.add_layers(layer_dispatch_stream)
lambda_geocode_property.add_layers(layer_geocode_property)
lambda_fetch_properties.add_layers(layer_fetch_properties)
# ENVIRONMENT VARIABLES
lambda_geocode_property.add_environment(key='MONGODB_URI', value=mongodb_config.uri)
lambda_geocode_property.add_environment(key='MONGODB_DATABASE', value=mongodb_config.database)
lambda_geocode_property.add_environment(key='MONGODB_COLLECTION', value=mongodb_config.collection)
lambda_geocode_property.add_environment(
key='API_ACCESS_TOKEN_GEOCODING', value=access_keys_config.geocoding
)
lambda_fetch_properties.add_environment(key='MONGODB_URI', value=mongodb_config.uri)
lambda_fetch_properties.add_environment(key='MONGODB_DATABASE', value=mongodb_config.database)
lambda_fetch_properties.add_environment(key='MONGODB_COLLECTION', value=mongodb_config.collection)
lambda_fetch_properties.add_environment(key='MONGODB_MAX_PAGE_SIZE', value=mongodb_config.max_page_size)
# EXPOSED ENTITIES
# -