""" Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: MIT-0 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. chaliceapp.py This files is the infrastructure as a code in CDK, it generates the required integrations required for this project. """ import os import json from aws_cdk import aws_lambda as _lambda, aws_dynamodb as dynamodb try: from aws_cdk import core as cdk except ImportError: import aws_cdk as cdk from chalice.cdk import Chalice RUNTIME_SOURCE_DIR = os.path.join( os.path.dirname(os.path.dirname(__file__)), os.pardir, "runtime" ) class ChaliceApp(cdk.Stack): def __init__(self, scope, id, **kwargs): super().__init__(scope, id, **kwargs) self.dynamodb_table = self._create_ddb_table() self.chalice = Chalice( self, "ChaliceApp", source_dir=RUNTIME_SOURCE_DIR, stage_config={ "environment_variables": { "APP_TABLE_NAME": self.dynamodb_table.table_name } }, ) self.dynamodb_table.grant_read_write_data(self.chalice.get_role("DefaultRole")) self.async_lambda = self._set_ddb_trigger_function(self.dynamodb_table) def _create_ddb_table(self): dynamodb_table = dynamodb.Table( self, "AppTable", partition_key=dynamodb.Attribute( name="PK", type=dynamodb.AttributeType.STRING ), sort_key=dynamodb.Attribute(name="SK", type=dynamodb.AttributeType.STRING), billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST, stream=dynamodb.StreamViewType.NEW_AND_OLD_IMAGES, removal_policy=cdk.RemovalPolicy.DESTROY, ) cdk.CfnOutput(self, "AppTableName", value=dynamodb_table.table_name) return dynamodb_table def _set_ddb_trigger_function(self, ddb_table): async_lambda = _lambda.Function( self, "AsyncHandler", runtime=_lambda.Runtime.PYTHON_3_9, code=_lambda.Code.from_asset("asycn_lambda"), handler="app.handler", environment={ "APP_TABLE_NAME": ddb_table.table_name, }, ) ddb_table.grant_stream_read(async_lambda) company_inserts = _lambda.CfnEventSourceMapping( scope=self, id="companyInsertsOnlyEventSourceMapping", function_name=async_lambda.function_name, event_source_arn=ddb_table.table_stream_arn, maximum_batching_window_in_seconds=1, starting_position="LATEST", batch_size=1, ) company_inserts.add_property_override( property_path="FilterCriteria", value={ "Filters": [ { "Pattern": json.dumps( { "eventName": ["INSERT", "MODIFY"], "dynamodb": { "NewImage": { "PK": {"S": [{"prefix": "COMPANY"}]}, "SK": {"S": [{"prefix": "PRODUCT"}]}, } }, } ) } ] }, ) ddb_table.grant_read_write_data(async_lambda)