Skip to content

Commit

Permalink
chore: Refactor example Python stack by extracting a base class (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
lym953 authored Sep 27, 2024
1 parent 9da899d commit 76401a8
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 92 deletions.
99 changes: 7 additions & 92 deletions examples/python-stack/cdk_python/cdk_python_stack.py
Original file line number Diff line number Diff line change
@@ -1,100 +1,12 @@
from aws_cdk import (
aws_lambda as _lambda,
aws_lambda_go_alpha as go,
aws_apigatewayv2 as apigwv2,
BundlingOptions,
BundlingOutput,
Duration,
Stack,
)
from constructs import Construct
from datadog_cdk_constructs_v2 import Datadog
from datadog_cdk_constructs_v2 import Datadog, DatadogProps
import os
from cdk_python.cdk_python_stack_base import CdkPythonStackBase


class CdkPythonStack(Stack):
class CdkPythonStack(CdkPythonStackBase):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)

print("Creating Hello World Python stack")

hello_node = _lambda.Function(
self,
"hello-node",
runtime=_lambda.Runtime.NODEJS_20_X,
timeout=Duration.seconds(10),
memory_size=256,
code=_lambda.Code.from_asset(
"../lambda/node",
bundling=BundlingOptions(
image=_lambda.Runtime.NODEJS_20_X.bundling_image,
command=[
"bash",
"-c",
"cp -aT . /asset-output && npm install --prefix /asset-output",
],
user="root",
),
),
handler="hello.lambda_handler",
)

hello_python = _lambda.Function(
self,
"hello-python",
runtime=_lambda.Runtime.PYTHON_3_11,
timeout=Duration.seconds(10),
memory_size=256,
code=_lambda.Code.from_asset(
"../lambda/python",
bundling=BundlingOptions(
image=_lambda.Runtime.PYTHON_3_11.bundling_image,
command=[
"bash",
"-c",
"pip install -r requirements.txt -t /asset-output && cp -aT . /asset-output",
],
),
),
handler="hello.lambda_handler",
)

hello_go = go.GoFunction(
self,
"hello-go",
entry="../lambda/go/hello.go",
runtime=_lambda.Runtime.PROVIDED_AL2,
timeout=Duration.seconds(10),
bundling=go.BundlingOptions(
go_build_flags=['-ldflags "-s -w"'],
),
)

hello_dotnet = _lambda.Function(
self,
"hello-dotnet",
runtime=_lambda.Runtime.DOTNET_8,
handler="HelloWorld::HelloWorld.Handler::SayHi",
timeout=Duration.seconds(10),
memory_size=256,
code=_lambda.Code.from_asset(
"../lambda/dotnet",
bundling=BundlingOptions(
image=_lambda.Runtime.DOTNET_8.bundling_image,
command=[
'/bin/sh',
'-c',
' dotnet tool install -g Amazon.Lambda.Tools' +
' && dotnet build' +
' && dotnet lambda package --output-package /asset-output/function.zip'
],
user="root",
output_type=BundlingOutput.ARCHIVED
),
),

)

datadog = Datadog(
self,
"Datadog",
Expand All @@ -109,4 +21,7 @@ def __init__(self, scope: Construct, id: str, **kwargs) -> None:
flush_metrics_to_logs=True,
site="datadoghq.com",
)
datadog.add_lambda_functions([hello_node, hello_python, hello_go, hello_dotnet])

# Ensure DatadogProps can be imported properly
props = DatadogProps()
datadog.add_lambda_functions(self.lambdaFunctions)
94 changes: 94 additions & 0 deletions examples/python-stack/cdk_python/cdk_python_stack_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from aws_cdk import (
aws_lambda as _lambda,
aws_lambda_go_alpha as go,
BundlingOptions,
BundlingOutput,
Duration,
Stack,
)
from constructs import Construct


class CdkPythonStackBase(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)

print("Creating Hello World Python stack")

hello_node = _lambda.Function(
self,
"hello-node",
runtime=_lambda.Runtime.NODEJS_20_X,
timeout=Duration.seconds(10),
memory_size=256,
code=_lambda.Code.from_asset(
"../lambda/node",
bundling=BundlingOptions(
image=_lambda.Runtime.NODEJS_20_X.bundling_image,
command=[
"bash",
"-c",
"cp -aT . /asset-output && npm install --prefix /asset-output",
],
user="root",
),
),
handler="hello.lambda_handler",
)

hello_python = _lambda.Function(
self,
"hello-python",
runtime=_lambda.Runtime.PYTHON_3_11,
timeout=Duration.seconds(10),
memory_size=256,
code=_lambda.Code.from_asset(
"../lambda/python",
bundling=BundlingOptions(
image=_lambda.Runtime.PYTHON_3_11.bundling_image,
command=[
"bash",
"-c",
"pip install -r requirements.txt -t /asset-output && cp -aT . /asset-output",
],
),
),
handler="hello.lambda_handler",
)

hello_go = go.GoFunction(
self,
"hello-go",
entry="../lambda/go/hello.go",
runtime=_lambda.Runtime.PROVIDED_AL2,
timeout=Duration.seconds(10),
bundling=go.BundlingOptions(
go_build_flags=['-ldflags "-s -w"'],
),
)

hello_dotnet = _lambda.Function(
self,
"hello-dotnet",
runtime=_lambda.Runtime.DOTNET_8,
handler="HelloWorld::HelloWorld.Handler::SayHi",
timeout=Duration.seconds(10),
memory_size=256,
code=_lambda.Code.from_asset(
"../lambda/dotnet",
bundling=BundlingOptions(
image=_lambda.Runtime.DOTNET_8.bundling_image,
command=[
'/bin/sh',
'-c',
' dotnet tool install -g Amazon.Lambda.Tools' +
' && dotnet build' +
' && dotnet lambda package --output-package /asset-output/function.zip'
],
user="root",
output_type=BundlingOutput.ARCHIVED
),
),
)

self.lambdaFunctions = [hello_node, hello_python, hello_go, hello_dotnet]

0 comments on commit 76401a8

Please sign in to comment.