serverless / github-action

:zap::octocat: A Github Action for deploying with the Serverless Framework
Apache License 2.0
662 stars 173 forks source link

Error: Cannot find module 'serverless' #70

Closed b-sw closed 2 years ago

b-sw commented 2 years ago

Hey all.

Whenever I manually deploy by calling for example: serverless deploy --stage prod deploy passes and I can then call deployed API endpoints.

If I use the script the deploy also passes but when I try to query my newly aws-deployed API I receive Internal Server Error and this is seen in the logs: image

Question: should serverless be at all required at runtime? My understanding is that it's only used for deploy. What's the error about then?

This is my serverless.yaml:

service: graderef-backend

useDotenv: true

plugins:
  - serverless-offline

provider:
  name: aws
  runtime: nodejs16.x
  region: eu-west-1
  deploymentMethod: direct
  environment:
    SECRET1: ${SECRET1}
    SECRET2: ${SECRET2}
    ...
functions:
  main:
    handler: dist/src/serverless.handler
    events:
      - http:
          method: ANY
          path: /
      - http:
          method: ANY
          path: '{proxy+}'

And this is my gh action script:

name: Deploy-serverless

on:
  push:
    branches: ['feature-serverless']

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install modules
        run: npm install
      - name: Run tests
        run: SECRET1=${{secrets.SECRET1}} SECRET2=${{secrets.SECRET2}} npm run test:e2e
  deploy:
    needs: test
    name: deploy
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [ 16.x ]
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci
      - name: serverless deploy
        uses: serverless/github-action@v3.1
        with:
          args: deploy
        env:
         AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
         AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
         SECRET1: ${{ secrets.SECRET1}}
         SECRET2: ${{ secrets.SECRET2}}
         ...
b-sw commented 2 years ago

I missed adding npm run build to my deploy script. As is seen - handler is specified in dist/ path which is only being created after building npm app. After adjusting deploy script:

...
- name: Install NPM dependencies
        run: npm install

- name: Build dist
  run: npm run build

- name: Deploy Lambda functions
...

all is good.