amplify-backend icon indicating copy to clipboard operation
amplify-backend copied to clipboard

Improve/expose helpful error messaging from function build logs by default

Open treewhopper opened this issue 1 year ago • 4 comments

Environment information

System:
  OS: Linux 5.15 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
  CPU: (32) x64 13th Gen Intel(R) Core(TM) i9-13900K
  Memory: 42.51 GB / 47.04 GB
  Shell: /bin/bash
Binaries:
  Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
  Yarn: 1.22.22 - ~/.nvm/versions/node/v20.9.0/bin/yarn
  npm: 10.1.0 - ~/.nvm/versions/node/v20.9.0/bin/npm
  pnpm: 9.3.0 - ~/.nvm/versions/node/v20.9.0/bin/pnpm
NPM Packages:
  @aws-amplify/backend: 1.0.4
  @aws-amplify/backend-cli: 1.2.0
  aws-amplify: 6.4.3
  aws-cdk: 2.150.0
  aws-cdk-lib: 2.150.0
  typescript: 5.4.5
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Description

When importing the typed env vars for a function, ie import { env } from '$amplify/env/[function name]'; it's possible to import the environment for another function. This happened to me by accident when I copied one function folder as the template for a new function.

In some cases, this will result in a branch build failure. For me, the sandbox build went smoothly, so I could not determine where the issue was occurring from the error message presented in the build logs, which looked like:

233 2024-07-24T15:41:59.512Z [INFO]: NodeJSFunctionConstructInitializationError: Failed to instantiate nodejs function construct
234 Resolution: See the underlying error message for more details.
235 Cause: Failed to bundle asset amplify-[appid]-develop-branch-fa6d7c9088/function/[function name]/Code/Stage, bundle output is located at /codebuild/output/src2565109353/src/[app name]/.amplify/artifacts/cdk.out/bundling-temp-34810b71819b8ed1141ca70264cce619498c6f7893cb552751a08df7d54d1329-error: Error: bash -c npx --no-install esbuild --bundle "/codebuild/output/src2565109353/src/[app name]/amplify/functions/[function name]/handler.ts" --target=node20 --platform=node --format=esm --outfile="/codebuild/output/src2565109353/src/[app name]/.amplify/artifacts/cdk.out/bundling-temp-34810b71819b8ed1141ca70264cce619498c6f7893cb552751a08df7d54d1329/index.mjs" --minify --sourcemap --loader:.node=file --banner:js="/** * Reads SSM environment context from a known Amplify environment variable, * fetches values from SSM and places those values in the corresponding environment variables */export const internalAmplifyFunctionResolveSsmParams = async (client) => { const envPathObject = JSON.parse(process.env.AMPLIFY_SSM_ENV_CONFIG ?? '{}'); const paths = Object.keys(envPathObject); if (paths.length === 0) { return; } let actualSsmClient; if (client) { actualSsmClient = client; } else { const ssmSdk = await import('@aws-sdk/client-ssm'); actualSsmClient = new ssmSdk.SSM(); } const resolveSecrets = async (paths) => { const response = await actualSsmClient.getParameters({ Names: paths, WithDecryption: true, }); if (response.Parameters && response.Parameters.length > 0) { for (const parameter of response.Parameters) { if (parameter.Name) { const envKey = Object.keys(envPathObject).find((key) => envPathObject[key].sharedPath === parameter.Name); const envName = envKey ? envPathObject[envKey].name : envPathObject[parameter.Name]?.name; process.env[envName] = parameter.Value; } } } return response; }; const response = await resolveSecrets(paths); const sharedPaths = (response?.InvalidParameters || []) .map((invalidParam) => envPathObject[invalidParam].sharedPath) .filter((sharedParam) => !!sharedParam); if (sharedPaths.length > 0) { await resolveSecrets(sharedPaths); }};await internalAmplifyFunctionResolveSsmParams();const SSM_PARAMETER_REFRESH_MS = 1000 * 60;setInterval(() => { void internalAmplifyFunctionResolveSsmParams();}, SSM_PARAMETER_REFRESH_MS);export {};" --inject:"/codebuild/output/src2565109353/src/[app name]/node_modules/@aws-amplify/backend-function/lib/lambda-shims/cjs_shim.js" run in directory /codebuild/output/src2565109353/src/[app name] exited with status 1

By adding in --debug to my pipeline-deploy arguments in amplify.yml, I was able to determine the issue:

2024-07-24T20:30:51.807Z [INFO]: [DEBUG] 2024-07-24T20:30:51.807Z: Bundling asset amplify-[appid]-develop-branch-fa6d7c9088/function/[function name]-lambda/Code/Stage...
181 2024-07-24T20:30:52.218Z [INFO]: [DEBUG] 2024-07-24T20:30:52.218Z: ✘ [ERROR] Could not resolve "$amplify/env/[WRONG FUNCTION NAME]"
182 amplify/functions/[function name]/handler.ts:2:20:
183 2 │ import { env } from '$amplify/env/[WRONG FUNCTION NAME]';
184 ╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
185 You can mark the path "$amplify/env/[WRONG FUNCTION NAME]" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle.
186 2024-07-24T20:30:52.219Z [INFO]:
187 2024-07-24T20:30:52.344Z [INFO]: [DEBUG] 2024-07-24T20:30:52.344Z: 1 error
188 2024-07-24T20:30:52.345Z [INFO]:
189 2024-07-24T20:30:52.359Z [INFO]: [DEBUG] 2024-07-24T20:30:52.359Z: /codebuild/output/src2785554619/src/[app name]/node_modules/@aws-amplify/backend-function/src/factory.ts:328

My request is that this very helpful error messaging be exposed to the default log.

Thanks!

treewhopper avatar Jul 24 '24 23:07 treewhopper

Hey @treewhopper :wave: thanks for taking the time to file this! I've marked this as a feature request to improve the messaging here 🙂

josefaidt avatar Jul 24 '24 23:07 josefaidt

Has there been any progress on this? Just ran into the same issue today, and wouldn't have had any idea where to look without this thread.

ColtonGlasgow13 avatar Sep 16 '24 13:09 ColtonGlasgow13

Is anyone currently working on implementing this issue? If there isn’t an existing PR submitted, I would like to take it on. Would that be alright?

tttol avatar Oct 17 '24 23:10 tttol

Just ran into this myself. With several lambda functions in my project I woudln't have found the issue without this comment.

mtliendo avatar Feb 25 '25 05:02 mtliendo

This was resolved with https://github.com/aws-amplify/amplify-backend/pull/2503 let us know if you are still facing this issue.

Amplifiyer avatar Jun 16 '25 17:06 Amplifiyer