Skip to content

Commit 222fa63

Browse files
committed
Cross compilation using docker! closes #2
1 parent 906ce0f commit 222fa63

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ import requirements
3232
import requests
3333
```
3434

35-
## Limitations
35+
## Cross compiling!
36+
Compiling non-pure-Python modules is supported on MacOS via the use of Docker
37+
and the [docker-lambda](https://github.com/lambci/docker-lambda) image.
38+
To enable docker usage, add the following to your `serverless.yml`:
39+
```yaml
40+
custom:
41+
dockerizePip: true
42+
```
3643

37-
* MacOS & Windows: For now this only works with pure Python modules unless running serverless on the same architeture as AWS (x86_64 Linux).
44+
## Limitations
3845
* if using the `package` directive in `serverless.yml` ensure that `.requirements` and `requirements.py` are included.
3946

4047

index.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,29 @@ class ServerlessPythonRequirements {
1919
};
2020

2121
packRequirements() {
22-
const requirementsFile = path.join(this.serverless.config.servicePath, 'requirements.txt');
23-
24-
if (!fse.existsSync(requirementsFile)) {
22+
if (!fse.existsSync(path.join(this.serverless.config.servicePath, 'requirements.txt'))) {
2523
return BbPromise.resolve();
2624
}
2725

2826
this.serverless.cli.log('Packaging required Python packages...');
2927

3028
return new BbPromise((resolve, reject) => {
31-
const res = child_process.spawnSync('pip', [
29+
let cmd = 'pip';
30+
let options = [
3231
'install',
33-
'-t',
34-
path.join(this.serverless.config.servicePath, '.requirements'),
35-
'-r',
36-
requirementsFile,
37-
]);
32+
'-t', '.requirements',
33+
'-r', 'requirements.txt',
34+
];
35+
if (this.serverless.service.custom &&
36+
this.serverless.service.custom.dockerizePip) {
37+
cmd = 'docker';
38+
options = [
39+
'run', '--rm',
40+
'-v', `${this.serverless.config.servicePath}:/var/task:z`,
41+
'lambci/lambda:build-python2.7', 'pip',
42+
].concat(options);
43+
}
44+
const res = child_process.spawnSync(cmd, options);
3845
if (res.error) {
3946
return reject(res.error);
4047
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-python-requirements",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"engines": {
55
"node": ">=4.0"
66
},

0 commit comments

Comments
 (0)