|
| 1 | +from aws_cdk import ( |
| 2 | + aws_sns as sns, |
| 3 | + aws_sns_subscriptions as sns_subscriptions, |
| 4 | + aws_lambda as lambda_, |
| 5 | + aws_logs as logs, |
| 6 | + aws_apigateway as api_gateway, |
| 7 | + aws_lambda_event_sources as event_sources, |
| 8 | + aws_dynamodb as dynamodb, |
| 9 | + core |
| 10 | +) |
| 11 | +from dataclasses import dataclass |
| 12 | +from typing import AnyStr |
| 13 | + |
| 14 | + |
| 15 | +DEFAULT_LAMBDA_HANDLER = 'main.lambda_handler' |
| 16 | +DEFAULT_LAMBDA_RUNTIME = lambda_.Runtime.PYTHON_3_8 |
| 17 | +DEFAULT_LAMBDA_LOG_RETENTION = logs.RetentionDays.ONE_WEEK |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class MongoDBConfiguration: |
| 22 | + |
| 23 | + uri: AnyStr |
| 24 | + max_page_size: AnyStr |
| 25 | + |
| 26 | + |
| 27 | +@dataclass |
| 28 | +class AccessKeysConfiguration: |
| 29 | + |
| 30 | + geocoding: AnyStr |
| 31 | + |
| 32 | + |
| 33 | +@dataclass |
| 34 | +class ImportedAssetsConfiguration: |
| 35 | + |
| 36 | + table_property: dynamodb.Table |
| 37 | + |
| 38 | + |
| 39 | +class DataProcessingStack(core.Stack): |
| 40 | + |
| 41 | + def __init__(self, scope: core.Construct, id_: str, |
| 42 | + imported_assets_config: ImportedAssetsConfiguration, |
| 43 | + mongodb_config: MongoDBConfiguration, |
| 44 | + access_keys_config: AccessKeysConfiguration, |
| 45 | + **kwargs): |
| 46 | + |
| 47 | + super().__init__(scope, id_, **kwargs) |
| 48 | + |
| 49 | + # LAMBDAS DEFINITIONS |
| 50 | + |
| 51 | + lambda_dispatch_stream = lambda_.Function( |
| 52 | + self, |
| 53 | + 'DispatchStream', |
| 54 | + code=lambda_.AssetCode('stack/lambda/dispatch_stream/1.0.0/python/dispatch_stream'), |
| 55 | + timeout=core.Duration.seconds(10), |
| 56 | + description='', |
| 57 | + function_name='DispatchStream', |
| 58 | + reserved_concurrent_executions=10, |
| 59 | + handler=DEFAULT_LAMBDA_HANDLER, |
| 60 | + runtime=DEFAULT_LAMBDA_RUNTIME, |
| 61 | + log_retention=DEFAULT_LAMBDA_LOG_RETENTION, |
| 62 | + memory_size=128, |
| 63 | + retry_attempts=0, |
| 64 | + dead_letter_queue_enabled=False |
| 65 | + ) |
| 66 | + |
| 67 | + lambda_geocode_property = lambda_.Function( |
| 68 | + self, |
| 69 | + 'GeocodeProperty', |
| 70 | + code=lambda_.AssetCode('stack/lambda/geocode_property/1.0.0/python/geocode_property'), |
| 71 | + timeout=core.Duration.seconds(15), |
| 72 | + description='', |
| 73 | + function_name='GeocodeProperty', |
| 74 | + reserved_concurrent_executions=10, |
| 75 | + handler=DEFAULT_LAMBDA_HANDLER, |
| 76 | + runtime=DEFAULT_LAMBDA_RUNTIME, |
| 77 | + log_retention=DEFAULT_LAMBDA_LOG_RETENTION, |
| 78 | + memory_size=128, |
| 79 | + retry_attempts=0, |
| 80 | + dead_letter_queue_enabled=True |
| 81 | + ) |
| 82 | + |
| 83 | + lambda_fetch_properties = lambda_.Function( |
| 84 | + self, |
| 85 | + 'FetchProperties', |
| 86 | + code=lambda_.AssetCode('stack/lambda/fetch_properties/1.0.0/python/fetch_properties'), |
| 87 | + timeout=core.Duration.seconds(10), |
| 88 | + description='', |
| 89 | + function_name='FetchProperties', |
| 90 | + reserved_concurrent_executions=10, |
| 91 | + handler=DEFAULT_LAMBDA_HANDLER, |
| 92 | + runtime=DEFAULT_LAMBDA_RUNTIME, |
| 93 | + log_retention=DEFAULT_LAMBDA_LOG_RETENTION, |
| 94 | + memory_size=128, |
| 95 | + retry_attempts=0, |
| 96 | + dead_letter_queue_enabled=True |
| 97 | + ) |
| 98 | + |
| 99 | + # LAYERS DEFINITIONS |
| 100 | + |
| 101 | + layer_dispatch_stream = lambda_.LayerVersion( |
| 102 | + self, |
| 103 | + 'DispatchStreamLibs', |
| 104 | + code=lambda_.Code.from_asset('stack/lambda/dispatch_stream/1.0.0/'), |
| 105 | + description='', |
| 106 | + layer_version_name='DispatchStreamLibs', |
| 107 | + compatible_runtimes=[DEFAULT_LAMBDA_RUNTIME] |
| 108 | + ) |
| 109 | + |
| 110 | + layer_geocode_property = lambda_.LayerVersion( |
| 111 | + self, |
| 112 | + 'GeocodePropertyLibs', |
| 113 | + code=lambda_.Code.from_asset('stack/lambda/geocode_property/1.0.0/'), |
| 114 | + description='', |
| 115 | + layer_version_name='GeocodePropertyLibs', |
| 116 | + compatible_runtimes=[DEFAULT_LAMBDA_RUNTIME] |
| 117 | + ) |
| 118 | + |
| 119 | + layer_fetch_properties = lambda_.LayerVersion( |
| 120 | + self, |
| 121 | + 'FetchPropertiesLibs', |
| 122 | + code=lambda_.Code.from_asset('stack/lambda/fetch_properties/1.0.0/'), |
| 123 | + description='', |
| 124 | + layer_version_name='FetchPropertiesLibs', |
| 125 | + compatible_runtimes=[DEFAULT_LAMBDA_RUNTIME] |
| 126 | + ) |
| 127 | + |
| 128 | + # CLOUDWATCH RULES DEFINITIONS |
| 129 | + # - |
| 130 | + |
| 131 | + # SQS QUEUES DEFINITIONS |
| 132 | + # - |
| 133 | + |
| 134 | + # SNS TOPICS DEFINITIONS |
| 135 | + |
| 136 | + topic_new_properties = sns.Topic( |
| 137 | + self, |
| 138 | + 'NewProperties', |
| 139 | + display_name='', |
| 140 | + topic_name='NewProperties' |
| 141 | + ) |
| 142 | + |
| 143 | + # API GATEWAYS |
| 144 | + api_gateway_graphql = api_gateway.LambdaRestApi( |
| 145 | + self, |
| 146 | + 'GraphQLApi', |
| 147 | + handler=lambda_fetch_properties, |
| 148 | + rest_api_name='GraphQLApi', |
| 149 | + description='GraphQL API', |
| 150 | + cloud_watch_role=True |
| 151 | + ) |
| 152 | + api_gateway_graphql.root.add_resource('graphql').add_method('POST') |
| 153 | + |
| 154 | + # DYNAMODB PERMISSIONS |
| 155 | + lambda_dispatch_stream.add_event_source(event_sources.DynamoEventSource( |
| 156 | + table=imported_assets_config.table_property, |
| 157 | + starting_position=lambda_.StartingPosition.LATEST, |
| 158 | + batch_size=10, |
| 159 | + max_batching_window=core.Duration.seconds(30), |
| 160 | + parallelization_factor=10, |
| 161 | + retry_attempts=0 |
| 162 | + )) |
| 163 | + |
| 164 | + # CLOUDWATCH SCHEDULING RULES |
| 165 | + # - |
| 166 | + |
| 167 | + # SQS PERMISSIONS |
| 168 | + # - |
| 169 | + |
| 170 | + # SNS PERMISSIONS |
| 171 | + |
| 172 | + topic_new_properties.grant_publish(lambda_dispatch_stream) |
| 173 | + topic_new_properties.add_subscription(sns_subscriptions.LambdaSubscription(lambda_geocode_property)) |
| 174 | + |
| 175 | + # LAYERS ASSIGNMENTS |
| 176 | + |
| 177 | + lambda_dispatch_stream.add_layers(layer_dispatch_stream) |
| 178 | + lambda_geocode_property.add_layers(layer_geocode_property) |
| 179 | + lambda_fetch_properties.add_layers(layer_fetch_properties) |
| 180 | + |
| 181 | + # ENVIRONMENT VARIABLES |
| 182 | + |
| 183 | + lambda_geocode_property.add_environment(key='MONGODB_URI', value=mongodb_config.uri) |
| 184 | + lambda_geocode_property.add_environment( |
| 185 | + key='API_ACCESS_TOKEN_GEOCODING', value=access_keys_config.geocoding |
| 186 | + ) |
| 187 | + lambda_fetch_properties.add_environment(key='MONGODB_URI', value=mongodb_config.uri) |
| 188 | + lambda_fetch_properties.add_environment(key='MONGODB_MAX_PAGE_SIZE', value=mongodb_config.max_page_size) |
| 189 | + |
| 190 | + # EXPOSED ENTITIES |
| 191 | + # - |
0 commit comments