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

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

Open
davidrhoderick opened this issue Apr 25, 2021 · 2 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@davidrhoderick
Copy link

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?

@SamXDesc
Copy link

I'm facing the same problem here. Any solutions?

@naorpeled
Copy link
Collaborator

Hey @davidrhoderick and @SamXDesc,
sorry for the huge delay.
Will try to take a look at this sometime this week 🙏

@naorpeled naorpeled added the bug Something isn't working label Jun 3, 2023
@naorpeled naorpeled self-assigned this Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants