Skip to content

Commit

Permalink
chore: rename Lambda related interfaces (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
lym953 authored Oct 15, 2024
1 parent ba9f865 commit 5b1db2a
Show file tree
Hide file tree
Showing 23 changed files with 1,215 additions and 124 deletions.
42 changes: 39 additions & 3 deletions examples/go-stack/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,38 @@ func NewAppStackWithoutDatadog(scope constructs.Construct, id *string, props *Ap
return stack, myFunction
}

// Creates a stack with Datadog integration set up
func NewAppStackWithDatadog(scope constructs.Construct, id string, props *AppStackProps) awscdk.Stack {
// Creates a stack with Datadog integration set up, using the new API (DatadogLambda, DatadogLambdaProps)
func NewAppStackWithDatadogLambda(scope constructs.Construct, id string, props *AppStackProps) awscdk.Stack {
stack, lambdaFunction := NewAppStackWithoutDatadog(scope, &id, props)

// Set up Datadog integration
datadog := ddcdkconstruct.NewDatadogLambda(
stack,
jsii.String("Datadog"),
&ddcdkconstruct.DatadogLambdaProps{
NodeLayerVersion: jsii.Number(113),
PythonLayerVersion: jsii.Number(97),
JavaLayerVersion: jsii.Number(21),
DotnetLayerVersion: jsii.Number(15),
AddLayers: jsii.Bool(true),
ExtensionLayerVersion: jsii.Number(62),
FlushMetricsToLogs: jsii.Bool(true),
Site: jsii.String("datadoghq.com"),
ApiKey: jsii.String(os.Getenv("DD_API_KEY")),
EnableDatadogTracing: jsii.Bool(true),
EnableMergeXrayTraces: jsii.Bool(true),
EnableDatadogLogs: jsii.Bool(true),
InjectLogContext: jsii.Bool(true),
LogLevel: jsii.String("debug"),
})
datadog.AddLambdaFunctions(&[]interface{}{lambdaFunction}, nil)

return stack
}

// Creates a stack with Datadog integration set up, using the old API (Datadog, DatadogProps) to ensure
// backward compatibility. Users are recommended to use the new API.
func NewAppStackWithDatadogOldApi(scope constructs.Construct, id string, props *AppStackProps) awscdk.Stack {
stack, lambdaFunction := NewAppStackWithoutDatadog(scope, &id, props)

// Set up Datadog integration
Expand Down Expand Up @@ -82,7 +112,13 @@ func main() {

app := awscdk.NewApp(nil)

NewAppStackWithDatadog(app, "AppStack", &AppStackProps{
NewAppStackWithDatadogLambda(app, "CdkGoStack", &AppStackProps{
awscdk.StackProps{
Env: env(),
},
})

NewAppStackWithDatadogOldApi(app, "CdkGoLambdaOldApiStack", &AppStackProps{
awscdk.StackProps{
Env: env(),
},
Expand Down
2 changes: 2 additions & 0 deletions examples/python-stack/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python3
from aws_cdk import App
from cdk_python.cdk_python_stack import CdkPythonStack
from cdk_python.cdk_python_lambda_old_api_stack import CdkPythonLambdaOldApiStack

app = App()
CdkPythonStack(app, "CdkPythonStack")
CdkPythonLambdaOldApiStack(app, "CdkPythonLambdaOldApiStack")

app.synth()
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from constructs import Construct
from datadog_cdk_constructs_v2 import Datadog, DatadogProps
import os
from cdk_python.cdk_python_stack_base import CdkPythonStackBase

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

datadog = Datadog(
self,
"Datadog",
dotnet_layer_version=15,
node_layer_version=107,
python_layer_version=89,
extension_layer_version=55,
add_layers=True,
api_key=os.getenv("DD_API_KEY"),
enable_datadog_tracing=True,
enable_datadog_asm=True,
flush_metrics_to_logs=True,
site="datadoghq.com",
)

# Ensure DatadogProps can be imported properly
props = DatadogProps()
datadog.add_lambda_functions(self.lambdaFunctions)
8 changes: 4 additions & 4 deletions examples/python-stack/cdk_python/cdk_python_stack.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from constructs import Construct
from datadog_cdk_constructs_v2 import Datadog, DatadogProps
from datadog_cdk_constructs_v2 import DatadogLambda, DatadogLambdaProps
import os
from cdk_python.cdk_python_stack_base import CdkPythonStackBase

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

datadog = Datadog(
datadog = DatadogLambda(
self,
"Datadog",
dotnet_layer_version=15,
Expand All @@ -22,6 +22,6 @@ def __init__(self, scope: Construct, id: str, **kwargs) -> None:
site="datadoghq.com",
)

# Ensure DatadogProps can be imported properly
props = DatadogProps()
# Ensure DatadogLambdaProps can be imported properly
props = DatadogLambdaProps()
datadog.add_lambda_functions(self.lambdaFunctions)
2 changes: 2 additions & 0 deletions examples/typescript-stack/bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env node
import * as cdk from "aws-cdk-lib";
import { CdkTypeScriptStack } from "../lib/cdk-typescript-stack";
import { CdkTypeScriptLambdaOldApiStack } from "../lib/cdk-typescript-lambda-old-api-stack";

const app = new cdk.App();
new CdkTypeScriptStack(app, "CdkTypeScriptStack", {});
new CdkTypeScriptLambdaOldApiStack(app, "CdkTypeScriptLambdaOldApiStack", {});
app.synth();
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import { Datadog, DatadogProps } from "datadog-cdk-constructs-v2";
import { CdkTypeScriptStackBase } from "./cdk-typescript-stack-base";

export class CdkTypeScriptLambdaOldApiStack extends CdkTypeScriptStackBase {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

console.log("Instrumenting Lambda Functions in TypeScript stack using the old Datadog Lambda API");

const datadogProps: DatadogProps = {
dotnetLayerVersion: 15,
nodeLayerVersion: 108,
pythonLayerVersion: 89,
extensionLayerVersion: 55,
addLayers: true,
apiKey: process.env.DD_API_KEY,
enableDatadogTracing: true,
enableDatadogASM: true,
flushMetricsToLogs: true,
site: "datadoghq.com",
};

const datadog = new Datadog(this, "Datadog", datadogProps);

datadog.addLambdaFunctions(this.lambdaFunctions);
}
}
2 changes: 1 addition & 1 deletion examples/typescript-stack/lib/cdk-typescript-stack-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Function } from "aws-cdk-lib/aws-lambda";
import { HttpLambdaIntegration } from "aws-cdk-lib/aws-apigatewayv2-integrations";
import { Construct } from "constructs";

export class CdkTypeScriptStackBase extends Stack {
export abstract class CdkTypeScriptStackBase extends Stack {
protected lambdaFunctions: lambda.Function[];
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
Expand Down
6 changes: 3 additions & 3 deletions examples/typescript-stack/lib/cdk-typescript-stack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import { Datadog, DatadogProps } from "datadog-cdk-constructs-v2";
import { DatadogLambda, DatadogLambdaProps } from "datadog-cdk-constructs-v2";
import { CdkTypeScriptStackBase } from "./cdk-typescript-stack-base";

export class CdkTypeScriptStack extends CdkTypeScriptStackBase {
Expand All @@ -9,7 +9,7 @@ export class CdkTypeScriptStack extends CdkTypeScriptStackBase {

console.log("Instrumenting Lambda Functions in TypeScript stack with Datadog");

const datadogProps: DatadogProps = {
const datadogLambdaProps: DatadogLambdaProps = {
dotnetLayerVersion: 15,
nodeLayerVersion: 108,
pythonLayerVersion: 89,
Expand All @@ -22,7 +22,7 @@ export class CdkTypeScriptStack extends CdkTypeScriptStackBase {
site: "datadoghq.com",
};

const datadog = new Datadog(this, "Datadog", datadogProps);
const datadog = new DatadogLambda(this, "Datadog", datadogLambdaProps);

datadog.addLambdaFunctions(this.lambdaFunctions);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
{
"Resources": {
"HelloWorldFunctionServiceRole8E0BD458": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
]
}
]
},
"Metadata": {
"aws:cdk:path": "LambdaGoOldLambdaApiStack/HelloWorldFunction/ServiceRole/Resource"
}
},
"HelloWorldFunctionB2AB6E79": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "\n\t\t exports.handler = async function(event) {\n\t\t\treturn {\n\t\t\t statusCode: 200,\n\t\t\t body: JSON.stringify('Hello World!'),\n\t\t\t};\n\t\t };\n\t\t"
},
"Environment": {
"Variables": {
"DD_LAMBDA_HANDLER": "index.handler",
"DD_TRACE_ENABLED": "true",
"DD_SERVERLESS_APPSEC_ENABLED": "false",
"DD_MERGE_XRAY_TRACES": "true",
"DD_LOGS_INJECTION": "false",
"DD_SERVERLESS_LOGS_ENABLED": "true",
"DD_CAPTURE_LAMBDA_PAYLOAD": "false",
"DD_LOG_LEVEL": "debug",
"DD_FLUSH_TO_LOG": "false",
"DD_SITE": "datadoghq.com",
"DD_API_KEY": "1234",
"DD_TAGS": "git.commit.sha:XXXXXXXX,git.repository_url:github.com/DataDog/datadog-cdk-constructs"
}
},
"Handler": "/opt/nodejs/node_modules/datadog-lambda-js/handler.handler",
"Layers": [
"arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Node20-x:113",
"arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Extension:62"
],
"Role": {
"Fn::GetAtt": [
"HelloWorldFunctionServiceRole8E0BD458",
"Arn"
]
},
"Runtime": "nodejs20.x",
"Tags": [
{
"Key": "dd_cdk_construct",
"Value": "vX.XX.X"
}
]
},
"DependsOn": [
"HelloWorldFunctionServiceRole8E0BD458"
],
"Metadata": {
"aws:cdk:path": "LambdaGoOldLambdaApiStack/HelloWorldFunction/Resource"
}
},
"HelloWorldFunctionFunctionUrl4150BDAD": {
"Type": "AWS::Lambda::Url",
"Properties": {
"AuthType": "NONE",
"TargetFunctionArn": {
"Fn::GetAtt": [
"HelloWorldFunctionB2AB6E79",
"Arn"
]
}
},
"Metadata": {
"aws:cdk:path": "LambdaGoOldLambdaApiStack/HelloWorldFunction/FunctionUrl/Resource"
}
},
"HelloWorldFunctioninvokefunctionurlA2CB1A84": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunctionUrl",
"FunctionName": {
"Fn::GetAtt": [
"HelloWorldFunctionB2AB6E79",
"Arn"
]
},
"FunctionUrlAuthType": "NONE",
"Principal": "*"
},
"Metadata": {
"aws:cdk:path": "LambdaGoOldLambdaApiStack/HelloWorldFunction/invoke-function-url"
}
},
"CDKMetadata": {
"Type": "AWS::CDK::Metadata",
"Properties": {
"Analytics": "vX:XXXXXX:XXXXXX"
},
"Metadata": {
"aws:cdk:path": "LambdaGoOldLambdaApiStack/CDKMetadata/Default"
}
}
},
"Outputs": {
"myFunctionUrlOutput": {
"Value": {
"Fn::GetAtt": [
"HelloWorldFunctionFunctionUrl4150BDAD",
"FunctionUrl"
]
}
}
},
"Parameters": {
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
}
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5"
],
{
"Ref": "BootstrapVersion"
}
]
}
]
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}
]
}
}
}
Loading

0 comments on commit 5b1db2a

Please sign in to comment.