fix(gen2-migration): fix lambda authorizer import and REGION hardcode - REVIEW LAST#14852
Draft
fix(gen2-migration): fix lambda authorizer import and REGION hardcode - REVIEW LAST#14852
Conversation
…14813) Two bugs in the Gen1-to-Gen2 migration generate step: 1. Lambda authorizer import: When a GraphQL API uses a Lambda authorizer as an additional auth mode, the generated data/resource.ts references the function variable in lambdaAuthorizationMode without importing it. Added extractLambdaFunctionName() to DataRenderer which reads the authorizationModes config and emits the missing import. 2. REGION env var hardcode: The generated function/resource.ts files hardcode REGION to the literal resolved value (e.g. 'us-east-1') instead of resolving dynamically. Since AWS Lambda automatically provides process.env.AWS_REGION, the REGION env var is now omitted entirely from the generated defineFunction() environment block. Tests: updated inline snapshot for data.generator.test.ts, added two new tests for REGION omission in function.generator.test.ts, updated 18 golden migration app snapshot files. --- Prompt: Fix issue #14813 — lambda authorizer function reference in data/resource.ts is missing its import statement, and REGION environment variable is hardcoded to 'us-east-1' instead of being resolved dynamically.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
NOTE TO ME: To merge this, first rebase onto both #14487 and #14517 first, then run
UPDATE_SNAPSHOTS=1 yarn testto reconcile snapshots and resolve test file conflictsSolves #14813.
Issue Summary
Two bugs in the Gen1-to-Gen2 migration
generatestep: (1) when a GraphQL API uses a Lambda authorizer, the generateddata/resource.tsreferences the function variable without importing it, and (2) the generatedfunction/resource.tsfiles hardcode theREGIONenvironment variable to the literal resolved value (e.g.'us-east-1') instead of resolving it dynamically. Since existing Gen 1 function code readsprocess.env.REGIONat runtime, dropping it entirely would break those functions — so the fix emitsREGION: process.env.AWS_REGION ?? ''to resolve dynamically from the Lambda-injectedAWS_REGION.Reasoning
data/resource.tsemitslambdaAuthorizationMode: { function: myAuthFunc }but never importsmyAuthFunc. The function variable is defined in../function/myAuthFunc/resource.tsand needs an explicit import.data.renderer.ts— foundrender()which builds the import list but had no logic to inspect authorization modes for Lambda function references.extractLambdaFunctionName()toDataRendererwhich reads theauthorizationModesconfig, checks bothdefaultAuthenticationandadditionalAuthenticationProvidersforAWS_LAMBDAtype, and returns the function name. Therender()method now emits the missing import when a Lambda authorizer is found.renderEnvironment()method infunction.renderer.tswas passingREGIONthrough as a literal string (REGION: 'us-east-1'). The initial fix dropped it entirely, reasoning thatAWS_REGIONis auto-injected by Lambda. But Gen 1 function code (e.g.new CognitoIdentityProviderClient({ region: process.env.REGION })) readsprocess.env.REGION, notprocess.env.AWS_REGION— so dropping it would cause those SDK clients to receiveundefinedfor the region.REGION: process.env.AWS_REGION ?? ''— a dynamic expression that resolves at runtime from the Lambda-injectedAWS_REGION, keeping backward compatibility with existing function code.Solution
data.renderer.ts— AddedextractLambdaFunctionName()that inspects authorization modes for Lambda authorizer config and returns the function name.render()now emits an import for the Lambda function when present.data.generator.test.ts— Updated inline snapshot for the Lambda auth mode test to verify the import is emitted.function.renderer.ts— Changed theREGIONhandling fromif (key === 'REGION') continue(skip entirely) to emittingREGION: process.env.AWS_REGION ?? ''. This generates a dynamic reference instead of a hardcoded literal or omission.function.generator.test.ts— Updated two REGION tests: one verifyingREGIONrenders asprocess.env.AWS_REGION ?? ''alongside other env vars, and one verifying the environment block is still emitted whenREGIONis the only env var.function/*/resource.tssnapshots to reflect the new dynamic REGION output (REGION: process.env.AWS_REGION ?? ''instead ofREGION: 'us-east-1').Example
Input (Gen 1 — deployed function config):
{ "FunctionName": "admin-main-abc", "Environment": { "Variables": { "ENV": "main", "REGION": "us-east-1" } } }Output — before fix (resource.ts):
Output — after fix (resource.ts):