@composurecdk/lambda 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # @composurecdk/lambda
2
+
3
+ Lambda builders for [ComposureCDK](../../README.md).
4
+
5
+ This package provides a fluent builder for AWS Lambda functions with secure, AWS-recommended defaults. It wraps the CDK [Function](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html) construct — refer to the CDK documentation for the full set of configurable properties.
6
+
7
+ ## Function Builder
8
+
9
+ ```ts
10
+ import { createFunctionBuilder } from "@composurecdk/lambda";
11
+
12
+ const handler = createFunctionBuilder()
13
+ .runtime(Runtime.NODEJS_22_X)
14
+ .handler("index.handler")
15
+ .code(Code.fromAsset("lambda"))
16
+ .build(stack, "MyFunction");
17
+ ```
18
+
19
+ Every [FunctionProps](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.FunctionProps.html) property is available as a fluent setter on the builder.
20
+
21
+ ## Secure Defaults
22
+
23
+ `createFunctionBuilder` applies the following defaults. Each can be overridden via the builder's fluent API.
24
+
25
+ | Property | Default | Rationale |
26
+ | --------------- | -------- | ------------------------------------------------------------------------------------ |
27
+ | `tracing` | `ACTIVE` | Enables X-Ray distributed tracing for observability. |
28
+ | `loggingFormat` | `JSON` | Structured logs for CloudWatch Logs Insights auto-discovery and consistent querying. |
29
+
30
+ These defaults are guided by the [AWS Well-Architected Serverless Applications Lens](https://docs.aws.amazon.com/wellarchitected/latest/serverless-applications-lens/opex-distributed-tracing.html).
31
+
32
+ The defaults are exported as `FUNCTION_DEFAULTS` for visibility and testing:
33
+
34
+ ```ts
35
+ import { FUNCTION_DEFAULTS } from "@composurecdk/lambda";
36
+ ```
37
+
38
+ ### Overriding defaults
39
+
40
+ ```ts
41
+ import { LoggingFormat, Tracing } from "aws-cdk-lib/aws-lambda";
42
+
43
+ const handler = createFunctionBuilder()
44
+ .runtime(Runtime.NODEJS_22_X)
45
+ .handler("index.handler")
46
+ .code(Code.fromAsset("lambda"))
47
+ .tracing(Tracing.PASS_THROUGH)
48
+ .loggingFormat(LoggingFormat.TEXT)
49
+ .build(stack, "MyFunction");
50
+ ```
51
+
52
+ ## Examples
53
+
54
+ - [LambdaApiStack](../examples/src/lambda-api-app.ts) — REST API backed by a Lambda function, wired with `ref`
55
+ - [DualFunctionStack](../examples/src/dual-function-app.ts) — Two Lambda functions with different configurations
56
+ - [MultiStackApp](../examples/src/multi-stack-app.ts) — Lambda split across stacks via `.withStacks()`
57
+ - [StrategyStackApp](../examples/src/strategy-stack-app.ts) — Lambda split across stacks via `.withStackStrategy()`
@@ -0,0 +1,8 @@
1
+ import { type FunctionProps } from "aws-cdk-lib/aws-lambda";
2
+ /**
3
+ * Secure, AWS-recommended defaults applied to every Lambda function built
4
+ * with {@link createFunctionBuilder}. Each property can be individually
5
+ * overridden via the builder's fluent API.
6
+ */
7
+ export declare const FUNCTION_DEFAULTS: Partial<FunctionProps>;
8
+ //# sourceMappingURL=defaults.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,KAAK,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAEpF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,OAAO,CAAC,aAAa,CAapD,CAAC"}
@@ -0,0 +1,20 @@
1
+ import { LoggingFormat, Tracing } from "aws-cdk-lib/aws-lambda";
2
+ /**
3
+ * Secure, AWS-recommended defaults applied to every Lambda function built
4
+ * with {@link createFunctionBuilder}. Each property can be individually
5
+ * overridden via the builder's fluent API.
6
+ */
7
+ export const FUNCTION_DEFAULTS = {
8
+ /**
9
+ * Enable AWS X-Ray active tracing for distributed request tracking.
10
+ * @see https://docs.aws.amazon.com/wellarchitected/latest/serverless-applications-lens/opex-distributed-tracing.html
11
+ */
12
+ tracing: Tracing.ACTIVE,
13
+ /**
14
+ * Emit logs as structured JSON for CloudWatch Logs Insights auto-discovery
15
+ * and consistent querying across services.
16
+ * @see https://docs.aws.amazon.com/wellarchitected/latest/serverless-applications-lens/opex-logging.html
17
+ */
18
+ loggingFormat: LoggingFormat.JSON,
19
+ };
20
+ //# sourceMappingURL=defaults.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaults.js","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,OAAO,EAAsB,MAAM,wBAAwB,CAAC;AAEpF;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAA2B;IACvD;;;OAGG;IACH,OAAO,EAAE,OAAO,CAAC,MAAM;IAEvB;;;;OAIG;IACH,aAAa,EAAE,aAAa,CAAC,IAAI;CAClC,CAAC"}
@@ -0,0 +1,69 @@
1
+ import { Function as LambdaFunction, type FunctionProps } from "aws-cdk-lib/aws-lambda";
2
+ import { type IConstruct } from "constructs";
3
+ import { type IBuilder, type Lifecycle } from "@composurecdk/core";
4
+ type FunctionBuilderProps = FunctionProps;
5
+ /**
6
+ * The build output of a {@link IFunctionBuilder}. Contains the CDK constructs
7
+ * created during {@link Lifecycle.build}, keyed by role.
8
+ */
9
+ export interface FunctionBuilderResult {
10
+ /** The Lambda function construct created by the builder. */
11
+ function: LambdaFunction;
12
+ }
13
+ /**
14
+ * A fluent builder for configuring and creating an AWS Lambda function.
15
+ *
16
+ * Each configuration property from the CDK {@link FunctionProps} is exposed
17
+ * as an overloaded method: call with a value to set it (returns the builder
18
+ * for chaining), or call with no arguments to read the current value.
19
+ *
20
+ * The builder implements {@link Lifecycle}, so it can be used directly as a
21
+ * component in a {@link compose | composed system}. When built, it creates
22
+ * a Lambda function with the configured properties and returns a
23
+ * {@link FunctionBuilderResult}.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * const handler = createFunctionBuilder()
28
+ * .runtime(Runtime.NODEJS_22_X)
29
+ * .handler("index.handler")
30
+ * .code(Code.fromAsset("lambda"))
31
+ * .memorySize(256)
32
+ * .timeout(Duration.seconds(30));
33
+ * ```
34
+ */
35
+ export type IFunctionBuilder = IBuilder<FunctionBuilderProps, FunctionBuilder>;
36
+ declare class FunctionBuilder implements Lifecycle<FunctionBuilderResult> {
37
+ props: Partial<FunctionBuilderProps>;
38
+ build(scope: IConstruct, id: string): FunctionBuilderResult;
39
+ }
40
+ /**
41
+ * Creates a new {@link IFunctionBuilder} for configuring an AWS Lambda function.
42
+ *
43
+ * This is the entry point for defining a Lambda function component. The returned
44
+ * builder exposes every {@link FunctionProps} property as a fluent setter/getter
45
+ * and implements {@link Lifecycle} for use with {@link compose}.
46
+ *
47
+ * @returns A fluent builder for an AWS Lambda function.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * const handler = createFunctionBuilder()
52
+ * .runtime(Runtime.NODEJS_22_X)
53
+ * .handler("index.handler")
54
+ * .code(Code.fromAsset("lambda"))
55
+ * .timeout(Duration.seconds(30));
56
+ *
57
+ * // Use standalone:
58
+ * const result = handler.build(stack, "MyFunction");
59
+ *
60
+ * // Or compose into a system:
61
+ * const system = compose(
62
+ * { handler, table: createTableBuilder() },
63
+ * { handler: ["table"], table: [] },
64
+ * );
65
+ * ```
66
+ */
67
+ export declare function createFunctionBuilder(): IFunctionBuilder;
68
+ export {};
69
+ //# sourceMappingURL=function-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"function-builder.d.ts","sourceRoot":"","sources":["../src/function-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAE,KAAK,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACxF,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAW,KAAK,QAAQ,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAG5E,KAAK,oBAAoB,GAAG,aAAa,CAAC;AAE1C;;;GAGG;AACH,MAAM,WAAW,qBAAqB;IACpC,4DAA4D;IAC5D,QAAQ,EAAE,cAAc,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,MAAM,gBAAgB,GAAG,QAAQ,CAAC,oBAAoB,EAAE,eAAe,CAAC,CAAC;AAE/E,cAAM,eAAgB,YAAW,SAAS,CAAC,qBAAqB,CAAC;IAC/D,KAAK,EAAE,OAAO,CAAC,oBAAoB,CAAC,CAAM;IAE1C,KAAK,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,GAAG,qBAAqB;CAS5D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,qBAAqB,IAAI,gBAAgB,CAExD"}
@@ -0,0 +1,46 @@
1
+ import { Function as LambdaFunction } from "aws-cdk-lib/aws-lambda";
2
+ import { Builder } from "@composurecdk/core";
3
+ import { FUNCTION_DEFAULTS } from "./defaults.js";
4
+ class FunctionBuilder {
5
+ props = {};
6
+ build(scope, id) {
7
+ const mergedProps = {
8
+ ...FUNCTION_DEFAULTS,
9
+ ...this.props,
10
+ };
11
+ return {
12
+ function: new LambdaFunction(scope, id, mergedProps),
13
+ };
14
+ }
15
+ }
16
+ /**
17
+ * Creates a new {@link IFunctionBuilder} for configuring an AWS Lambda function.
18
+ *
19
+ * This is the entry point for defining a Lambda function component. The returned
20
+ * builder exposes every {@link FunctionProps} property as a fluent setter/getter
21
+ * and implements {@link Lifecycle} for use with {@link compose}.
22
+ *
23
+ * @returns A fluent builder for an AWS Lambda function.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * const handler = createFunctionBuilder()
28
+ * .runtime(Runtime.NODEJS_22_X)
29
+ * .handler("index.handler")
30
+ * .code(Code.fromAsset("lambda"))
31
+ * .timeout(Duration.seconds(30));
32
+ *
33
+ * // Use standalone:
34
+ * const result = handler.build(stack, "MyFunction");
35
+ *
36
+ * // Or compose into a system:
37
+ * const system = compose(
38
+ * { handler, table: createTableBuilder() },
39
+ * { handler: ["table"], table: [] },
40
+ * );
41
+ * ```
42
+ */
43
+ export function createFunctionBuilder() {
44
+ return Builder(FunctionBuilder);
45
+ }
46
+ //# sourceMappingURL=function-builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"function-builder.js","sourceRoot":"","sources":["../src/function-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAsB,MAAM,wBAAwB,CAAC;AAExF,OAAO,EAAE,OAAO,EAAiC,MAAM,oBAAoB,CAAC;AAC5E,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAqClD,MAAM,eAAe;IACnB,KAAK,GAAkC,EAAE,CAAC;IAE1C,KAAK,CAAC,KAAiB,EAAE,EAAU;QACjC,MAAM,WAAW,GAAG;YAClB,GAAG,iBAAiB;YACpB,GAAG,IAAI,CAAC,KAAK;SACU,CAAC;QAC1B,OAAO;YACL,QAAQ,EAAE,IAAI,cAAc,CAAC,KAAK,EAAE,EAAE,EAAE,WAAW,CAAC;SACrD,CAAC;IACJ,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,qBAAqB;IACnC,OAAO,OAAO,CAAwC,eAAe,CAAC,CAAC;AACzE,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { createFunctionBuilder, type FunctionBuilderResult, type IFunctionBuilder, } from "./function-builder.js";
2
+ export { FUNCTION_DEFAULTS } from "./defaults.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,KAAK,qBAAqB,EAC1B,KAAK,gBAAgB,GACtB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { createFunctionBuilder, } from "./function-builder.js";
2
+ export { FUNCTION_DEFAULTS } from "./defaults.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,GAGtB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC"}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@composurecdk/lambda",
3
+ "version": "0.1.0",
4
+ "description": "Composable Lambda function builder with well-architected defaults",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "types": "./dist/index.d.ts"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "README.md",
16
+ "LICENSE"
17
+ ],
18
+ "scripts": {
19
+ "clean": "rm -rf dist",
20
+ "build": "tsc -p tsconfig.build.json",
21
+ "typecheck": "tsc --noEmit",
22
+ "test": "vitest run --passWithNoTests",
23
+ "test:watch": "vitest"
24
+ },
25
+ "keywords": [],
26
+ "author": "Jason Duffett (https://github.com/laazyj)",
27
+ "license": "MIT",
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "type": "module",
32
+ "peerDependencies": {
33
+ "@composurecdk/core": "^0.1.0",
34
+ "aws-cdk-lib": "^2.0.0",
35
+ "constructs": "^10.0.0"
36
+ },
37
+ "devDependencies": {
38
+ "@types/node": "^25.5.0",
39
+ "aws-cdk-lib": "^2.245.0",
40
+ "constructs": "^10.6.0",
41
+ "typescript": "^6.0.2",
42
+ "vitest": "^4.1.2"
43
+ }
44
+ }