-
Notifications
You must be signed in to change notification settings - Fork 90
Description
When using the serverless-localstack plugin with Serverless Framework v4 inside a Docker container, the plugin fails to properly redirect all AWS SDK calls to the LocalStack container. Despite configuring the correct endpoint URL, some AWS SDK calls still attempt to connect to localhost:4566 instead of the LocalStack container, resulting in connection refused errors.
Environment Details
- Serverless Framework Version: 4.18.2
- serverless-localstack Version: 1.3.1
- Node.js Version: 22.16.0
- Docker Environment: Docker Compose with separate containers for LocalStack and Serverless deployment
Configuration
docker-compose.yml:
services:
aws:
image: localstack/localstack-pro
container_name: localstack
environment:
- LOCALSTACK_AUTH_TOKEN=${LOCALSTACK_AUTH_TOKEN:?}
ports:
- '4566:4566'
- '4510-4559:4510-4559'
volumes:
- /var/run/docker.sock:/var/run/docker.sock
test:
build:
context: .
dockerfile: Dockerfile
environment:
- SERVERLESS_LICENSE_KEY=${SERVERLESS_LICENSE_KEY}
- AWS_ACCESS_KEY_ID=dummy
- AWS_SECRET_ACCESS_KEY=dummy
- AWS_ENDPOINT_URL=http://aws:4566
command: npm run deploy:local
depends_on:
aws:
condition: service_healthy
serverless.yml:
service: my-test-project
plugins:
- serverless-localstack
custom:
localstack:
stages:
- local
provider:
name: aws
runtime: nodejs22.x
region: eu-central-1
stage: local
functions:
hello:
handler: build/index.hello
events:
- httpApi:
path: /
method: get
Expected Behavior
All AWS SDK calls should be redirected to http://aws:4566 (the LocalStack container) when running inside the Docker environment.
Actual Behavior
The deployment fails with connection errors as some AWS SDK calls still attempt to connect to 127.0.0.1:4566 (localhost) instead of the LocalStack container:
✖ Error: connect ECONNREFUSED 127.0.0.1:4566
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1637:16)
Terminal Output
From the logs, I can see that:
- LocalStack starts successfully and is ready
- The serverless-localstack plugin reports "Reconfigured endpoints"
- Some AWS calls succeed (e.g.,
AWS sts.GetCallerIdentity => 200) - But the deployment ultimately fails with connection refused to localhost
Workaround
The issue does not occur when:
- Running Serverless Framework v4 directly on the host machine (outside Docker)
- Using LocalStack running on the host machine
- The same configuration works perfectly in non-Docker environments
Reproduction Steps
- Clone the repository: https://github.com/TeemuLeino/localstack-sls-v4
- Set up environment variables:
export LOCALSTACK_AUTH_TOKEN=your_token
export SERVERLESS_LICENSE_KEY=your_license
- Run:
docker compose up --build - Observe the connection refused error
Additional Information
- The issue appears to be related to how the
serverless-localstackplugin handles endpoint configuration in containerized environments - The plugin seems to partially reconfigure endpoints but not all AWS SDK calls are properly redirected
- This is specifically a problem with Serverless Framework v4 in Docker environments
- The same setup works fine with Serverless Framework v3
Files
- Repository: https://github.com/TeemuLeino/localstack-sls-v4
- Key files:
docker-compose.yml,serverless.yml,package.json,Dockerfile
This issue prevents the use of serverless-localstack in containerized CI/CD pipelines and development environments where both LocalStack and Serverless Framework need to run in separate containers.