Skip to content

Commit

Permalink
add infra common stack
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvictor committed Apr 7, 2024
1 parent 97b4111 commit c776335
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Empty file added aws_packages/infra/__init__.py
Empty file.
71 changes: 71 additions & 0 deletions aws_packages/infra/common_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from os import path

from aws_cdk import CfnOutput, Duration, RemovalPolicy, Stack
from aws_cdk import aws_apigateway as apigw
from aws_cdk import aws_dynamodb as dynamodb
from aws_cdk import aws_lambda as lambda_
from aws_cdk.aws_apigateway import Cors, CorsOptions
from constructs import Construct


class CommonServiceStack(Stack):
def __init__(
self, scope: Construct, construct_id: str, stage_name="dev", **kwargs
) -> None:
super().__init__(scope, construct_id, **kwargs)

self._stage_name = stage_name

def _create_general_dynamo_table(self, name: str, stage_name: str):
return dynamodb.TableV2(
self,
f"{self._stage_name}-{name}-table",
table_name=f"{self._stage_name}-{name}-table",
partition_key=dynamodb.Attribute(
name="pk", type=dynamodb.AttributeType.STRING
),
contributor_insights=True,
table_class=dynamodb.TableClass.STANDARD,
point_in_time_recovery=True,
removal_policy=(
RemovalPolicy.RETAIN
if stage_name in ["prod"]
else RemovalPolicy.DESTROY
),
)

def _create_python_lambda_function(
self,
identifier: str,
source: str,
lambda_layers: list,
envs: dict,
function_handler: str,
):
_lambda_function = lambda_.Function(
self,
identifier,
function_name=identifier,
code=lambda_.Code.from_asset(path.join(source)),
handler=function_handler,
runtime=lambda_.Runtime.PYTHON_3_12,
timeout=Duration.seconds(300),
layers=lambda_layers,
environment=envs,
)
return _lambda_function

def _create_api_gw(self, identifier: str, handler_function):
api_gateway_resource = apigw.LambdaRestApi(
self,
identifier,
handler=handler_function,
proxy=False,
default_cors_preflight_options=CorsOptions(
allow_origins=Cors.ALL_ORIGINS,
allow_methods=Cors.ALL_METHODS,
allow_credentials=True,
allow_headers=Cors.DEFAULT_HEADERS,
),
)
return api_gateway_resource

0 comments on commit c776335

Please sign in to comment.