fI am using localstack framework and run it on a docker container I am able to connect from my host to the dynamo service running on the container (port 4569). When trying to connect from a lambda running on this container to dynamo (running on the same container), the connection is refused. the IP I am using is the name of the container. (e.g., telnet localstack 4569 will fail from running it from the lambda)

Docker ps returns:

13:06:33  CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                                                                                    NAMES
13:06:33  b909ac695561        localstack/localstack:0.11.4   "docker-entrypoint.sh"   2 minutes ago       Up 2 minutes        4566-4568/tcp, 4593-4597/tcp, 0.0.0.0:4569-4592->4569-4592/tcp, 0.0.0.0:8055->8080/tcp   localstack

docker network inspect localstack-network returns:

13:06:33  [
13:06:33      {
13:06:33          "Name": "localstack-network",
13:06:33          "Id": "09994610b0d71dfc4fe0147bbc884a749362c3fb42397366591c73e3c10702eb",
13:06:33          "Created": "2020-10-04T10:03:52.586816186Z",
13:06:33          "Scope": "local",
13:06:33          "Driver": "bridge",
13:06:33          "EnableIPv6": false,
13:06:33          "IPAM": {
13:06:33              "Driver": "default",
13:06:33              "Options": null,
13:06:33              "Config": [
13:06:33                  {
13:06:33                      "Subnet": "100.66.0.0/16",
13:06:33                      "Gateway": "100.66.0.1"
13:06:33                  }
13:06:33              ]
13:06:33          },
13:06:33          "Internal": false,
13:06:33          "Attachable": true,
13:06:33          "Ingress": false,
13:06:33          "ConfigFrom": {
13:06:33              "Network": ""
13:06:33          },
13:06:33          "ConfigOnly": false,
13:06:33          "Containers": {
13:06:33              "b909ac695561200c1ab43c70c9f25cd537622593b7eade03d16af89b70c97d76": {
13:06:33                  "Name": "localstack",
13:06:33                  "EndpointID": "783e7aefbef801d7707d46f664b6adf329dceeee6108d23a36c63d5cb3a3fdae",
13:06:33                  "MacAddress": "02:42:64:42:00:02",
13:06:33                  "IPv4Address": "100.66.0.2/16",
13:06:33                  "IPv6Address": ""
13:06:33              }
13:06:33          },
13:06:33          "Options": {},
13:06:33          "Labels": {
13:06:33              "com.docker.compose.network": "localstack-network",
13:06:33              "com.docker.compose.project": "infra",
13:06:33              "com.docker.compose.version": "1.24.1"
13:06:33          }
13:06:33      }
13:06:33  ]

docker-compose.yaml:

  services:
  localstack:
    image: localstack/localstack:0.11.4
    container_name: localstack
    networks:
      - localstack-network
    ports:
      - "4566-4597:4566-4597"
      - '8080:8080'
    environment:
      - SERVICES=lambda,stepfunctions,dynamodb,s3,sns
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
      - PORT_WEB_UI=${PORT_WEB_UI- }
      - LAMBDA_EXECUTOR=docker #${LAMBDA_EXECUTOR- docker-reuse}
      - DOCKER_HOST=unix:///var/run/docker.sock
      - LAMBDA_REMOTE_DOCKER=true
      - LAMBDA_REMOVE_CONTAINERS=true
    volumes:
      - "/tmp${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
2

There are 2 answers

1
Tomaz Tekavec On

Lambda which runs in localstack is actually executed in a container, so you have a container (Lambda) running in another container (localstack) so Lambda needs to know localstack address if it needs to communicate with other localstack services (like DynamoDB).

Inside your Lambda function, get localstack address and use it in configuration object that is used for creating an instance of DynamoDB client - this is an C# example:

var serviceURL = $@"http://{Environment.GetEnvironmentVariable("LOCALSTACK_HOSTNAME")}:4569";

There might be other reasons why your Lambda cannot connect to localstack so feel free to expand your question with your docker/docker-compose yaml setup file and the function itself, I might be able to help you further if you're still stuck.

PS: You can also switch to single port 4566 for all localstack services since you're using quite recent version.

1
Carlos Schuenck On

When you don't specify "endpoint" it's going to try to request to AWS, so you need to config your code to look for your local aws environment. If you use "http:localhost:4556" it will not work, it will not be able to connect. The solution is to specify the endpoint as "host.docker.internal:4566" and configure the local credentials too. Example:

Try to do something like this:

const s3 = new S3({
  s3ForcePathStyle: true,
  accessKeyId: 'S3RVER', // This specific key is required when working offline
  secretAccessKey: 'S3RVER',
  endpoint: new Endpoint('host.docker.internal:4566'),
});