Skip to content

Commit 96bf37e

Browse files
authored
Merge pull request #4 from UnitedIncome/feature/zipimport
Use zipped libs to deal with aws size limits
2 parents c533dac + dc01040 commit 96bf37e

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,26 @@ and the [docker-lambda](https://github.com/lambci/docker-lambda) image.
4040
To enable docker usage, add the following to your `serverless.yml`:
4141
```yaml
4242
custom:
43-
dockerizePip: true
43+
pythonRequirements:
44+
dockerizePip: true
4445
```
4546

47+
## ZipImport!
48+
To help deal with potentially large dependencies (for example: `numpy`, `scipy`
49+
and `scikit-learn`) there is support for having python import using
50+
[zipimport](https://docs.python.org/2.7/library/zipimport.html). To enable this
51+
add the following to your `serverless.yml`:
52+
```yaml
53+
custom:
54+
pythonRequirements:
55+
zipImport: true
56+
```
57+
58+
4659
## Limitations
47-
* if using the `package` directive in `serverless.yml` ensure that `.requirements` and `requirements.py` are included.
60+
* if using the `package` directive in `serverless.yml` ensure that
61+
`requirements.py` is are included as well as `.requirements` or
62+
`.requirements.zip` if using [ZipImport](#zipimport).
4863

4964

5065
## Manual invocations

index.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const _ = require('lodash');
66
const path = require('path');
77
const fse = require('fs-extra');
88
const child_process = require('child_process');
9+
const Zip = require('adm-zip');
910

1011
BbPromise.promisifyAll(fse);
1112

@@ -18,7 +19,7 @@ class ServerlessPythonRequirements {
1819
path.join(this.serverless.config.servicePath, 'requirements.py'));
1920
};
2021

21-
packRequirements() {
22+
installRequirements() {
2223
if (!fse.existsSync(path.join(this.serverless.config.servicePath, 'requirements.txt'))) {
2324
return BbPromise.resolve();
2425
}
@@ -32,8 +33,7 @@ class ServerlessPythonRequirements {
3233
'-t', '.requirements',
3334
'-r', 'requirements.txt',
3435
];
35-
if (this.serverless.service.custom &&
36-
this.serverless.service.custom.dockerizePip) {
36+
if (this.custom.dockerizePip) {
3737
cmd = 'docker';
3838
options = [
3939
'run', '--rm',
@@ -52,8 +52,25 @@ class ServerlessPythonRequirements {
5252
});
5353
};
5454

55+
packRequirements() {
56+
return this.installRequirements().then(() => {
57+
return new BbPromise((resolve, reject) => {
58+
if (this.custom.zipImport) {
59+
const zip = new Zip();
60+
zip.addLocalFolder('.requirements', '');
61+
zip.writeZip('.requirements.zip');
62+
fse.remove('.requirements', (err) => err?reject():resolve());
63+
} else resolve();
64+
});
65+
});
66+
}
67+
5568
cleanup() {
56-
const artifacts = ['requirements.py', '.requirements'];
69+
const artifacts = ['requirements.py'];
70+
if (this.custom.zipImport)
71+
artifacts.push('.requirements.zip')
72+
else
73+
artifacts.push('.requirements')
5774

5875
return BbPromise.all(_.map(artifacts, (artifact) =>
5976
fse.removeAsync(path.join(this.serverless.config.servicePath, artifact))));;
@@ -76,6 +93,7 @@ class ServerlessPythonRequirements {
7693
constructor(serverless, options) {
7794
this.serverless = serverless;
7895
this.options = options;
96+
this.custom = this.serverless.service.custom && this.serverless.service.custom.pythonRequirements || {};
7997

8098
this.commands = {
8199
'requirements': {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"devDependencies": {},
4141
"dependencies": {
4242
"bluebird": "^3.0.6",
43+
"adm-zip": "0.4.7",
4344
"fs-extra": "^0.26.7",
4445
"lodash": "^4.13.1"
4546
}

requirements.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44

55
requirements = os.path.join(
6-
os.path.split(__file__)[0],
7-
'.requirements',
8-
)
6+
os.path.split(__file__)[0], '.requirements')
7+
zip_requirements = os.path.join(
8+
os.path.split(__file__)[0], '.requirements.zip')
99

10-
if requirements not in sys.path:
10+
if requirements not in sys.path:
1111
sys.path.append(requirements)
12+
13+
if zip_requirements not in sys.path:
14+
sys.path.append(zip_requirements)

0 commit comments

Comments
 (0)