aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 820 forks source link

NodejsFunction is not available in custom resource. #10286

Closed harukaxq closed 2 years ago

harukaxq commented 2 years ago

Before opening, please confirm:

JavaScript Framework

Next.js

Amplify APIs

Not applicable

Amplify Categories

Not applicable

Environment information

System: OS: Linux 5.13 Zorin OS 16.1 CPU: (16) x64 AMD Ryzen 9 5900HS with Radeon Graphics Memory: 2.43 GB / 15.04 GB Container: Yes Shell: 3.4.1 - /usr/bin/fish Binaries: Node: 16.6.1 - ~/.asdf/installs/nodejs/16.6.1/bin/node Yarn: 1.22.17 - ~/.asdf/installs/nodejs/16.6.1/.npm/bin/yarn npm: 7.20.3 - ~/.asdf/plugins/nodejs/shims/npm Browsers: Chrome: 100.0.4896.127 Firefox: 99.0 npmPackages: @ampproject/toolbox-optimizer: undefined () @babel/core: undefined () @babel/runtime: 7.15.4 @hapi/accept: undefined () @napi-rs/triples: undefined () @next/bundle-analyzer: ^12.1.4 => 12.1.5 @next/react-dev-overlay: undefined () @next/react-refresh-utils: 12.1.5 @peculiar/webcrypto: undefined () @types/node: ^17.0.23 => 17.0.25 @types/react: ^17.0.44 => 17.0.44 @typescript-eslint/eslint-plugin: ^5.18.0 => 5.20.0 @typescript-eslint/parser: ^5.18.0 => 5.20.0 (5.10.1) @vercel/nft: undefined () abort-controller: undefined () acorn: undefined () amphtml-validator: undefined () arg: undefined () assert: undefined () async-retry: undefined () async-sema: undefined () autoprefixer: ^10.4.4 => 10.4.4 babel-packages: undefined () browserify-zlib: undefined () browserslist: undefined () buffer: undefined () bytes: undefined () chalk: undefined () ci-info: undefined () cli-select: undefined () comment-json: undefined () compression: undefined () conf: undefined () constants-browserify: undefined () content-disposition: undefined () content-type: undefined () cookie: undefined () cross-env: ^7.0.3 => 7.0.3 cross-spawn: undefined () crypto-browserify: undefined () cssnano: ^5.1.7 => 5.1.7 cssnano-simple: undefined () debug: undefined () devalue: undefined () domain-browser: undefined () eslint: ^8.13.0 => 8.14.0 eslint-config-airbnb-base: ^15.0.0 => 15.0.0 eslint-config-airbnb-typescript: ^17.0.0 => 17.0.0 eslint-config-next: ^12.1.4 => 12.1.5 eslint-config-prettier: ^8.5.0 => 8.5.0 eslint-plugin-import: ^2.26.0 => 2.26.0 (2.25.2) eslint-plugin-jsx-a11y: ^6.5.1 => 6.5.1 eslint-plugin-prettier: ^4.0.0 => 4.0.0 eslint-plugin-react: ^7.29.4 => 7.29.4 (7.29.1) eslint-plugin-react-hooks: ^4.4.0 => 4.4.0 (4.3.0) eslint-plugin-simple-import-sort: ^7.0.0 => 7.0.0 eslint-plugin-tailwindcss: ^3.5.0 => 3.5.0 eslint-plugin-unused-imports: ^2.0.0 => 2.0.0 etag: undefined () events: undefined () find-cache-dir: undefined () find-up: undefined () formdata-node: undefined () fresh: undefined () get-orientation: undefined () glob: undefined () gzip-size: undefined () http-proxy: undefined () https-browserify: undefined () husky: ^7.0.4 => 7.0.4 icss-utils: undefined () ignore-loader: undefined () image-size: undefined () is-animated: undefined () is-docker: undefined () is-wsl: undefined () jest-worker: undefined () json5: undefined () jsonwebtoken: undefined () lint-staged: ^12.3.7 => 12.4.0 loader-utils: undefined () lodash.curry: undefined () lru-cache: undefined () micromatch: undefined () mini-css-extract-plugin: undefined () nanoid: undefined () native-url: undefined () neo-async: undefined () next: ^12.1.4 => 12.1.5 next-seo: ^5.4.0 => 5.4.0 node-fetch: undefined () node-html-parser: undefined () npm-run-all: ^4.1.5 => 4.1.5 ora: undefined () os-browserify: undefined () p-limit: undefined () path-browserify: undefined () postcss: ^8.4.12 => 8.4.12 (8.4.5) postcss-flexbugs-fixes: undefined () postcss-modules-extract-imports: undefined () postcss-modules-local-by-default: undefined () postcss-modules-scope: undefined () postcss-modules-values: undefined () postcss-preset-env: undefined () postcss-safe-parser: undefined () postcss-scss: undefined () postcss-value-parser: undefined () prettier: ^2.6.2 => 2.6.2 process: undefined () punycode: undefined () querystring-es3: undefined () raw-body: undefined () react: ^17.0.2 => 17.0.2 react-dom: ^17.0.2 => 17.0.2 react-is: 17.0.2 react-refresh: 0.12.0 react-server-dom-webpack: undefined () regenerator-runtime: 0.13.4 rimraf: ^3.0.2 => 3.0.2 sass-loader: undefined () schema-utils: undefined () semver: undefined () send: undefined () setimmediate: undefined () source-map: undefined () stream-browserify: undefined () stream-http: undefined () string-hash: undefined () string_decoder: undefined () strip-ansi: undefined () tailwindcss: ^3.0.23 => 3.0.24 terser: undefined () text-table: undefined () timers-browserify: undefined () tty-browserify: undefined () typescript: ^4.6.3 => 4.6.3 ua-parser-js: undefined () unistore: undefined () use-subscription: undefined () util: undefined () uuid: undefined () vm-browserify: undefined () watchpack: undefined () web-streams-polyfill: undefined () web-vitals: undefined () webpack: undefined () webpack-sources: undefined () ws: undefined () npmGlobalPackages: @ansible/ansible-language-server: 0.4.0-alpha.6 @aws-amplify/cli: 8.0.0 @interplay/cli-plugin-react: 2.0.8 @interplay/cli: 2.0.8 dynamodb-admin: 4.1.0 graphql-language-service-cli: 3.2.14 graphql: 16.3.0 intelephense: 1.8.2 terminalizer: 0.7.2 typescript: 4.6.2

Describe the bug

I want to create a Lambda with NodejsFunction in amplify's custom resource (CDK). But I can't complete it due to the following error

⠹ Fetching updates to backend environment: local from the cloud.⠋ Building resource custom/shopify⠋ Building custom resourcesSending build context to Docker daemon  132.6kB
Step 1/12 : ARG IMAGE=public.ecr.aws/sam/build-nodejs14.x
Step 2/12 : FROM $IMAGE
 ---> 469ecc9417bf
Step 3/12 : RUN npm install --global yarn@1.22.5
 ---> Using cache
 ---> ce7db24ac320
Step 4/12 : RUN npm install --global pnpm
 ---> Using cache
 ---> 9eeb73fa2bdc
Step 5/12 : RUN npm install --global typescript
 ---> Using cache
 ---> 18322303e7b4
Step 6/12 : ARG ESBUILD_VERSION=0
 ---> Using cache
 ---> 8353ccf232fe
Step 7/12 : RUN npm install --global --unsafe-perm=true esbuild@$ESBUILD_VERSION
 ---> Using cache
 ---> 2dd6a0a0569c
Step 8/12 : RUN mkdir /tmp/npm-cache &&     chmod -R 777 /tmp/npm-cache &&     npm config --global set cache /tmp/npm-cache
 ---> Using cache
 ---> 0efc4c676ff3
Step 9/12 : RUN mkdir /tmp/yarn-cache &&     chmod -R 777 /tmp/yarn-cache &&     yarn config set cache-folder /tmp/yarn-cache
 ---> Using cache
 ---> a8ab29e1eb7c
Step 10/12 : RUN npm config --global set update-notifier false
 ---> Using cache
 ---> 583b60e2e709
Step 11/12 : RUN /sbin/useradd -u 1000 user && chmod 711 /
 ---> Using cache
 ---> f9b4bbe030bb
Step 12/12 : CMD [ "esbuild" ]
 ---> Using cache
 ---> 5367d6933795
Successfully built 5367d6933795
Successfully tagged cdk-4184c0b02180e727f7f794a3376e510cea9c0e28da4a9d9d47717e7391d7312e:latest
Bundling asset Default/shopifyapplocal/Code/Stage...
esbuild cannot run locally. Switching to Docker bundling.

  asset-output/index.js  99b

⚡ Done in 3ms
✔ Successfully pulled backend environment local from the cloud.
⠋ Building resource custom/shopify⠋ Building custom resourcesSending build context to Docker daemon  132.6kB
Step 1/12 : ARG IMAGE=public.ecr.aws/sam/build-nodejs14.x
Step 2/12 : FROM $IMAGE
 ---> 469ecc9417bf
Step 3/12 : RUN npm install --global yarn@1.22.5
 ---> Using cache
 ---> ce7db24ac320
Step 4/12 : RUN npm install --global pnpm
 ---> Using cache
 ---> 9eeb73fa2bdc
Step 5/12 : RUN npm install --global typescript
 ---> Using cache
 ---> 18322303e7b4
Step 6/12 : ARG ESBUILD_VERSION=0
 ---> Using cache
 ---> 8353ccf232fe
Step 7/12 : RUN npm install --global --unsafe-perm=true esbuild@$ESBUILD_VERSION
 ---> Using cache
 ---> 2dd6a0a0569c
Step 8/12 : RUN mkdir /tmp/npm-cache &&     chmod -R 777 /tmp/npm-cache &&     npm config --global set cache /tmp/npm-cache
 ---> Using cache
 ---> 0efc4c676ff3
Step 9/12 : RUN mkdir /tmp/yarn-cache &&     chmod -R 777 /tmp/yarn-cache &&     yarn config set cache-folder /tmp/yarn-cache
 ---> Using cache
 ---> a8ab29e1eb7c
Step 10/12 : RUN npm config --global set update-notifier false
 ---> Using cache
 ---> 583b60e2e709
Step 11/12 : RUN /sbin/useradd -u 1000 user && chmod 711 /
 ---> Using cache
 ---> f9b4bbe030bb
Step 12/12 : CMD [ "esbuild" ]
 ---> Using cache
 ---> 5367d6933795
Successfully built 5367d6933795
Successfully tagged cdk-4184c0b02180e727f7f794a3376e510cea9c0e28da4a9d9d47717e7391d7312e:latest
Bundling asset Default/shopifyapplocal/Code/Stage...
esbuild cannot run locally. Switching to Docker bundling.

  asset-output/index.js  99b

⚡ Done in 2ms

    Current Environment: local

┌──────────┬───────────────┬───────────┬───────────────────┐
│ Category │ Resource name │ Operation │ Provider plugin   │
├──────────┼───────────────┼───────────┼───────────────────┤
│ Custom   │ shopify       │ Update    │ awscloudformation │
└──────────┴───────────────┴───────────┴───────────────────┘
⠋ Building resource custom/shopify⠋ Building custom resourcesSending build context to Docker daemon  132.6kB
Step 1/12 : ARG IMAGE=public.ecr.aws/sam/build-nodejs14.x
Step 2/12 : FROM $IMAGE
 ---> 469ecc9417bf
Step 3/12 : RUN npm install --global yarn@1.22.5
 ---> Using cache
 ---> ce7db24ac320
Step 4/12 : RUN npm install --global pnpm
 ---> Using cache
 ---> 9eeb73fa2bdc
Step 5/12 : RUN npm install --global typescript
 ---> Using cache
 ---> 18322303e7b4
Step 6/12 : ARG ESBUILD_VERSION=0
 ---> Using cache
 ---> 8353ccf232fe
Step 7/12 : RUN npm install --global --unsafe-perm=true esbuild@$ESBUILD_VERSION
 ---> Using cache
 ---> 2dd6a0a0569c
Step 8/12 : RUN mkdir /tmp/npm-cache &&     chmod -R 777 /tmp/npm-cache &&     npm config --global set cache /tmp/npm-cache
 ---> Using cache
 ---> 0efc4c676ff3
Step 9/12 : RUN mkdir /tmp/yarn-cache &&     chmod -R 777 /tmp/yarn-cache &&     yarn config set cache-folder /tmp/yarn-cache
 ---> Using cache
 ---> a8ab29e1eb7c
Step 10/12 : RUN npm config --global set update-notifier false
 ---> Using cache
 ---> 583b60e2e709
Step 11/12 : RUN /sbin/useradd -u 1000 user && chmod 711 /
 ---> Using cache
 ---> f9b4bbe030bb
Step 12/12 : CMD [ "esbuild" ]
 ---> Using cache
 ---> 5367d6933795
Successfully built 5367d6933795
Successfully tagged cdk-4184c0b02180e727f7f794a3376e510cea9c0e28da4a9d9d47717e7391d7312e:latest
Bundling asset Default/shopifyapplocal/Code/Stage...
esbuild cannot run locally. Switching to Docker bundling.

  asset-output/index.js  99b

⚡ Done in 2ms
⠙ Updating resources in the cloud. This may take a few minutes...

UPDATE_IN_PROGRESS amplify-shopifyapp-local-203126 AWS::CloudFormation::Stack Sat Apr 23 2022 22:13:51 GMT+0900 (GMT+09:00) User Initiated
UPDATE_IN_PROGRESS customshopify                   AWS::CloudFormation::Stack Sat Apr 23 2022 22:13:56 GMT+0900 (GMT+09:00)
⠼ Updating resources in the cloud. This may take a few minutes...

UPDATE_IN_PROGRESS amplify-shopifyapp-local-203126-customshopify-3IWOENNSMK48 AWS::CloudFormation::Stack Sat Apr 23 2022 22:13:57 GMT+0900 (GMT+09:00) User Initiated
⠴ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS shopifyapplocalServiceRole3F1D96A5 AWS::IAM::Role Sat Apr 23 2022 22:14:03 GMT+0900 (GMT+09:00)
CREATE_IN_PROGRESS shopifyapplocalServiceRole3F1D96A5 AWS::IAM::Role Sat Apr 23 2022 22:14:04 GMT+0900 (GMT+09:00) Resource creation Initiated
⠇ Updating resources in the cloud. This may take a few minutes...

CREATE_COMPLETE shopifyapplocalServiceRole3F1D96A5 AWS::IAM::Role Sat Apr 23 2022 22:14:26 GMT+0900 (GMT+09:00)
⠴ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS shopifyapplocal209B4418 AWS::Lambda::Function Sat Apr 23 2022 22:14:29 GMT+0900 (GMT+09:00)
⠇ Updating resources in the cloud. This may take a few minutes...

CREATE_IN_PROGRESS shopifyapplocal209B4418 AWS::Lambda::Function Sat Apr 23 2022 22:14:32 GMT+0900 (GMT+09:00) Resource creation Initiated
⠇ Updating resources in the cloud. This may take a few minutes...

CREATE_FAILED               shopifyapplocal209B4418                                    AWS::Lambda::Function      Sat Apr 23 2022 22:14:32 GMT+0900 (GMT+09:00) Resource handler returned message: "Error occurred while GetObject. S3 Error Code: NoSuchBucket. S3 Error Message: The specified bucket does not exist (Service: Lambda, Status Code: 400, Request ID: 9dac2070-7391-49bb-9d56-843be74cfa6a)" (RequestToken: 50d6a377-b53b-61bf-de72-f5bf578556a0, HandlerErrorCode: InvalidRequest)
UPDATE_ROLLBACK_IN_PROGRESS amplify-shopifyapp-local-203126-customshopify-3IWOENNSMK48 AWS::CloudFormation::Stack Sat Apr 23 2022 22:14:33 GMT+0900 (GMT+09:00) The following resource(s) failed to create: [shopifyapplocal209B4418].                                                                                                                                                                                                                                                     
⠙ Updating resources in the cloud. This may take a few minutes...

UPDATE_FAILED               customshopify                   AWS::CloudFormation::Stack Sat Apr 23 2022 22:14:44 GMT+0900 (GMT+09:00) Embedded stack arn:aws:cloudformation:ap-northeast-1:178726331894:stack/amplify-shopifyapp-local-203126-customshopify-3IWOENNSMK48/40b263f0-c2fd-11ec-9c87-0e2a2931a26f was not successfully updated. Currently in UPDATE_ROLLBACK_IN_PROGRESS with reason: The following resource(s) failed to create: [shopifyapplocal209B4418].
UPDATE_ROLLBACK_IN_PROGRESS amplify-shopifyapp-local-203126 AWS::CloudFormation::Stack Sat Apr 23 2022 22:14:45 GMT+0900 (GMT+09:00) The following resource(s) failed to update: [customshopify].                                                                                                                                                                                           
⠧ Updating resources in the cloud. This may take a few minutes...

UPDATE_IN_PROGRESS customshopify AWS::CloudFormation::Stack Sat Apr 23 2022 22:14:52 GMT+0900 (GMT+09:00)
⠙ Updating resources in the cloud. This may take a few minutes...

UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS amplify-shopifyapp-local-203126-customshopify-3IWOENNSMK48 AWS::CloudFormation::Stack Sat Apr 23 2022 22:15:04 GMT+0900 (GMT+09:00)
⠴ Updating resources in the cloud. This may take a few minutes...

UPDATE_COMPLETE                              customshopify                   AWS::CloudFormation::Stack Sat Apr 23 2022 22:15:14 GMT+0900 (GMT+09:00)
UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS amplify-shopifyapp-local-203126 AWS::CloudFormation::Stack Sat Apr 23 2022 22:15:15 GMT+0900 (GMT+09:00)
⠋ Updating resources in the cloud. This may take a few minutes...

UPDATE_COMPLETE          customshopify                   AWS::CloudFormation::Stack Sat Apr 23 2022 22:15:50 GMT+0900 (GMT+09:00)
UPDATE_ROLLBACK_COMPLETE amplify-shopifyapp-local-203126 AWS::CloudFormation::Stack Sat Apr 23 2022 22:15:50 GMT+0900 (GMT+09:00)
⠋ Updating resources in the cloud. This may take a few minutes...

Following resources failed

Resource Name: amplify-shopifyapp-2-shopifyapplocal209B4418-fNmst9gMVL0K (AWS::Lambda::Function)
Event Type: create
Reason: Resource handler returned message: "Error occurred while GetObject. S3 Error Code: NoSuchBucket. S3 Error Message: The specified bucket does not exist (Service: Lambda, Status Code: 400, Request ID: 9dac2070-7391-49bb-9d56-843be74cfa6a)" (RequestToken: 50d6a377-b53b-61bf-de72-f5bf578556a0, HandlerErrorCode: InvalidRequest)

✖ An error occurred when pushing the resources to the cloud
🛑 An error occurred during the push operation: Resource is not in the state stackUpdateComplete

The following error is from cdk v2, but the same problem occurs with cdk v1

I got the same error when using Lambda and Asset.

I know I can do the same thing with amplify add function, but I want to use NodejsFunction for reusability and extensibility.

Expected behavior

Lambda is created correctly

Reproduction steps

  1. amplify add custom -> cdk
  2. edit package.json and cdk-stack.ts
  3. cd amplify/backend/custom/{resorce name}/
  4. yarn install
  5. amplify push --yes

Code Snippet

cdk-stack.ts

import * as AmplifyHelpers from '@aws-amplify/cli-extensibility-helper';
import { CfnParameter, Stack, StackProps } from 'aws-cdk-lib';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Construct } from 'constructs';
import path from 'path';

export class cdkStack extends Stack {
  constructor(
    scope: Construct,
    id: string,
    props?: StackProps,
    amplifyResourceProps?: AmplifyHelpers.AmplifyResourceProps
  ) {
    super(scope, id, { ...props });
    /* Do not remove - Amplify CLI automatically injects the current deployment environment in this input parameter */
    new CfnParameter(this, 'env', {
      type: 'String',
      description: 'Current Amplify CLI env name',
    });
    const amplifyProjectInfo = AmplifyHelpers.getProjectInfo();
    new NodejsFunction(
      this,
      `${amplifyProjectInfo.projectName}${amplifyProjectInfo.envName}`,
      {
        entry: path.join(__dirname, './../resource/lambda/src/index.js'),
        depsLockFilePath: path.join(
          __dirname,
          './../resource/lambda/yarn.lock'
        ),
      }
    );
  }
}

package.json

{
  "name": "custom-resource",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "@aws-amplify/cli-extensibility-helper": "^2.0.0",
    "aws-cdk-lib": "2.0.0",
    "constructs": "^10.0.0"
  },
  "devDependencies": {
    "typescript": "^4.2.4"
  }
}

resource/lambda/src/index.js


exports.handler = function (event, context) {
  console.log('hooo');
};

resource/lambda/package.json

{
  "name": "lambda",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT"
}

Log output

``` // Put your logs below this line ```

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

chrisbonifacio commented 2 years ago

Hi @harukaxq 👋 thanks for raising this issue. It sounds like this has more to do with the CLI so I'm going to transfer this over for better support and feedback.

ykethan commented 2 years ago

Hey @harukaxq, Thank you for reaching out. Currently, amplify does not support packaging assets out of the box. A feature request is currently is open for this https://github.com/aws-amplify/amplify-cli/issues/9055, Please do add your use case and queries in this thread.