Skip to content

With serverless-localstack, browser calls hang but invoke works #102

@davidrhoderick

Description

@davidrhoderick

When I'm using serverless-localstack, I can get a regular handler to work and I can invoke the function that utilizes serverless-mysql but if I try to access the function's path via the browser, the page hangs, I get no results, usually I'm redirected back to the previous path, and I can see a lambda instance spin up in docker and just stay running.

I'd expect that if I can invoke the function that I'd get the results and the page wouldn't hang. I tried using massive for connecting to PostgreSQL and it worked but I thought this would be closer to what I'm used to and work better in the serverless ecosystem.

Here is my serverless.yml:

org: someone
app: someapp
service: api

frameworkVersion: '2'

disabledDeprecations:
  - CLI_OPTIONS_SCHEMA

plugins:
  - serverless-localstack
  - serverless-mocha-plugin

custom:
  localstack:
    stages:
      - local
    host: http://localhost
    edgePort: 4566
    autostart: true
    lambda:
      mountCode: false
    docker:
      sudo: false

provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221
  stage: local
  environment:
    MYSQL_ENDPOINT: 172.21.0.2
    MYSQL_DATABASE: someapp
    MYSQL_USER: someapp_user
    MYSQL_PASSWORD: some+password
    
functions:
  hello:
    handler: ./handler.hello
    events:
      - http:
          path: hello
          method: GET
          cors: true
  testdb:
    handler: ./handler.testdb
    events:
      - http:
          path: testdb
          method: GET
          cors: true

Here's my handler.js:

const mysql = require('serverless-mysql')({
  config: {
    host     : process.env.MYSQL_ENDPOINT,
    database : process.env.MYSQL_DATABASE,
    user     : process.env.MYSQL_USER,
    password : process.env.MYSQL_PASSWORD
  },
  onError: (error) => { // This is never called, did I code this correctly?
    return {
      statusCode: 500,
      body: error
    }
  }
})

exports.hello = async (event) => { // This works
  const { name = '' } = event.body || {}

  return {
    statusCode: 200,
    body: `Hello ${name} 👋`,
  }
}

exports.testdb = async () => { // This hangs and creates a lambda docker that does nothing
  let results = await mysql.query('SELECT * FROM test')

  await mysql.end() // If I use mysql.quit(), the invoke call will finish, I still get the same error on the page though

  return results
}

If I run invoke, I'll get the following and no lambda instance in docker:

Serverless: Using serverless-localstack
Serverless: Warning: Unable to find plugin named: TypeScriptPlugin
[
    {
        "id": 1,
        "data": "first entry"
    },
    {
        "id": 2,
        "data": "second entry"
    },
    {
       "id": 3,
       "data": "third entry"
    }
]

Which is all correct data. If I use await mysql.quit() instead of .end(), it'll give me back my terminal, but with .end(), it just hangs there until I ctrl+c out of it.

Is this related to #79? Is there a way to implement this with serverless-localstack for local development and testing?

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions