Skip to content

chore(tests): add LMI e2e suite and run-scoped shared capacity provider - #5465

Open
svozza wants to merge 23 commits into
mainfrom
feat/lmi-e2e-tests
Open

chore(tests): add LMI e2e suite and run-scoped shared capacity provider#5465
svozza wants to merge 23 commits into
mainfrom
feat/lmi-e2e-tests

Conversation

@svozza

@svozza svozza commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds an end-to-end test that validates Logger's InvokeStore-backed log-attribute isolation on Lambda Managed Instances (LMI), where multiple invocations run concurrently in the same execution environment. Also introduces the testing infrastructure to support it, including a run-scoped shared capacity provider so LMI suites don't each provision their own VPC + capacity provider.

Changes

  • Add a Logger LMI e2e suite (packages/logger/tests/e2e/lmi.test.ts) that saturates a deliberately small fleet, forces concurrent invocations to multiplex into shared execution environments, and asserts each invocation's appended keys stay isolated
  • Capture the handler's own log lines by teeing process.stdout.write (the production write path) and returning them in the response payload, making log collection deterministic — no CloudWatch polling or ingestion latency
  • Filter captured lines by the InvokeStore-scoped invocationKey, and additionally assert on function_request_id as a regression check now that the Lambda context is scoped per invocation under LMI (fix(logger): scope lambda context per invocation under LMI concurrency #5430)
  • Add TestLmiCapacityProvider (dual-stack IPv6-only VPC, egress-only IGW, CloudWatch Logs interface endpoint — no NAT) and extend TestNodejsFunction to attach a function to a capacity provider by construct or by ARN
  • Add packages/testing/src/lmi/cli.ts with deploy/destroy commands that manage a run-scoped LmiShared-<runId>-<arch> stack and print the capacity provider ARN, so a workflow run can provision one shared capacity provider per architecture up front and pass it to each suite via LMI_CAPACITY_PROVIDER_ARN (suites fall back to an ephemeral per-suite provider when unset)
  • Run the LMI suite unconditionally on every e2e dispatch (removes the RUN_LMI_TESTS gate and workflow input)

Issue number: closes #5518


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/XL PRs between 500-999 LOC, often PRs that grown with feedback label Jul 13, 2026
Comment thread packages/testing/src/lmi/cli.ts Fixed
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/XL PRs between 500-999 LOC, often PRs that grown with feedback and removed size/XL PRs between 500-999 LOC, often PRs that grown with feedback labels Jul 13, 2026
@sonarqubecloud

Copy link
Copy Markdown

@svozza
svozza requested a review from dreamorosi July 13, 2026 21:12
Comment thread packages/testing/src/lmi/cli.ts Outdated

@dreamorosi dreamorosi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are findings from a deep review pass, grouped as 2 blockers plus several smaller fixes. Happy to discuss any of them.

Comment thread packages/testing/src/lmi/deploySharedCapacityProvider.ts
Comment thread packages/testing/src/lmi/destroySharedCapacityProvider.ts
Comment thread .github/workflows/run-e2e-tests.yml
Comment thread .github/workflows/run-e2e-tests.yml
Comment thread .github/workflows/run-e2e-tests.yml
Comment thread packages/testing/src/resources/TestNodejsFunction.ts
Comment thread packages/logger/package.json Outdated
Comment thread packages/logger/tests/e2e/lmi.test.ts Outdated
Comment thread packages/testing/src/invokeTestFunction.ts Outdated

@dreamorosi dreamorosi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the long wait - I just finally found some time to review this now.

Some review comments that I think are valid, let me know if you disagree and/or I misunderstood anything.

@dreamorosi

Copy link
Copy Markdown
Contributor

The infrastructure-leak concern raised in review—cancelled cells leaving functions attached to the shared capacity provider and causing DELETE_FAILED during teardown—will be backstopped by the scheduled sweeper tracked in #5519. Within this PR, it would be good to keep the scope to job timeout-minutes and a retry/second pass in the destroy script. Before merge, a fresh full e2e workflow run against the current head SHA would also be good, since the last green run (29740823390) predates the newest commits and the merge from main.

@dreamorosi

Copy link
Copy Markdown
Contributor

The temporary orphan-cleanup experiment succeeded in the latest e2e run: all four LMI cells passed, teardown found and deleted all four deliberately orphaned function stacks, and both shared capacity provider stacks were then deleted successfully. The workflow was red overall because four non-LMI matrix cells failed, but the targeted LMI teardown path completed successfully.

Before this is merge-ready, please:

  • revert the temporary f0faefd8 commit that sets DISABLE_TEARDOWN
  • bring the branch up to date with main and resolve the current conflicts in package-lock.json and packages/testing/package.json
  • run the final checks against the resulting head

I have resolved the review threads whose requested changes are now present. I am leaving the requested-changes review in place until the temporary commit and conflicts are cleared.

@svozza

svozza commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

Yeah, we're not finished yet, I think the no clobber change is causing unrelated tests to fail. I'm going to be doing multiple CI runs to diagnose this so we can't merge until that's done anyway.

svozza added 23 commits August 6, 2026 22:35
… Managed Instances

Adds an opt-in (RUN_LMI_TESTS) e2e suite that provisions an ephemeral
Lambda Managed Instances capacity provider (dual-stack IPv6 VPC, no NAT,
CloudWatch Logs interface endpoint) and proves the InvokeStore-backed
log attribute isolation across invocations multiplexed into the same
execution environment via a module-scoped promise barrier.

Testing utils changes:
- TestLmiCapacityProvider construct + ./resources/capacity-provider export
- ExtraTestProps.lmi option on TestNodejsFunction (memorySize>=2048 floor,
  $LATEST.PUBLISHED qualified output)
- includeTailLogs opt-out on invoke helpers (Tail unsupported on LMI)

Related to #5092, findings posted on the issue.
… CloudWatch polling

The handler tees process.stdout.write (the Logger's production write
path) and returns its own log lines in the response payload, making log
collection deterministic — no FilterLogEvents polling, no ingestion
latency, no CloudWatch client in the test.

Captured lines are filtered by the InvokeStore-scoped invocationKey
rather than function_request_id: addContext stores the Lambda context
in instance state, so under LMI multiplexing the request id stamped on
log lines can belong to a different invocation. Tracked separately as
a bug (see lmi-request-id-bug-handoff.md); once fixed, the filter
should flip back to function_request_id as a regression check.
…ogs by request id

With the lambda context now scoped per invocation under LMI (#5430),
the handler selects its own log lines by function_request_id — an
independent per-invocation attribute — and the test asserts the
InvokeStore-scoped invocationKey on those lines, making the suite a
regression check for both the context scoping and appendKeys isolation.

The suite adds ~4 minutes to the logger e2e cell, inside the agreed
budget for running on every e2e dispatch, so the RUN_LMI_TESTS gate
and workflow input are removed.
The e2e suites are I/O-bound (waiting on CloudFormation), but vitest
sizes its worker pool from CPU cores, so on 2-core CI runners only 3 of
the 6 logger e2e files ran concurrently and the rest queued — each then
paying its own stack deploy after waiting. With 8 workers all suites
deploy their stacks up front and the cell duration approaches the
slowest single suite (~6 min measured) instead of a serialized ~8 min.

CloudFormation read-API pressure from the extra concurrent stack
monitors is bounded by the existing DescribeStackEvents polling patch
in the testing package (10s interval per stack).
With all 36 matrix cells sharing one account, the extra concurrent
stack operations from 8-worker logger cells pushed the account-wide
CloudFormation API rate over the edge: three unrelated cells failed
with 'Throttling: Rate exceeded' on stack deploys. Back to default
worker sizing; cell-duration work moves to the run-scoped shared
capacity provider follow-up, which removes per-cell VPC/CP stacks
entirely instead of racing them.
Instead of every LMI suite provisioning its own capacity provider + VPC,
a workflow run can deploy one shared capacity provider per architecture up
front and pass its ARN to each suite via LMI_CAPACITY_PROVIDER_ARN.

- add packages/testing/src/lmi/cli.ts with deploy/destroy commands that
  manage a run-scoped LmiShared-<runId>-<arch> stack and print the
  capacity provider ARN on stdout
- TestNodejsFunction accepts a capacity provider ARN string and attaches
  the function via L1 CfnFunction.capacityProviderConfig (an imported
  capacity provider has no addFunction)
- ExtraTestProps.lmi.capacityProvider widened to CapacityProvider | string
- TestLmiCapacityProvider ctor loosened to Pick<TestStack, 'stack'> so the
  CLI can build the stack standalone
- logger lmi.test.ts reads LMI_CAPACITY_PROVIDER_ARN, falling back to an
  ephemeral per-suite capacity provider when unset
The --run-id argument flows into a CloudFormation stack name and, via
join(tmpdir(), ...), into the assembly output path that is later read
back. A crafted value (e.g. containing ../) could escape tmpdir(), so
restrict it to the alphanumerics-and-hyphens set CloudFormation already
requires for stack names and reject anything else at the boundary.
The path-injection scanner's taint analysis doesn't recognise the
run-id regex guard as sanitisation, and its guidance is to validate the
constructed path before touching the file system. Resolve the assembly
output directory and assert it stays within os.tmpdir() before it is
written to or read back, keeping the run-id validation as the input-side
guard for defence in depth.
The constructor's cognitive complexity hit 17 (limit 15) once it carried
the nested ARN-vs-construct capacity-provider branching. Move that logic
into two private methods so the constructor keeps a flat structure and
the two attachment strategies read independently. No behaviour change.
Two TestStack instances synthesizing at once (the shared LMI capacity
provider stacks, one per architecture, deployed concurrently) both run
fromAssemblyBuilder, which by default temporarily replaces the global
process.env with an immutable proxy for the synth window. Interleaved
synth windows race on that swap. Pass clobberEnv: false; the assembly
builder does not read the injected env, so opting out is safe.
A function attached to a shared capacity provider by ARN keeps the
provider's ENIs in use, and that cross-stack link is invisible to
CloudFormation, so a function stack orphaned by a cancelled or timed-out
cell makes the provider-stack delete fail with DELETE_FAILED and leaks an
EC2 fleet plus VPC.

- embed an Lmi marker and the run id in the LMI function stack name so
  the teardown job can attribute and find orphans (see lmi/naming.ts)
- teardown now sweeps leftover Lmi-<runId> function stacks and waits for
  them to be gone before deleting the provider stacks, and retries the
  provider teardown once to absorb throttling and ENI detachment
- add timeout-minutes: 60 to the three LMI jobs so a hung deploy/cell
  can't keep EC2 fleets alive for the 6-hour default
- fail the ARN resolution step when describe-stacks returns empty/None
  instead of silently falling back to per-suite provisioning

Residual leak paths (workflow-level cancellation, DISABLE_TEARDOWN,
LmiShared-local-*) are tracked as the scheduled sweeper in #5519.
The ARN attach path sets publishToLatestPublished on the L1 resource
explicitly; the construct path relied on CDK's undocumented default. Set
it on both so the two paths emit identical templates and the behaviour
doesn't depend on an undocumented service default.
The option was added so the LMI suite could opt out of Tail logs (Tail is
unsupported on capacity-provider functions), but the suite now invokes
directly and collects logs from the response payload, leaving the option
uncalled. Remove it and document the Tail constraint on invokeFunctionOnce
so it isn't reintroduced without the guard.
Drop @aws-sdk/client-cloudwatch-logs (unused since log capture moved off
CloudWatch polling) and declare @aws-sdk/client-lambda, which the LMI e2e
suite imports directly and previously resolved only via workspace hoisting.
The progress lines used testName, which is run through
generateTestUniqueName and so mislabels stacks created from an
explicitly-named Stack object — the shared LMI provider stack logged as
`LmiShared-24-x86-<uuid>-arm64` instead of its real
`LmiShared-<runId>-<arch>` name. Log stack.stackName, the actual
deployed name, which is correct for both generated and explicit names.
@svozza

svozza commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @dreamorosi — since you've already resolved the threads from the commits, just closing the loop with everything that landed:

Blockers

  • Concurrent-synth process.env raceclobberEnv: false in TestStack.#synthAssembly() (covers both the deploy and destroy scripts).
  • Teardown leak on cancelled/timed-out cells — LMI function stacks now carry an Lmi-<runId> marker; the teardown job sweeps leftover function stacks and waits for them to delete before the provider stacks (so ENIs are detached and the provider delete can't DELETE_FAILED), with a retry pass. Residual cases (workflow-level cancellation, DISABLE_TEARDOWN, LmiShared-local-*) tracked in Maintenance: Add a scheduled sweeper for leaked e2e infrastructure #5519.

Should-fixes

  • timeout-minutes: 60 on all three LMI jobs.
  • ARN resolution now fails loudly on empty/None instead of silently falling back to per-suite provisioning.
  • publishToLatestPublished set explicitly on the construct attach path so both paths emit identical templates.
  • Logger deps: dropped unused @aws-sdk/client-cloudwatch-logs, declared @aws-sdk/client-lambda.
  • Multiplexing comment in lmi.test.ts rewritten to match observed behaviour (barrier guarantees the overlap; env counts vary and aren't asserted).
  • Removed the unused includeTailLogs option, with a comment on invokeFunctionOnce noting Tail is unsupported on capacity-provider functions.

Validation

  • Teardown sweep proven end-to-end in real CI (a run with teardown skipped on every LMI cell left 4 orphans; the sweep found and deleted all 4, then both providers deleted cleanly with the real teardown role) and reproduced locally against real capacity-provider infra.
  • Happy-path LMI run: setup + all 4 LMI cells + teardown green, zero throttling.

⚠️ Not ready to merge yet
While validating I hit non-LMI cells failing with NoStack: CloudFormationStack object does not hold a stack. It's not from this PR — it reproduces on a clean main (run 31155239957, 6/40 cells) and is independent of the clobberEnv change (confirmed by a controlled run with it reverted). Filed as #5537. I'd like to hold this PR until #5537 is resolved, otherwise the branch's CI stays red on unrelated flakes and we can't get a clean signal that the LMI work is green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XXL PRs with 1K+ LOC, largely documentation related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Maintenance: Add Logger E2E tests for Lambda Managed Instances

3 participants