Skip to content

Commit 8f17545

Browse files
jeastham1993claude
andcommitted
fix(pricing): separate CDK build scripts with external dd-trace
CDK synthesises Lambda code by re-running esbuild scripts directly via Code.fromCustomCommand — it does not consume the pre-built ZIPs produced by make build / package.sh. This means CDK and Terraform/SAM can use different build configurations without needing a build-mode flag. Add src/pricing-api/cdk/ build scripts that mark dd-trace as external. The Datadog Lambda layer (nodeLayerVersion: 130, added by the CDK construct with enableDatadogTracing: true) provides dd-trace at /opt/nodejs at runtime. The layer wrapper (datadog_lambda.handler) initialises dd-trace and flushes spans after each invocation. Restore enableDatadogTracing: true in the CDK stack so the Lambda tracing layer and handler wrapper are added back. Terraform/SAM continue to use the adapters/ build scripts which bundle dd-trace. They have no Lambda layer so traces are not forwarded, but the functions run correctly and integration tests pass. Build script ownership: adapters/ — bundled dd-trace (Terraform, SAM) cdk/ — external dd-trace (CDK, Lambda layer required) workshop/ — no dd-trace (workshop stubs) Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent 49a6122 commit 8f17545

6 files changed

Lines changed: 141 additions & 11 deletions

File tree

src/pricing-service/lib/pricing-api/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class Api extends Construct {
5454
const isWorkshopBuild = process.env.WORKSHOP_BUILD === "true";
5555
const pathToBuildFile = isWorkshopBuild
5656
? "./src/pricing-api/workshop/buildCalculatePricingFunction.js"
57-
: "./src/pricing-api/adapters/buildCalculatePricingFunction.js";
57+
: "./src/pricing-api/cdk/buildCalculatePricingFunction.js";
5858
const pathToOutputFile = "./out/calculatePricingFunction";
5959

6060
const code = Code.fromCustomCommand(pathToOutputFile, [

src/pricing-service/lib/pricing-api/pricingApiStack.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,15 @@ export class PricingApiStack extends cdk.Stack {
3333
const datadogConfiguration = isWorkshopBuild
3434
? undefined
3535
: new DatadogLambda(this, "Datadog", {
36-
// dd-trace is bundled via esbuild so the Lambda tracing layer is not
37-
// needed and would create a second tracer instance if added. The
38-
// extension layer (below) is still used to forward traces and metrics
39-
// from the bundled dd-trace to Datadog.
36+
nodeLayerVersion: 130,
4037
extensionLayerVersion: 90,
4138
site: process.env.DD_SITE ?? "datadoghq.com",
4239
apiKeySecret: ddApiKey,
4340
service,
4441
version,
4542
env,
4643
enableColdStartTracing: true,
47-
enableDatadogTracing: false,
44+
enableDatadogTracing: true,
4845
captureLambdaPayload: true,
4946
});
5047

@@ -66,18 +63,18 @@ export class PricingApiStack extends cdk.Stack {
6663
});
6764

6865
if (!isWorkshopBuild) {
69-
new PricingEventHandlers(this, "PricingEventHandlers", {
66+
const _ = new PricingEventHandlers(this, "PricingEventHandlers", {
7067
serviceProps: pricingServiceProps,
7168
ddApiKeySecret: ddApiKey,
7269
});
7370
}
7471

75-
new StringParameter(this, "PricingAPIEndpoint", {
72+
const _param = new StringParameter(this, "PricingAPIEndpoint", {
7673
parameterName: `/${sharedProps.environment}/${sharedProps.serviceName}/api-endpoint`,
7774
stringValue: api.api.url,
7875
});
7976

80-
new cdk.CfnOutput(this, `PricingServiceApiEndpoint-${env}`, {
77+
const _output = new cdk.CfnOutput(this, `PricingServiceApiEndpoint-${env}`, {
8178
exportName: `PricingServiceApiEndpoint-${env}`,
8279
value: `${api.api.url}pricing`,
8380
});

src/pricing-service/lib/pricing-api/pricingEventHandlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class PricingEventHandlers extends Construct {
4242
}).queue;
4343
const pathToBuildFile = isWorkshopBuild
4444
? "./src/pricing-api/workshop/buildProductCreatedPricingHandler.js"
45-
: "./src/pricing-api/adapters/buildProductCreatedPricingHandler.js";
45+
: "./src/pricing-api/cdk/buildProductCreatedPricingHandler.js";
4646
const pathToOutputFile = "./out/productCreatedPricingHandler";
4747

4848
const code = Code.fromCustomCommand(pathToOutputFile, [
@@ -127,7 +127,7 @@ export class PricingEventHandlers extends Construct {
127127

128128
const pathToBuildFile = isWorkshopBuild
129129
? "./src/pricing-api/workshop/buildProductUpdatedPricingHandler.js"
130-
: "./src/pricing-api/adapters/buildProductUpdatedPricingHandler.js";
130+
: "./src/pricing-api/cdk/buildProductUpdatedPricingHandler.js";
131131
const pathToOutputFile = "./out/productUpdatedPricingHandler";
132132

133133
const code = Code.fromCustomCommand(pathToOutputFile, [
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Unless explicitly stated otherwise all files in this repository are licensed
3+
// under the Apache License Version 2.0.
4+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
5+
// Copyright 2024 Datadog, Inc.
6+
//
7+
// CDK build — dd-trace is provided by the Datadog Lambda layer at runtime
8+
// (added via enableDatadogTracing: true in the DatadogLambda CDK construct).
9+
// Do NOT use this script for Terraform or SAM deployments; use the
10+
// equivalent script in adapters/ which bundles dd-trace instead.
11+
//
12+
13+
const esbuild = require("esbuild");
14+
15+
esbuild
16+
.build({
17+
entryPoints: ["./src/pricing-api/adapters/calculatePricingFunction.ts"],
18+
bundle: true,
19+
minify: true,
20+
keepNames: true,
21+
outfile: "out/calculatePricingFunction/index.js",
22+
platform: "node",
23+
target: ["node22"],
24+
external: [
25+
// provided by the Datadog Lambda layer at runtime
26+
"dd-trace",
27+
28+
// esbuild cannot bundle native modules
29+
"@datadog/native-metrics",
30+
"@datadog/pprof",
31+
"@datadog/native-appsec",
32+
"@datadog/native-iast-taint-tracking",
33+
"@datadog/native-iast-rewriter",
34+
35+
// graphql
36+
"graphql/language/visitor",
37+
"graphql/language/printer",
38+
"graphql/utilities",
39+
40+
"@aws-sdk/client-eventbridge",
41+
"@aws-sdk/client-ssm",
42+
],
43+
})
44+
.catch((err) => {
45+
console.error(err);
46+
process.exit(1);
47+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Unless explicitly stated otherwise all files in this repository are licensed
3+
// under the Apache License Version 2.0.
4+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
5+
// Copyright 2024 Datadog, Inc.
6+
//
7+
// CDK build — dd-trace is provided by the Datadog Lambda layer at runtime.
8+
//
9+
10+
const esbuild = require("esbuild");
11+
12+
esbuild
13+
.build({
14+
entryPoints: ["./src/pricing-api/adapters/productCreatedPricingHandler.ts"],
15+
bundle: true,
16+
minify: true,
17+
keepNames: true,
18+
outfile: "out/productCreatedPricingHandler/index.js",
19+
platform: "node",
20+
target: ["node22"],
21+
external: [
22+
// provided by the Datadog Lambda layer at runtime
23+
"dd-trace",
24+
25+
// esbuild cannot bundle native modules
26+
"@datadog/native-metrics",
27+
"@datadog/pprof",
28+
"@datadog/native-appsec",
29+
"@datadog/native-iast-taint-tracking",
30+
"@datadog/native-iast-rewriter",
31+
32+
// graphql
33+
"graphql/language/visitor",
34+
"graphql/language/printer",
35+
"graphql/utilities",
36+
37+
"@aws-sdk/client-sqs",
38+
],
39+
})
40+
.catch((err) => {
41+
console.error(err);
42+
process.exit(1);
43+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Unless explicitly stated otherwise all files in this repository are licensed
3+
// under the Apache License Version 2.0.
4+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
5+
// Copyright 2024 Datadog, Inc.
6+
//
7+
// CDK build — dd-trace is provided by the Datadog Lambda layer at runtime.
8+
//
9+
10+
const esbuild = require("esbuild");
11+
12+
esbuild
13+
.build({
14+
entryPoints: ["./src/pricing-api/adapters/productUpdatedPricingHandler.ts"],
15+
bundle: true,
16+
minify: true,
17+
keepNames: true,
18+
outfile: "out/productUpdatedPricingHandler/index.js",
19+
platform: "node",
20+
target: ["node22"],
21+
external: [
22+
// provided by the Datadog Lambda layer at runtime
23+
"dd-trace",
24+
25+
// esbuild cannot bundle native modules
26+
"@datadog/native-metrics",
27+
"@datadog/pprof",
28+
"@datadog/native-appsec",
29+
"@datadog/native-iast-taint-tracking",
30+
"@datadog/native-iast-rewriter",
31+
32+
// graphql
33+
"graphql/language/visitor",
34+
"graphql/language/printer",
35+
"graphql/utilities",
36+
37+
"@aws-sdk/client-sqs",
38+
],
39+
})
40+
.catch((err) => {
41+
console.error(err);
42+
process.exit(1);
43+
});

0 commit comments

Comments
 (0)