docker-aws-sam-local icon indicating copy to clipboard operation
docker-aws-sam-local copied to clipboard

Use with docker-compose?

Open GeoffreyBooth opened this issue 7 years ago • 1 comments

I’m trying to use this in start-api mode with docker-compose. I have this in my docker-compose.yml file:

version: '3'

services:
  amazon:
    image: cnadiminti/aws-sam-local
    command: local start-api --host 0.0.0.0 --docker-volume-basedir /var/opt
    ports:
      - '3000:3000'
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./amazon:/var/opt

With the code from https://github.com/awslabs/aws-sam-local/tree/develop/samples/api-event-source in ./amazon. The container starts up, and seems to load template.yml just fine:

amazon_1   | nodejs6.10: Pulling from lambci/lambda
amazon_1   | Digest: sha256:7eb4ced6a15ae3c30effc4ec0cd3aabb2bd57c9a8330b37920c3d5d722d81083
amazon_1   | Status: Image is up to date for lambci/lambda:nodejs6.10
amazon_1   |
amazon_1   | Mounting index.handler (nodejs6.10) at http://0.0.0.0:3000/ [OPTIONS GET HEAD POST PUT DELETE TRACE CONNECT]
amazon_1   | Mounting static files from /var/opt/public at /
amazon_1   |
amazon_1   | You can now browse to the above endpoints to invoke your functions.
amazon_1   | You do not need to restart/reload SAM CLI while working on your functions,
amazon_1   | changes will be reflected instantly/automatically. You only need to restart
amazon_1   | SAM CLI if you update your AWS SAM template.
amazon_1   |

But hitting http://localhost:3000/ throws an error:

amazon_1   | 2017/11/01 06:48:24 Invoking index.handler (nodejs6.10)
amazon_1   | START RequestId: a0b9d93e-517e-1bbe-6ffb-d8ad37e1d5db Version: $LATEST
amazon_1   | Unable to import module 'index': Error
amazon_1   |     at Function.Module._resolveFilename (module.js:469:15)
amazon_1   |     at Function.Module._load (module.js:417:25)
amazon_1   |     at Module.require (module.js:497:17)
amazon_1   |     at require (internal/module.js:20:19)
amazon_1   | END RequestId: a0b9d93e-517e-1bbe-6ffb-d8ad37e1d5db
amazon_1   | REPORT RequestId: a0b9d93e-517e-1bbe-6ffb-d8ad37e1d5db	Duration: 12.61 ms	Billed Duration: 0 ms	Memory Size: 0 MB	Max Memory Used: 28 MB
amazon_1   | 2017/11/01 06:48:45 Function returned an invalid response (must include one of: body, headers or statusCode in the response object): %!s(<nil>)

The files in that folder work fine via sam local start-api, so it’s not a file path issue.

GeoffreyBooth avatar Nov 01 '17 06:11 GeoffreyBooth

I got this to work. --docker-volume-basedir needs to reference a path on the host, e.g. /Users/Geoffrey/Sites/project/amazon on my Mac, not /var/opt inside the container as I expected it to be. Submitted #2 to document this as an example for others.

GeoffreyBooth avatar Nov 01 '17 18:11 GeoffreyBooth