Skip to content

Deploy API Gateway NodeJS + MongoDB #161

Description

@dimoreira

I have a similar scenario as the example AWS Node REST API + MongoDB.

The main difference of my project is that I wrote the service in multiple files using modules for better testability.

I can package and deploy from my Mac, but I'm implementing some CI/CD using Bitbucket Pipelines for my project. And as I use bcrypt for password generation and user authentication I need to use a docker image for bitbucket-pipelines that mimics the AWS AMI used for AWS Lambda deploys.

The main problem is that when I use the serverless deploy on the Linux machine, the command returns an error in the Bitbucket Pipelines:

Serverless Error ---------------------------------------
 
An error occurred while provisioning your stack: UserAuthLambdaFunction
    - Unzipped size must be smaller than 262144000 bytes.
 
Stack Trace --------------------------------------------

An example of my setup is the following:

Files and folders:

__tests__/ # jest folder for model and services test
bitbucket-pipelines.yml
config/
models/
package.json
serverless.yml
services/
userService.js
yarn.lock

My serverless.yml file is setup as this:

service: <NAME-OF-MY-SERVICE>

provider:
  name: aws
  runtime: nodejs6.10
  profile: ${env:AWS_PROFILE}
  stage: ${env:STAGE_ENVIRONMENT}
  region: us-east-2

functions:
  userSignup:
    handler: userService.signup
    events:
      - http:
          path: users
          method: post
  userAuth:
    handler: userService.auth
    events:
      - http:
          path: users/sign_in
          method: post

package:
  exclude:
    - .git/**
    - .gitignore
    - __tests__/**
    - config/utils.js
    - bitbucket-pipelines.yml
    - yarn.lock

My userService.js handler file is setup as this:

'use strict'

const mongoose = require('./config/mongoose')
require('dotenv').config()
let db = mongoose.connect(process.env.MONGO_URL)

const UserService = require('./services/users')
const userService = new UserService()

module.exports.signup = (event, context, cb) => {
  userService.signup(event, (err, res) => {
    cb(err, res)
    db.disconnect()
  })
}

module.exports.auth = (event, context, cb) => {
  const response = {
    statusCode: 200,
    body: {
      message: 'userService auth endpoint',
      payload: event.body
    }
  }

  cb(null, response)
  db.disconnect()
}

My service currently only does the signup part and return an authorization token as the response body. I need to know how the Mac deploys successfully (but with error from native modules as bcrypt being generated with the different architecture needed), and the Linux (AWS AMI) one can't deploy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions