#✅ - Gen 2 SSM parameter injection to Function crashes in branch builds

24 messages · Page 1 of 1 (latest)

hollow dragon
#

For some reason, one of my functions refuses to build in branch builds. It works in sandbox mode, including completely fresh sandbox builds.

The error reported appears to have something to do with injecting the SSM secrets helper into this function. Of note, I am not using any secrets with this function. I've tried removing the function, then readding it to the project, and I still get the same error. Has anyone seen this, or do you have any ideas for what might be causing the bundle injection to fail?

Thanks!

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
rich jay
#

👋 do you have other Functions that build okay? this looks more related to the build itself

Error: bash -c npx --no-install esbuild --bundle 
#

is this in a monorepo setup where the esbuild bin may be in a different location than the root or the subpackage?

hollow dragon
#

hmm no this is a normal repo

#

all other functions build okay

rich jay
#

can you try adding --debug to the deployment command?

hollow dragon
#

yes

#

to pipeline-deploy?

rich jay
#

yep!

#

(I'm also taking a note of this error message to make improvements)

hollow dragon
#

okay, just kicked that off

#

sounds like we have a meeting tomorrow? i'm looking forward to seeing what you think of our various Amplify projects. i'll try to have something good to show you

#

okay very helpful to have this new debug info

#

found one problem so far, one of my functions env imports was pointed at another functions env name
ie. import { env } from '$amplify/env/[function name]';

running it again now. this error wasn't fully exposed to me without --debug, and the sandbox probably has all of those env helpers available from previous runs.

#

yeah i think that was it, the build is still running, but it's past that issue. thank you!

#

copy pasta issue on my end

magic hamletBOT
#

✅ - Gen 2 SSM parameter injection to Function crashes in branch builds

#

Answer selected!

ie. `import { env } from '$amplify/env/[function name]';`

running it again now. this error wasn't fully exposed to me without --debug, and the sandbox probably has all of those env helpers available from previous runs.```
Kudos to @hollow dragon!
[#1265749610924609596 message](/guild/705853757799399426/thread/1265749610924609596/p/1265749610924609596/#msg-1265769709295505479)
rich jay
rich jay
hollow dragon
#

feature request? this was not a bug on ampx, just a typo on my side that was hard to track down

rich jay
#

either is fine I think the templates just have slightly different language on the inputs

#

we can track it as a feature request to improve the messaging