Created
October 16, 2021 03:26
-
-
Save neilkuan/8a6a83781f1da9ffb5f6335cf02b64ed to your computer and use it in GitHub Desktop.
cross-stack-cdk-python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
from aws_cdk import core | |
from py_d.db_stack import DbStack | |
from py_d.lambda_stack import LambdaStack | |
env = core.Environment(account=os.getenv('CDK_DEFAULT_ACCOUNT'), region=os.getenv('CDK_DEFAULT_REGION')) | |
app = core.App() | |
db = DbStack(app, "PyDStack", env=env ) | |
LambdaStack(app, "LambdaStack",env=env, db_table=db.dbtable) | |
app.synth() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aws_cdk import ( | |
core as cdk, | |
aws_dynamodb as dynamodb, | |
) | |
class DbStack(cdk.Stack): | |
def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None: | |
super().__init__(scope, construct_id, **kwargs) | |
self.dbtable = dynamodb.Table(self, "dbtable", partition_key=dynamodb.Attribute(name="id", type=dynamodb.AttributeType.STRING)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aws_cdk import ( | |
core as cdk, | |
aws_lambda as lambda_, | |
aws_dynamodb as dynamodb, | |
) | |
class LambdaStack(cdk.Stack): | |
def __init__(self, scope: cdk.Construct, construct_id: str, db_table: dynamodb.Table,**kwargs) -> None: | |
super().__init__(scope, construct_id, **kwargs) | |
inline_code = '''import os | |
table = os.getenv('TABLE_NAME') | |
def handler(event, context): | |
return { | |
'event': event, | |
'table_arn': table | |
}''' | |
lambda_.Function(self, "Lambda", code=lambda_.InlineCode(inline_code), | |
handler="index.handler", | |
runtime=lambda_.Runtime.PYTHON_3_8, | |
environment={ | |
"TABLE_NAME": db_table.table_arn | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment