localstack / serverless-localstack

⚡ Serverless plugin for running against LocalStack
514 stars 82 forks source link

Serverless localstack plugin does not autostart docker container #258

Open marcogreiveldinger opened 1 month ago

marcogreiveldinger commented 1 month ago

Hey there, I have a strange behaviour with the plugin when using it with the docker flag autostart: true. The situation is that our config works on my machine, but it does not work on the machine from a colleague. It starts a localstack container as expected, but on the other machine, nothing is happening. I tried to debug it a couple of times, but did not get any further. Do you have any idea what is going wrong here?

service: akeneo-graphQL-connector

plugins:
  - serverless-localstack
  - serverless-plugin-typescript

provider:
  name: aws
  stage: local
  region: eu-central-1
  runtime: nodejs20.x
  stackName: akeneo-graphQL-connector
  environment:
    DYNAMODB_TABLE: "test"
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:Query
      Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"

custom:
  localstack:
    stages:
      # list of stages for which the plugin should be enabled
      - local
    host: http://localhost  # optional - LocalStack host to connect to
    edgePort: 4566  # optional - LocalStack edge port to connect to
    autostart: true  # optional - Start LocalStack in Docker on Serverless deploy
    lambda:
      # Enable this flag to improve performance
      mountCode: True
    docker:
      # Enable this flag to run "docker ..." commands as sudo
      sudo: False

functions:
  akeneo-graphQl:
    handler: handler.akeneoGraphQLInteractor
    name: akeneo-graphQl-connector-api
    description: A microservice for connection to the akeneo Graph QL pim and related akeneo Graph QL api executions
    timeout: 300
    events:
      - http:
          path: /{proxy+}
          method: any
          cors: true

We both start our local development with npm run start-local-dev

Our scripts to run the localstack plugin with nodemon

  "scripts": {
    "build": "npx tsc",
    "deploy": "LAMBDA_REMOVE_CONTAINERS=1 LAMBDA_KEEPALIVE_MS=5 serverless deploy --stage local",
    "watch": "npx nodemon -e '.ts' -x 'npm run build' || exit 1",
    "start-local-dev": "LAMBDA_REMOVE_CONTAINERS=1 LAMBDA_KEEPALIVE_MS=5 npm run deploy && npm run watch",
  }

The versions we are using:

    "nodemon": "^3.1.0",
    "serverless-localstack": "^1.2.1",
    "serverless-plugin-typescript": "^2.1.5",

serverless is used in 3.39.0. We tried to install it with npm install -g serverless@3.39.0 or with homebrew brew install serverless docker is also usable via the cli with the current user, so no root rights needed for that.

Do you have any idea why it works with these commands on my machine, but does not on the machine of my colleague?

This is his output after the start npm run start-local-dev


akeneo-connector@1.0.0 start-local-dev
> LAMBDA_REMOVE_CONTAINERS=1 LAMBDA_KEEPALIVE_MS=5 npm run deploy && npm run watch

> akeneo-connector@1.0.0 deploy
> LAMBDA_REMOVE_CONTAINERS=1 LAMBDA_KEEPALIVE_MS=5 serverless deploy --stage local

(node:3150) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

Deploying akeneo-graphQL-connector to stage local (eu-central-1)
Using serverless-localstack
Using serverless-localstack
serverless-localstack: Reconfigured endpoints
serverless-localstack: Reconfigured endpoints
Compiling with Typescript...
Using local tsconfig.json - tsconfig.json
Typescript compiled.

Warning: Package patterns at function level are only applicable if package.individually is set to true at service level or function level in serverless.yaml. The framework will ignore the patterns defined at the function level and apply only the service-wide ones.
Starting LocalStack in Docker. This can take a while.
Skip plugin function AwsCompileFunctions.downloadPackageArtifacts (lambda.mountCode flag is enabled)
Skip plugin function AwsDeploy.extendedValidate (lambda.mountCode flag is enabled)

✖ Stack akeneo-graphQL-connector failed to deploy (34s)
Environment: darwin, node 22.2.0, framework 3.39.0 (local), plugin 7.2.3, SDK 4.5.1
Credentials: Local, environment variables
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Error:
Inaccessible host: `127.0.0.1' at port `4566'. This service may not be available in the `eu-central-1' region.

1 deprecation found: run 'serverless doctor' for more details```
steffyP commented 1 month ago

Hi @marcogreiveldinger, if the same scripts works for you but not on your colleague's machine it might me some configuration or permission setting. What machine do you use vs your colleague? My first guess would be that for some reason starting an docker container from the plugin is somehow blocked.

You could also try to use the docker-compose config (see sample config file) and see if that may work. Using docker compose is anyhow the better option as you are able to apply custom configuration using the docker-compose file.