Skip to content

fix(cli-internal): add lambda authorizer import and use dynamic region in data resource#14894

Open
adrianjoshua-strutt wants to merge 3 commits into
devfrom
fix/lambda-authorizer-auth-14813
Open

fix(cli-internal): add lambda authorizer import and use dynamic region in data resource#14894
adrianjoshua-strutt wants to merge 3 commits into
devfrom
fix/lambda-authorizer-auth-14813

Conversation

@adrianjoshua-strutt
Copy link
Copy Markdown
Member

Description

Fixes #14813.

Two bugs in the gen2-migration code generator that caused build failures when migrating Gen1 apps with Lambda authorizer auth modes:

  1. Missing import for Lambda authorizer function — The generated data/resource.ts referenced the Lambda function identifier (e.g., myAuthFn) without emitting an import statement, producing a TypeScript compilation error.

  2. Hardcoded REGION environment variable — The REGION env var was placed in defineFunction()'s static environment object with the Gen1 deployment region (e.g., us-east-1). This is incorrect for Gen2 where the deployment region may differ from the original Gen1 region.

Changes

Data resource generation

DataRenderer now accepts an optional lambdaAuthFunction reference describing the function name and relative import path. When present, it adds the import to the generated data/resource.ts.

DataGenerator extracts the Lambda function name from authorizationModes (searching both defaultAuthentication and additionalAuthenticationProviders) and passes it to the renderer.

Dynamic REGION resolution

FunctionGenerator.resolve() now intercepts the REGION key after classifyEnvVars() runs. Instead of leaving it as a retained (hardcoded) env var, it moves REGION to an escape hatch that emits backend.{funcName}.stack.region — the CDK token that resolves to the actual deployment region at synth time.

Testing

  • Unit tests: data.renderer.test.ts updated with Lambda auth import assertion
  • Integration snapshots: All 9 fixture apps regenerated and verified (12 snapshot tests pass)
  • Function generator tests: 19 tests pass
  • Full test suite: 55 tests across 5 suites pass

@adrianjoshua-strutt adrianjoshua-strutt marked this pull request as ready for review May 18, 2026 17:22
@adrianjoshua-strutt adrianjoshua-strutt requested a review from a team as a code owner May 18, 2026 17:22
@adrianjoshua-strutt adrianjoshua-strutt changed the base branch from gen2-migration to dev May 18, 2026 17:40
@adrianjoshua-strutt adrianjoshua-strutt force-pushed the fix/lambda-authorizer-auth-14813 branch from 8c3c551 to 076e83f Compare May 18, 2026 21:08
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- untyped authConfig providers array
function findLambdaFunctionNameInProviders(providers: any[] | undefined): string | undefined {
if (!providers) return undefined;
for (const provider of providers) {
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.

question: Is it possible to have multiple Lambda providers?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

AppSync supports only one Lambda authorizer per API, but we iterate defensively in case the config structure has multiple providers listed

const finalDynamicEnvVars = [...dynamicEnvVars];
if ('REGION' in finalLiteralEnvVars) {
delete finalLiteralEnvVars.REGION;
finalDynamicEnvVars.push({
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.

Can we have tests for this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added region tests

@adrianjoshua-strutt adrianjoshua-strutt self-assigned this May 19, 2026
@adrianjoshua-strutt adrianjoshua-strutt force-pushed the fix/lambda-authorizer-auth-14813 branch 3 times, most recently from a664cc1 to a1dd4e1 Compare May 21, 2026 17:50
…n in data resource

Add lambda authorizer function import to data/resource.ts when the
auth config uses AWS_LAMBDA authentication. Also move REGION env var
from static literal to a dynamic CDK stack.region reference.

---
Prompt: Fix lambda authorizer auth config and dynamic region in
gen2-migration data and function generators.
@adrianjoshua-strutt adrianjoshua-strutt force-pushed the fix/lambda-authorizer-auth-14813 branch from a1dd4e1 to 6e691d1 Compare May 26, 2026 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(gen2-migration): generate has bug in lambda authorizer for additional auth mode

2 participants