I'm trying to create a Docker image for AWS SAM CLI, but invoking any function gives an error: "Unable to import module 'index'". I can run the same test case outside of Docker sucessfully. You can clone the test case here or see the files below.
I already tried the following:
- Setting permissions of files and parent folder to 777 (or 755).
- Disabling SELinux in the Docker daemon (or enabling it).
- Running Docker under privileged mode (or not).
- I get the same error using an old (SAM 0.22) Docker image.
- Running the same function locally as described below (works).
- Zipping the folder and running on AWS (works).
These solutions probably don't apply:
- The format of the zip file incorrectly including the parent folder (no zip file is used).
- Issues related to NPM dependencies or node_modules (index.js has no dependencies).
- Compilation errors in index.js (syntax is correct, works outside of Docker and on AWS).
- The volume is being mounted on the host as described here.
Dockerfile
FROM alpine:3.6
WORKDIR /usr/src/app
RUN apk add --no-cache py-pip
RUN pip install --no-cache-dir aws-sam-cli
event.json
{}
index.js
exports.handler = function(event, context, callback) {
return callback(null, {
'body': 'Hello, World!'
});
};
template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloWorld:
Type: AWS::Serverless::Function
Properties:
FunctionName: HelloWorld
CodeUri: .
Handler: index.handler
Runtime: nodejs6.10
Timeout: 300
To run SAM locally:
sam local invoke -t template.yml -e event.json HelloWorld
Running SAM locally succeeds:
{"body":"Hello, World!"}
To run SAM under Docker:
docker build -t hello .
docker run \
-v $(pwd):/usr/src/app \
-v /var/run/docker.sock:/var/run/docker.sock \
hello sam local invoke -t template.yml -e event.json HelloWorld
Running SAM under Docker fails:
Unable to import module 'index': Error
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
OS: Ubuntu 16.04.1 x86_64
Docker version: 18.03.1-ce
SAM CLI version: 0.3.0