Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue bundling custom function: Failed to instantiate nodejs function construct #2484

Open
3 tasks done
mgreiner79 opened this issue Feb 5, 2025 · 3 comments
Open
3 tasks done
Labels
pending-triage Incoming issues that need categorization sandbox Related to the sandbox experience

Comments

@mgreiner79
Copy link

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

Not applicable

Amplify Version

v6

Amplify Categories

function

Backend

Amplify Gen 2

Environment information

 System:
    OS: Windows 11 10.0.22621
    CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
    Memory: 14.62 GB / 31.73 GB
  Binaries:
    Node: 20.18.2 - C:\Program Files\nodejs\node.EXE
    npm: 10.8.2 - C:\Program Files\nodejs\npm.CMD
  Browsers: {}
  npmPackages:
    %name%:  0.1.0 
    @aws-amplify/backend: ^1.5.0 => 1.5.0 
    @aws-amplify/backend-cli: ^1.2.9 => 1.2.9 
    @aws-amplify/ui-react: ^6.5.5 => 6.5.5 
    @aws-amplify/ui-react-internal:  undefined ()
    @aws-amplify/ui-react-server:  undefined ()
    @emotion/react: ^11.14.0 => 11.14.0 
    @emotion/styled: ^11.14.0 => 11.14.0 
    @mui/icons-material: ^6.4.2 => 6.4.2 
    @mui/material: ^6.4.2 => 6.4.2 
    @types/react: ^18.2.66 => 18.3.12 
    @types/react-dom: ^18.2.22 => 18.3.1 
    @typescript-eslint/eslint-plugin: ^7.2.0 => 7.18.0 
    @typescript-eslint/parser: ^7.2.0 => 7.18.0 
    @vitejs/plugin-react: ^4.2.1 => 4.3.3 
    aws-amplify: ^6.6.6 => 6.6.6 
    aws-amplify/adapter-core:  undefined ()
    aws-amplify/analytics:  undefined ()
    aws-amplify/analytics/kinesis:  undefined ()
    aws-amplify/analytics/kinesis-firehose:  undefined ()
    aws-amplify/analytics/personalize:  undefined ()
    aws-amplify/analytics/pinpoint:  undefined ()
    aws-amplify/api:  undefined ()
    aws-amplify/api/server:  undefined ()
    aws-amplify/auth:  undefined ()
    aws-amplify/auth/cognito:  undefined ()
    aws-amplify/auth/cognito/server:  undefined ()
    aws-amplify/auth/enable-oauth-listener:  undefined ()
    aws-amplify/auth/server:  undefined ()
    aws-amplify/data:  undefined ()
    aws-amplify/data/server:  undefined ()
    aws-amplify/datastore:  undefined ()
    aws-amplify/in-app-messaging:  undefined ()
    aws-amplify/in-app-messaging/pinpoint:  undefined ()
    aws-amplify/push-notifications:  undefined ()
    aws-amplify/push-notifications/pinpoint:  undefined ()
    aws-amplify/storage:  undefined ()
    aws-amplify/storage/s3:  undefined ()
    aws-amplify/storage/s3/server:  undefined ()
    aws-amplify/storage/server:  undefined ()
    aws-amplify/utils:  undefined ()
    aws-cdk: ^2.138.0 => 2.177.0
    aws-cdk-lib: ^2.138.0 => 2.163.1
    constructs: ^10.3.0 => 10.4.2
    esbuild: ^0.20.2 => 0.20.2 (0.23.1, 0.21.5)
    eslint: ^8.57.0 => 8.57.1
    eslint-plugin-react-hooks: ^4.6.0 => 4.6.2
    eslint-plugin-react-refresh: ^0.4.6 => 0.4.13
    openai: ^4.82.0 => 4.82.0
    react: ^18.2.0 => 18.3.1
    react-dom: ^18.2.0 => 18.3.1
    react-router-dom: ^7.1.5 => 7.1.5
    tsx: ^4.7.2 => 4.19.1
    typescript: ^5.4.5 => 5.6.3 (4.4.4, 4.9.5)
    vite: ^5.4.10 => 5.4.10
  npmGlobalPackages:
    @aws-amplify/cli: 12.14.2
    aws-cdk: 2.177.0
    typescript: 5.7.3

Describe the bug

When deploying a basic function to sandbox, the following error occurs:
"""
Failed to instantiate nodejs function construct
Caused By: Failed to bundle asset amplify-amplifyvitereacttemplate-<MY_SANDBOX>/function/test-function-lambda/Code/Stage, bundle output is located at <MY_PROJECT_FOLDER>amplify\artifacts\cdk.out\bundling-temp-49f5fc225f5f9c84d79ef6eb8c561146ce3a8f455642ad6758130b26a30884b3-error: Error: spawnSync cmd ENOENT
"""

Expected behavior

I am trying to implement the basic "hello world" exampl for implementing a custom function, as shown in the amplify gen2 docs.

It is expected that the "hello world" example, in a fresh Amplify template project, should successfully deploy the lambda function to the sandbox environment.

Reproduction steps

  1. Generate a template project using this template
  2. create the files for a function, an handler and modify the data schema (as shown in the code snippet section)
  3. Run npx ampx sandbox --debug

Code Snippet

// amplify\functions\testFunction\resources.ts
import { defineFunction } from '@aws-amplify/backend';

export const testFunction = defineFunction({
  name: 'test-function',
  entry: './handler.ts',
});

// amplify\functions\testFunction\handler.ts

import type { Schema } from '../../data/resource';

export const handler: Schema['testFunction']['functionHandler'] = async (
  event,
) => {
  const { name } = event.arguments;
  return `Hello, ${name}!`;
};

// amplify\data\resource.ts

import { type ClientSchema, a, defineData } from '@aws-amplify/backend';
import { testFunction } from '../functions/testFunction/resource';

const schema = a.schema({
  testFunction: a
    .query()
    .arguments({
      name: a.string(),
    })
    .returns(a.string())
    .handler(a.handler.function(testFunction))
    .authorization((allow) => [allow.authenticated()])
});

export type Schema = ClientSchema<typeof schema>;

export const data = defineData({
  schema,
  authorizationModes: {
    defaultAuthorizationMode: 'userPool',
    apiKeyAuthorizationMode: {
      expiresInDays: 30,
    },
  },
});

Log output

[DEBUG] 2025-02-05T22:18:08.462Z: [Sandbox] Executing command `deploy`
[DEBUG] 2025-02-05T22:18:14.445Z: WARNING: owners may reassign ownership for the following model(s) and role(s): Quiz: [owner], QuizAttempt: [owner]. If this is not intentional, you may want to apply field-level authorization rules to these fields. To read more: https://docs.amplify.aws/cli/graphql/authorization-rules/#per-user--owner-based-data-access.

[DEBUG] 2025-02-05T22:18:14.507Z: Bundling asset amplify-amplifyvitereacttemplate-<MY_SANDBOX>/function/test-function-lambda/Code/Stage...

[DEBUG] 2025-02-05T22:18:14.517Z: <PROJECT_FOLDER>\node_modules\@aws-amplify\backend-function\src\factory.ts:454
      throw new AmplifyUserError(
            ^


AmplifyError [NodeJSFunctionConstructInitializationError]: Failed to instantiate nodejs function construct
    at new AmplifyFunction (<PROJECT_FOLDER>\node_modules\@aws-amplify\backend-function\src\factory.ts:454:13)
    at FunctionGenerator.generateContainerEntry (<PROJECT_FOLDER>\node_modules\@aws-amplify\backend-function\src\factory.ts:362:12)
    at SingletonConstructContainer.getOrCompute (<PROJECT_FOLDER>\node_modules\@aws-amplify\backend\src\engine\singleton_construct_container.ts:51:19)
    at FunctionFactory.getInstance (<PROJECT_FOLDER>\node_modules\@aws-amplify\backend-function\src\factory.ts:191:31)    
    at <anonymous> (<PROJECT_FOLDER>\node_modules\@aws-amplify\backend\src\backend_factory.ts:117:68)
    at Array.forEach (<anonymous>)
    at new BackendFactory (<PROJECT_FOLDER>\node_modules\@aws-amplify\backend\src\backend_factory.ts:113:40)
    at defineBackend (<PROJECT_FOLDER>\node_modules\@aws-amplify\backend\src\backend_factory.ts:157:19)
    at <anonymous> (<PROJECT_FOLDER>\amplify\backend.ts:8:1)
    at ModuleJob.run (node:internal/modules/esm/module_job:234:25) {
  cause: Error: Failed to bundle asset amplify-amplifyvitereacttemplate-<MY_SANDBOX>/function/test-function-lambda/Code/Stage, bundle output is located at <PROJECT_FOLDER>\.amplify\artifacts\cdk.out\bundling-temp-49f5fc225f5f9c84d79ef6eb8c561146ce3a8f455642ad6758130b26a30884b3-error: Error: spawnSync cmd ENOENT
      at AssetStaging.bundle (<PROJECT_FOLDER>\node_modules\aws-cdk-lib\core\lib\asset-staging.js:2:619)
      at AssetStaging.stageByBundling (<PROJECT_FOLDER>\node_modules\aws-cdk-lib\core\lib\asset-staging.js:1:5297)        
      at stageThisAsset (<PROJECT_FOLDER>\node_modules\aws-cdk-lib\core\lib\asset-staging.js:1:2728)
      at Cache.obtain (<PROJECT_FOLDER>\node_modules\aws-cdk-lib\core\lib\private\cache.js:1:242)
      at new AssetStaging (<PROJECT_FOLDER>\node_modules\aws-cdk-lib\core\lib\asset-staging.js:1:3125)
      at new Asset (<PROJECT_FOLDER>\node_modules\aws-cdk-lib\aws-s3-assets\lib\asset.js:1:1141)
      at AssetCode.bind (<PROJECT_FOLDER>\node_modules\aws-cdk-lib\aws-lambda\lib\code.js:5:3487)
      at new Function (<PROJECT_FOLDER>\node_modules\aws-cdk-lib\aws-lambda\lib\function.js:1:10003)
      at new NodejsFunction (<PROJECT_FOLDER>\node_modules\aws-cdk-lib\aws-lambda-nodejs\lib\function.js:1:2111)
      at new AmplifyFunction (<PROJECT_FOLDER>\node_modules\@aws-amplify\backend-function\src\factory.ts:430:24),
  classification: 'ERROR',
  options: {
    message: 'Failed to instantiate nodejs function construct',
    resolution: 'See the underlying error message for more details. Use `--debug` for additional debugging information.'
  },
  serializedError: '{"name":"NodeJSFunctionConstructInitializationError","classification":"ERROR","options":{"message":"Failed to instantiate nodejs function construct","resolution":"See the underlying error message for more details. Use `--debug` for additional debugging information."},"cause":{"name":"Error","message":"Failed to bundle asset amplify-amplifyvitereacttemplate-<MY_SANDBOX>/function/test-function-lambda/Code/Stage, bundle output is located at <PROJECT_FOLDER>\\\\.amplify\\\\artifacts\\\\cdk.out\\\\bundling-temp-49f5fc225f5f9c84d79ef6eb8c561146ce3a8f455642ad6758130b26a30884b3-error: Error: spawnSync cmd ENOENT"}}',
  resolution: 'See the underlying error message for more details. Use `--debug` for additional debugging information.',
  details: undefined,
  link: undefined,
  code: undefined
}

Node.js v20.18.2

[DEBUG] 2025-02-05T22:18:14.582Z: Subprocess exited with error 1

[DEBUG] 2025-02-05T22:18:15.437Z: {
    "compilerOptions": {
        "target": "es2022",
        "module": "es2022",
        "moduleResolution": "bundler",
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "skipLibCheck": true,
        "paths": {
            "$amplify/*": [
                "../.amplify/generated/*"
            ]
        },
        "allowSyntheticDefaultImports": true,
        "resolvePackageJsonExports": true,
        "resolvePackageJsonImports": true,
        "useDefineForClassFields": true,
        "noImplicitAny": true,
        "noImplicitThis": true,
        "strictNullChecks": true,
        "strictFunctionTypes": true,
        "strictBindCallApply": true,
        "strictPropertyInitialization": true,
        "strictBuiltinIteratorReturn": true,
        "alwaysStrict": true,
        "useUnknownInCatchVariables": true
    },
    "files": [
        "./backend.ts",
        "./auth/resource.ts",
        "./data/resource.ts",
        "./functions/testFunction/handler.ts",
        "./functions/testFunction/resource.ts",
        "./storage/resource.ts"
    ]
}

Failed to instantiate nodejs function construct
Caused By: Failed to bundle asset amplify-amplifyvitereacttemplate-<MY_SANDBOX>/function/test-function-lambda/Code/Stage, bundle output is located at <PROJECT_FOLDER>\\.amplify\\artifacts\\cdk.out\\bundling-temp-49f5fc225f5f9c84d79ef6eb8c561146ce3a8f455642ad6758130b26a30884b3-error: Error: spawnSync cmd ENOENT

Resolution: See the underlying error message for more details. Use `--debug` for additional debugging information.

[INFO] 2025-02-05T22:18:20.865Z: [Sandbox] Watching for file changes...


aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

@github-actions github-actions bot added pending-triage Incoming issues that need categorization pending-maintainer-response Issue is pending a response from the Amplify team labels Feb 5, 2025
@HuiSF HuiSF transferred this issue from aws-amplify/amplify-js Feb 5, 2025
@HuiSF
Copy link
Member

HuiSF commented Feb 5, 2025

Hi @mgreiner79 Thanks for reporting this issue. I’m transferring it to the Amplify Backend for better assistance.

@ykethan
Copy link
Member

ykethan commented Feb 6, 2025

Hey @HuiSF, thank you for reaching out. Could provide us some information on how was node installed Node: 20.18.2 - C:\Program Files\nodejs\node.EXE and the terminal being used?

Additionally, could you try update the @aws-amplify/backend and @aws-amplify/backend-cli to the latest version and retry sandbox?
for example the latest versions are

 "@aws-amplify/backend": "^1.14.0",
"@aws-amplify/backend-cli": "^1.4.9",

@ykethan ykethan added pending-response Issue is pending response from author sandbox Related to the sandbox experience and removed pending-maintainer-response Issue is pending a response from the Amplify team labels Feb 6, 2025
@mgreiner79
Copy link
Author

Hi @ykethan , thanks for the reply.
the nodejs installation was using the windows installer (.msi) from here https://nodejs.org/en/download/

I have updated to

 "@aws-amplify/backend": "^1.14.0",
"@aws-amplify/backend-cli": "^1.4.9",

But the problem persists.

@github-actions github-actions bot removed the pending-response Issue is pending response from author label Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending-triage Incoming issues that need categorization sandbox Related to the sandbox experience
Projects
None yet
Development

No branches or pull requests

3 participants