Skip to content
Quest 04Intermediate14 min

Wire Up a Function

The lookup logic needs somewhere to run. A Lambda function is the obvious choice, and CDK makes the wiring pleasantly small, with two traps: runtimes that AWS no longer allows, and names that don't exist yet.

What you'll learn

  • Pick a currently supported runtime. Lambda blocks creating or updating functions on deprecated runtimes, so a stack that used to deploy can start failing.
  • The defaults (3-second timeout, 128 MB) suit tiny functions. Set timeout and memorySize from your measurements.
  • Never hard-code a generated name. Pass table.tableName: CDK resolves it when CloudFormation deploys.

Section 01

A function

lambda.Function needs a runtime, a handler and code. Code.fromAsset zips a local folder at synth time and uploads it during deploy. In a real project that folder (here lambda/lookup) must exist, or synth fails with “Cannot find asset”.

A lookup function
import * as lambda from "aws-cdk-lib/aws-lambda";

const lookup = new lambda.Function(this, "LookupFn", {
  runtime: lambda.Runtime.NODEJS_22_X,
  handler: "index.handler",
  code: lambda.Code.fromAsset("lambda/lookup"),
  timeout: cdk.Duration.seconds(10),
  memorySize: 256,
  environment: { TABLE_NAME: table.tableName },
});

Section 02

Runtimes get retired

Each language version has a published deprecation date. After it, you can no longer create new functions on that runtime, and later you can't update existing ones either. The practice starter uses a deprecated runtime, and the error says so. Lambda rejects a deprecated runtime when you deploy (or update), not when you synthesize, but the simulator flags it early. Check the Lambda runtimes table in the AWS documentation for the current list and dates; at the time of writing, Node.js 22 and 24 and Python 3.12 to 3.14 are current.

  • Upgrade runtimes on a schedule, not when AWS emails you.
  • CDK's Runtime constants are marked deprecated in the library as dates approach, which shows up as a warning when you synth.

Section 03

Names that don't exist yet

When you don't pass tableName, CloudFormation generates one, something like PokedexStack-PokedexTable-1UVA2OX3Z7WRL. You can't know it while writing code. So table.tableName isn't a string at all: it is a token, a placeholder that becomes a Ref in the template. Writing the literal "PokedexTable" would point the function at a table that doesn't exist.

The next quests build on this idea, so remember it: a value that comes from another construct is a reference, not text.

Practice

Your turn

Add the lookup function. The starter uses a runtime Lambda no longer supports and hard-codes the table name — which CDK generates at deploy time. Use a current runtime (Node.js 22 or 24, Python 3.12 to 3.14), give the function a 10-second timeout and 256 MB of memory, and pass the real table name through TABLE_NAME.

A current runtime, timeout 5–29 s, memorySize 256–1024 MB (Node.js 22/24 or Python 3.12–3.14), and TABLE_NAME: table.tableName.
Tab inserts two spaces. Press Escape, then Tab, to leave the editor. Control or Command plus Enter runs the code.
Simulated — no AWS calls

The simulator runs the constructs this course teaches, plus simple loops and helper methods. Other CDK code may not be recognised.

Check

Check your understanding

Quick Quiz · one question, as many tries as you need.

The function needs the table name. What is the best way to give it?

Choose one answer.