Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ CDKTN includes two packages:
- [cdktn-cli](./packages/cdktn-cli) - A CLI that allows users to run commands to initialize, import, and synthesize CDK Terrain applications.
- [cdktn](./packages/cdktn) - A library for defining Terraform resources using programming constructs.

First-party TypeScript/JavaScript packages add [Node.js Lambda functions](./packages/@cdktn/aws-lambda-nodejs) and reusable [native Node.js bundle assets](./packages/@cdktn/bundler-nodejs).

## Get Started

- [Overview](https://cdktn.io/docs)
Expand Down
28 changes: 28 additions & 0 deletions examples/typescript/aws-nodejs-function/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Ship a Node.js Lambda

This example deploys a TypeScript handler with one `NodejsFunction` construct. Its Rolldown bundle, ZIP, code hash, role and log group are created automatically.

From the repository root, build the first-party packages:

```sh
pnpm exec nx run-many -t build -p @cdktn/aws-lambda-nodejs cdktn-cli
cd examples/typescript/aws-nodejs-function
pnpm run synth
```

To deploy into your configured AWS account in `eu-central-1`, run `pnpm run deploy`. Use `cdktn destroy` to remove the example's resources afterwards. To run the app directly, use `node main.ts` on Node.js 22.18 or newer.

[`main.ts`](main.ts) contains the infrastructure and [`src/hello.ts`](src/hello.ts) is the deployed handler. See the [package guide](../../../packages/@cdktn/aws-lambda-nodejs/README.md) for runtime settings, permissions, existing roles, VPC support and bundling options.

## AWS smoke test

After building the packages, run the opt-in deployment test with an AWS profile permitted to manage Lambda functions, IAM execution roles/policies and CloudWatch log groups:

```sh
node verify-aws.mjs --synth-only
node verify-aws.mjs --profile my-test-profile --region eu-central-1
```

The test deploys three uniquely named Node.js 24 functions: this example's handler, an ESM handler with top-level await on ARM64, and a CommonJS handler on x86-64. It verifies deployed ZIP hashes, invocation results, TypeScript aliases, CommonJS dependencies, lazy imports, copied files, environment variables, JSON logs, source maps and log retention. It then checks a fresh synthesis produces no changes, changes a transitive source dependency, verifies that only the two affected functions update, and checks for drift again.

The test uses the AWS CLI and Terraform from `PATH`; `TERRAFORM_BINARY_NAME` can select another Terraform executable. It stores command output and local Terraform state in the temporary directory printed at startup. These files contain account and resource identifiers and should stay private. The test destroys its resources in `finally` and independently checks that the functions, log groups and execution roles are absent. If the process is forcibly interrupted or cleanup fails, use `terraform -chdir=<printed-directory>/cdktf.out/stacks/<stack-id> destroy` with the same AWS profile to resume cleanup.
5 changes: 5 additions & 0 deletions examples/typescript/aws-nodejs-function/cdktf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"language": "typescript",
"app": "node main.ts",
"sendCrashReports": "false"
}
17 changes: 17 additions & 0 deletions examples/typescript/aws-nodejs-function/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) OpenConstructs
// SPDX-License-Identifier: MPL-2.0
import { App, TerraformOutput, TerraformStack } from "cdktn";
import { AwsProvider } from "@cdktn/provider-aws/lib/provider/index.js";
import { NodejsFunction } from "@cdktn/aws-lambda-nodejs";

const app = new App();
const stack = new TerraformStack(app, "hello");
new AwsProvider(stack, "aws", { region: "eu-central-1" });

const hello = new NodejsFunction(stack, "hello", {
entry: "src/hello.ts",
environment: { GREETING: "Hello from CDK Terrain" },
});

new TerraformOutput(stack, "function_name", { value: hello.functionName });
app.synth();
15 changes: 15 additions & 0 deletions examples/typescript/aws-nodejs-function/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@examples/typescript-aws-nodejs-function",
"private": true,
"version": "0.0.0",
"type": "module",
"engines": { "node": ">=22.18.0" },
"scripts": { "synth": "cdktn synth", "deploy": "cdktn deploy" },
"dependencies": {
"@cdktn/aws-lambda-nodejs": "workspace:*",
"@cdktn/provider-aws": "25.4.0",
"cdktn": "workspace:*",
"constructs": "10.6.0"
},
"devDependencies": { "cdktn-cli": "workspace:*" }
}
7 changes: 7 additions & 0 deletions examples/typescript/aws-nodejs-function/src/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) OpenConstructs
// SPDX-License-Identifier: MPL-2.0
export async function handler(event: { name?: string } = {}) {
return {
message: `${process.env.GREETING ?? "Hello"}, ${event.name ?? "world"}!`,
};
}
Loading
Loading