HTTP & Runtime

Lambda Web Functions

A Lambda Web Function serves HTTP directly, and one execution environment answers many concurrent requests — 64 by default, up to 128. How the application serves those requests depends on the runtime, and both upstream models are composed here:

  • Node.js (nodejs24.x, nodejs26.x): the application starts its own HTTP server, Lambda forwards each request to it, and the application reads AWS_LAMBDA_HTTP_ENDPOINT for the address to listen on.
  • Python (python3.15, upstream public preview): Lambda manages the HTTP server and passes each request to an ASGI or WSGI application. The composition names the application; the entry point is written for you.

Either way, a Web Function is a different execution model from the request/response runtime described in HTTP Runtime (P0–P2), and this page covers the deployment surface for it, AppTheoryWebFunction.

One composition, three resources

AppTheoryWebFunction creates the function, its first revision, and the endpoint it serves requests on:

import { AppTheoryWebFunction } from "@theory-cloud/apptheory-cdk";
import * as lambda from "aws-cdk-lib/aws-lambda";

const web = new AppTheoryWebFunction(stack, "Web", {
  code: lambda.WebFunctionCode.fromAssetToManagedBucket("app"),
  runtime: lambda.WebFunctionRuntime.NODEJS_24_X,
  entryPoint: "server.mjs",
});

// web.domainName   — request this
// web.revisionId   — the revision the endpoint routes to
// web.endpoint     — the endpoint resource, its grants, and its L1 escape hatch

Nothing here reads an environment variable to decide how it deploys. The endpoint’s shape, the runtime, the entry file and the authorization mode are all encoded configuration, fixed at synthesis.

The application does not need an adapter and does not get a second entry point. On Node.js it starts an HTTP server and reads AWS_LAMBDA_HTTP_ENDPOINT for the address to listen on; on Python it is an ASGI or WSGI application, and Lambda supplies the server. Those two variables the service and this construct own — AWS_LAMBDA_HTTP_ENDPOINT and the runtime’s entry point — are refused in props.environment: the framework will not let you shadow one.

A Python Web Function composes the same way, and the entry point is not yours to spell:

import { AppTheoryPythonInterface, AppTheoryWebFunction } from "@theory-cloud/apptheory-cdk";

const pythonWeb = new AppTheoryWebFunction(stack, "PythonWeb", {
  code: lambda.WebFunctionCode.fromAssetToManagedBucket("app"),
  runtime: lambda.WebFunctionRuntime.of("python3.15"),
  // Served as `asgi:main:app`: the module, the callable and the interface, decided at synthesis.
  pythonApp: { module: "main", interface: AppTheoryPythonInterface.ASGI },
});

pythonApp names the module (without .py), the callable (default app) and the interface. The construct writes AWS_LAMBDA_PYTHON_ENTRYPOINT as interface:module:callable, so the runtime serves the interface you declared instead of detecting one. entryPoint stays Node-only: it names an entry file, and a Python Web Function names an application.

Endpoint authorization defaults to IamAuth

aws-cdk-lib requires the endpoint’s authorization mode to be chosen explicitly, so that an endpoint is never accidentally public or accidentally locked down. AppTheory picks the locked-down answer as the default:

authType Endpoint When
IamAuth (default) Not public. Lambda authorizes every request against IAM. Requests must be SigV4-signed and carry X-Amz-Content-SHA256, including a request with no body. The shape to deploy.
ApplicationManaged Public. Lambda enforces nothing; your code must authorize every request. Only on an explicit opt-in.

Choosing ApplicationManaged is an explicit opt-in and it is loud: aws-cdk-lib emits the documented warning at synthesis time, and a test pins that it reaches you through this construct.

WARNING WebFunction endpoint uses APPLICATION_MANAGED auth. The Lambda service will not enforce
authentication; ensure your handler code validates every request. (Construct Annotations)
   AppTheoryWebFunctionExample/Public/Function/Endpoint
   Acknowledge with 'Annotation::@aws-cdk/aws-lambda:webFunctionPublicEndpoint'

That warning is a signal, not a gate. It can be acknowledged — the Annotation:: key in the output above suppresses it — and an acknowledged warning is still an endpoint Lambda does not authenticate, so an endpoint that warns nothing has not thereby been shown to be private. Read the endpoint’s authType in the synthesized template: that is what the service acts on, and it is what this construct’s own assertions check.

Traffic between revisions

A new revision is created whenever the code, the runtime or the build configuration changes, and the endpoint sends all of its traffic to the revision this construct created. For a weighted split, the endpoint’s CfnWebFunctionEndpoint resource is exposed as the documented L1 escape hatch, because aws-cdk-lib’s WebFunctionTraffic only expresses single(revisionId):

web.endpoint.resource.revisionWeights = [
  { revisionId: priorRevisionId, weight: 90 },
  { revisionId: web.revisionId, weight: 10 },
];

What a revision ID is, and where the prior one comes from

A weight addresses a revision by ID, and every ID is produced by a deploy: web.revisionId is a value CloudFormation resolves, so the ID of the revision a previous deploy created is the one that deploy reported. Publish it as an output and read it back after the deploy.

Name the output after the function, because a stack that composes more than one Web Function has more than one revision ID and a split names the one whose endpoint you are weighting. The worked example composes two, and its two outputs are PrivateRevisionId and PublicRevisionId:

new CfnOutput(this, "PrivateRevisionId", { value: privateFunction.revisionId });
new CfnOutput(this, "PublicRevisionId", { value: publicFunction.revisionId });
aws cloudformation describe-stacks --stack-name <stack> \
  --query "Stacks[0].Outputs[?OutputKey=='PrivateRevisionId'].OutputValue" --output text

Swap in the output for the endpoint you are splitting — PrivateRevisionId for the IamAuth function, PublicRevisionId for the ApplicationManaged one.

Record that ID before the next deploy, not after it. The output is a live value: the deploy that supersedes a revision writes the new revision’s ID into it, so the ID that deploy replaced is afterwards in no output at all. The value you read now is the one a split names later, and it is the whole of a rollback: one entry, that ID, weight 100. Nothing recomputes it, so the recorded value is the record to keep.

A superseded revision is kept

A code or configuration change replaces the revision resource, and a revision the endpoint still routes weight to cannot be deleted: the service refuses with The revision rev-… cannot be deleted because the endpoints […] are using it. In a lab deploy on 2026-11-29 that refusal cost the update about 13 minutes of cleanup retries and left the revision serving 50% of traffic outside the stack.

So the revision resource carries a retain policy — DeletionPolicy: Retain and UpdateReplacePolicy: Retain — and a deploy that supersedes a revision leaves it in place: still serving any weight that addresses it, with the update’s cleanup finishing in one pass.

Retain means CloudFormation stops managing the revision; it does not mean the revision stays in the stack. A superseded revision is not deleted, and it stays available for a weighted split or a rollback, but it is no longer a stack-managed resource once the update supersedes it. That is the trade this construct makes: a revision a weight still routes to has to outlive the resource that created it.

The synthesis assertions pin those two attributes, so a template that schedules the prior revision for deletion fails the build instead of the deploy.

There is deliberately no property to change that policy. A revision a weight can address has to exist, so “delete the revision I just replaced” is the failure this retain exists to prevent, not an option — and the construct cannot see which revisions an endpoint’s weights address, because they are assigned on the L1 after composition.

How a superseded revision is removed

Deleting the function is this construct’s removal path, and the service removes all of the function’s revisions with it.

The lab observed that on the staging build that motivated this change — 832a7f07, which carried no retain policy, because the policy is what this change adds. On teardown there, cdk destroy deleted the function, the service removed the stack’s revision, and it also removed the prior revision the stack had already stopped managing; a web-function listing returned [] afterwards. That is a measurement of the function’s teardown on a build without retain, and nothing more.

It is not a measurement of a retained revision’s scope. A revision this construct retains after superseding it is a revision CloudFormation no longer manages, so its lifetime is not the stack’s to end, and this page does not assert when — or by what path — a retained revision is eventually cleaned up. Deleting the function is the construct’s removal path; that is not a claim that it is the only way a revision can disappear. The post-fix teardown, with retain in the template, is to be re-proved by Factory in a lab deploy.

The service accepts one entry with weight 100, or two entries summing to 100. AppTheory does not wrap this in a second API: the L1 property is the way, and it is what the synthesis assertion covers.

The managed code bucket outlives the stack

WebFunctionCode.fromAssetToManagedBucket() is the construct’s documented default and the path both worked examples use: aws-cdk-lib creates the bucket for you, copies each revision’s code into it, and empties and deletes that bucket with the stack. A retained revision’s code has to stay readable by the service, so aws-cdk-lib retains the bucket with the revision, and says so on every synthesis:

WARNING This revision is retained, so the code bucket AppTheoryWebFunctionExample/AWSCDKWebFunctionAssetBucket and its
policy are retained too, with the code of every retained revision in it. Deleting the stack leaves them in place, and
they are yours to clean up. To manage them yourself, stage the code with WebFunctionCode.fromAssetToProvidedBucket()
into a bucket you own. (Construct Annotations)
   AppTheoryWebFunctionExample/Private/Function/Revision
   Acknowledge with 'Annotation::@aws-cdk/aws-lambda:webFunctionManagedBucketRetained'

The warning is accurate, and it is one warning per Web Function: the example composes two, so it prints twice. It is a consequence of retaining revisions, not a defect in the composition, and acknowledging the annotation silences the message without changing what is left behind. This page does not acknowledge it.

cdk destroy leaves the code bucket behind. The stack deletes the function and its endpoint, and then stops. Left in place are the managed bucket, its bucket policy, the code object of every revision the stack created, and the custom resources that would otherwise have emptied the bucket and copied into it — all of them carry DeletionPolicy: Retain in the synthesized template. The usual “CDK empties the asset bucket for you” step does not happen here, precisely because the resource that performs it is retained too.

These are yours from then on, and they cost money until you remove them:

  • the managed bucket, holding the code archive of every revision the stack ever created;
  • its bucket policy.

Record the bucket’s physical name before you destroy the stack, because the stack that names it is what you are about to delete:

STACK=AppTheoryWebFunctionExample
BUCKET=$(aws cloudformation describe-stack-resources --stack-name "$STACK" \
  --query "StackResources[?ResourceType=='AWS::S3::Bucket'].PhysicalResourceId" --output text)

Then, after the destroy, empty and delete it. A code bucket has to be versioned — the construct refuses one that is not — so emptying it means deleting every object version and every delete marker, not just the current objects. delete-objects takes at most 1,000 keys per call and has no empty form: --query '{Objects: …}' writes {"Objects": null} when the page holds nothing of that kind, and delete-objects stops on that before it reaches S3. So the loop below counts a page before it deletes one, deletes only a page that came back with keys, and stops when the page is empty of both kinds. It runs unchanged in a shell that exits on the first error:

while :; do
  pending=$(aws s3api list-object-versions --bucket "$BUCKET" \
    --max-keys 1000 --query 'length(Versions || `[]`)' --output text)
  kind=Versions
  if [ "$pending" -eq 0 ]; then
    pending=$(aws s3api list-object-versions --bucket "$BUCKET" \
      --max-keys 1000 --query 'length(DeleteMarkers || `[]`)' --output text)
    kind=DeleteMarkers
  fi
  if [ "$pending" -eq 0 ]; then
    break
  fi

  aws s3api list-object-versions --bucket "$BUCKET" --max-keys 1000 \
    --query "{Objects: $kind[].{Key: Key, VersionId: VersionId}}" --output json > page.json
  aws s3api delete-objects --bucket "$BUCKET" --delete file://page.json
done

aws s3api delete-bucket --bucket "$BUCKET"

One page can hold versions and delete markers at once, so the loop deletes whichever kind the page it just listed has rather than draining one kind before looking at the other; it ends when a page holds neither, which is the only state in which the bucket is empty and delete-bucket can succeed. page.json is rewritten on every pass. The bucket policy goes with the bucket. If the stack is already gone and you did not record the name, the bucket still carries the stack’s generated prefix:

aws s3api list-buckets \
  --query "Buckets[?starts_with(Name, 'apptheorywebfunctionexample-awscdkwebfunctionassetbucket')].Name" --output text

If you would rather the bucket follow your own lifecycle, stage the code with WebFunctionCode.fromAssetToProvidedBucket() into a bucket you own. That is the same code prop — who owns the bucket is a deployment choice, not a second runtime contract — and the construct then emits no such warning. What it does not change is the revision policy: the revision stays retained either way.

Runtime support

Lambda Web Functions run managed language runtimes, and the set is smaller than for event functions. AppTheory composes what AWS supports and refuses the rest at synthesis rather than deploying a function that cannot start.

Runtime Upstream AppTheory
Node.js 24 (nodejs24.x) Supported Supported — WebFunctionRuntime.NODEJS_24_X
Node.js 26 (nodejs26.x) Supported Supported — WebFunctionRuntime.of("nodejs26.x")
Python 3.15 (python3.15) Public preview: not covered by the Lambda SLA or Technical Support, and not for production Supported as preview, with pythonApp naming the ASGI or WSGI application — WebFunctionRuntime.of("python3.15")
Go No Web Function runtime exists upstream N/A — no workaround, and no custom runtime

Python Web Functions are preview, and the label is upstream’s. python3.15 carries no SLA and no Technical Support, and AWS says it is not for production. AppTheory composes it because composing a supported execution model is how the framework stays the single path for it — not because the preview status has changed. Nothing on this page claims production support for Python Web Functions.

The Python model: Lambda’s server, your ASGI or WSGI application

A Python Web Function is not a def handler(event, context) function and is not served by AppTheory’s request/response Lambda entry point. Lambda manages the HTTP server and passes each request to an application through a declared interface:

Interface Concurrency in one environment AppTheory entry point
ASGI 3.0 One event loop. Requests are concurrent: while one awaits I/O, the loop serves another, and delivers another response’s chunks. apptheory.ASGIApp(runtime)
WSGI A pool of worker threads, one request per thread. apptheory.WSGIApp(runtime)

Both entry points are the same idea in two shapes: they turn a request into an AppTheory Request, call the runtime’s serve, and write the Response back — streaming a Response.body_stream chunk by chunk as it is produced, and releasing the producer when the client disconnects. Response.html_stream and apptheory.sse_event_stream therefore keep streaming over a Web Function instead of being buffered into one write.

Where your handler runs, and why it is safe there:

  • ASGI — the entry point is one async callable on the server’s single event loop. Each request’s synchronous serve call, and every blocking next() on a streamed body, runs on the loop’s default worker thread, which is the shape an ASGI framework gives a def endpoint and what upstream describes (“when you create a synchronous endpoint using def instead of async def, ASGI frameworks such as FastAPI and Starlette can run the endpoint in a thread pool”). The loop stays free to accept requests and deliver other responses while one handler works.
  • WSGI — the server calls the entry point on one of its worker threads, which is upstream’s WSGI model, and the handler runs there.

The runtime’s loop-bound state is the reason the ASGI entry point dispatches, not a workaround. serve() resolves an async def handler with asyncio.run, which raises when the calling thread already has a running loop. Every entry into serve from these two wrappers therefore happens off the loop, and an async def AppTheory handler keeps working unchanged. Per-request state itself is not loop-bound: it lives in the request’s Request, its Context, its handler’s return value and its body stream, and nothing in the wrappers is stored on a module, on the runtime or in a thread-local — so two overlapping requests cannot observe each other. py/tests/test_serving.py pins that (including an assertion that the handler never runs on the event-loop thread), and the example’s local row fails when a request reads another request’s state.

This is not the Managed Instances model. Lambda Managed Instances runs each concurrent Python invocation in a separate process; that is a different upstream feature, and Concurrency and Request Isolation states it as such. A Python Web Function keeps one event loop, or one worker-thread pool, inside one environment. The two models are not interchangeable, and neither of them serves Python with a thread per request in the sense of a thread-per-connection server you write yourself.

Why Go is N/A. Go is absent from the Web Function runtime list, exactly as it is absent from the Lambda Managed Instances runtime list. An AppTheory Go runtime is built for one invocation per environment because that is what AWS provides for Go. Supporting Go here would mean a custom runtime, and AppTheory does not ship or maintain runtime images. The gap is upstream, it is stated plainly, and it is not worked around: Concurrency and the upstream execution model.

Refusals, all of them at synthesis:

  • a runtime outside nodejs24.x, nodejs26.x and python3.15;
  • a Python runtime without pythonApp, or pythonApp with a Node.js runtime;
  • entryPoint with a Python runtime (a Python Web Function names an application, not a file);
  • a pythonApp.module or pythonApp.callable that carries a colon, which would smuggle in an interface prefix or a second entry point, and a pythonApp.interface outside asgi and wsgi;
  • AWS_LAMBDA_HTTP_ENDPOINT, AWS_LAMBDA_NODEJS_ENTRYPOINT or AWS_LAMBDA_PYTHON_ENTRYPOINT in props.environment;
  • a code bucket that is not versioned (from aws-cdk-lib).

What one environment must not do

One execution environment answers many requests — on one event loop for Node.js and ASGI Python, in a worker-thread pool for WSGI — so every request shares the process (and, on Python, the interpreter). Request state belongs to the request: its own closure, its own framework context, or its own request-scoped arguments, and never a module-level variable. AWS states the same rule for Python Web Functions (“avoid storing request-specific data in global variables”), and it is the rule AppTheory’s Context exists to make easy to follow. The guarantee and its per-language shape are stated in Concurrency and Request Isolation; the examples below are the Web Function instance of it, and each local row fails when a request reads another request’s state.

Prove it locally

Two examples serve a page and stream a response, and both run with no AWS credentials: examples/cdk/web-function for the Node.js model and examples/cdk/web-function-python for the Python one, which is one AppTheory application served once as an ASGI app and once as a WSGI app. Each is a row in the local harness, so it runs the way every other row does — see Local examples harness for prerequisites (Docker or Colima for DynamoDB Local, Node 24, Python 3.12+ with scripts/local/requirements-examples.txt installed, and no AWS credentials). To run only these rows:

python3 scripts/local/run_examples_local.py --only web-function --workers 1
python3 scripts/local/run_examples_local.py --only web-function-python,web-function-python-wsgi --workers 1

--only is the harness’s own debug filter and is never passed by CI; the full sweep is make examples-local.

Expected output:

  PASS  example ports are free (http 8151, aux 8152)
  [ 1/1] examples/cdk/web-function/app/server.mjs   L2  PASS  page 200 text/html, health 200,
         unknown route 404; 3 events over 0.37s, first at 0.00s, chunked; 2 streams overlapped
         0.49s of delivery, 4 + 5 chunks interleaved before either finished, each saw only its
         own tag

counts: 1 passed, 0 failed, 0 pending-milestone
RESULT: PASS

The Python rows report the same assertions against the two interfaces, each behind a real server:

  [ 1/2] examples/cdk/web-function-python/app/main.py        L2  PASS  page 200 text/html, health 200,
         unknown route 404; 3 events over 0.38s, first at 0.00s, chunked; 2 streams overlapped 0.51s
         of delivery, 5 + 4 chunks interleaved before either finished, each saw only its own tag
  [ 2/2] examples/cdk/web-function-python/app/main_wsgi.py   L2  PASS  page 200 text/html, health 200,
         unknown route 404; 3 events over 0.39s, first at 0.00s, chunked; 2 streams overlapped 0.50s
         of delivery, 4 + 5 chunks interleaved before either finished, each saw only its own tag

counts: 2 passed, 0 failed, 0 pending-milestone
RESULT: PASS

Neither row invents a server: ASGI is served by uvicorn (the reference ASGI server, one event loop) and WSGI by the standard library’s WSGI server with a threading mixin (one worker thread per request). Both are the local stand-ins for the server Lambda manages, and both are named as the row’s fidelity limit.

The applications can also be run directly. The Node.js one reads its listen address from the variable the service sets; the Python ones are called by a server, because that is what a Python Web Function is:

AWS_LAMBDA_HTTP_ENDPOINT=127.0.0.1:3000 node examples/cdk/web-function/app/server.mjs
curl -sS -D- http://127.0.0.1:3000/          # text/html page
curl -sS -N http://127.0.0.1:3000/stream     # chunked events, one every 120 ms
# ASGI, the way Lambda loads it: main:app
cd examples/cdk/web-function-python/app && PYTHONPATH=../../../../py/src python3 -m uvicorn main:app --port 3000
# WSGI, the way Lambda calls it: wsgi:main_wsgi:application
cd examples/cdk/web-function-python && PYTHONPATH=../../../py/src PORT=3000 python3 scripts/serve-wsgi.py

Both paths reach this checkout’s py/src, which is where the runtime lives until a deployment puts it in the archive root; the example’s README says the same thing and scripts/local/test_examples_local.py runs both lines as written, in an environment with no AppTheory installed.

The row asserts status codes, headers, incremental delivery (the first of three events arrives as soon as the response opens and the last about 0.37 s later, with no content-length to buffer it), and isolation between two concurrent requests in one environment.

Isolation is measured on delivery, not on the calls. Every chunk carries the instant it arrived, on one clock shared by both streams, so a server that answers the second request only after the first stream has finished does not pass for overlapping: the row fails unless each response delivered chunks while the other one was still open. The seconds and the interleave counts move between runs — a four-worker sweep of the same row reports 0.48 s and the same 4 + 5 — and what the row requires is at least two chunks from each stream before the other finishes. scripts/local/test_examples_local.py runs that assertion against three loopback control servers on every make examples-local: an interleaved one, which must pass, a serial one, and one that keeps the request tag in module state. The serial control fails the row with

the first stream delivered 0 chunk(s) before the second stream finished at 0.515s (its own first chunk
arrived at 0.515s), so the two responses were not delivered concurrently

Proof boundary

This page and both examples were verified locally: synthesis, the pinned template hashes, the focused construct assertions, and the harness rows above — including the Python rows, which drive the real ASGI and WSGI servers in this repository’s own Python runtime. Nothing was deployed, and no AWS credentials were used. Python Web Functions remain an upstream public preview; the local proof says the composition and the entry points behave, not that the preview is production-ready.

The example’s stack composes two functions, so both sides of the authorization decision appear in one synthesis: Private keeps the default IamAuth, and Public opts into ApplicationManaged, which Lambda does not authenticate. The second one is a synthesis demo — it shows what the opt-in looks like in a template — and it is not a decision to expose a service: the example’s application authenticates nothing. Anything you deploy should keep the IamAuth composition. The deploy step itself, theory app up with the stack as written, is not part of this proof.

Install the CDK package from a pinned GitHub Releases asset: Installation.