Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Serverless Container Support #723

Merged
merged 7 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,28 @@ custom:
```
Will run each webpack build one at a time which helps reduce memory usage and in some cases impoves overall build performance.

### Support for Docker Images as Custom Runtimes
AWS Lambda and `serverless` started supporting the use of Docker images as custom runtimes in 2021. See the [serverless documentation](https://www.serverless.com/blog/container-support-for-lambda) for details on how to configure a `serverless.yml` to use these features.

**NOTE: You must provide an override for the Image `CMD` property in your function definitions.**
See [Dockerfile documentation](https://docs.docker.com/engine/reference/builder/#cmd) for more information about the native Docker `CMD` property.

In the following example `entrypoint` is inherited from the shared Docker image, while `command` is provided as an override for each function:
```yaml
# serverless.yml
functions:
myFunction1:
image:
name: public.ecr.aws/lambda/nodejs:12
command:
- app.handler1
myFunction2:
image:
name: public.ecr.aws/lambda/nodejs:12
command:
- app.handler2
```

## Usage

### Automatic bundling
Expand Down
21 changes: 19 additions & 2 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ const preferredExtensions = [ '.js', '.ts', '.jsx', '.tsx' ];

module.exports = {
validate() {
const getHandlerFileAndFunctionName = functionDefinition => {
const { handler: handlerProp, image: imageProp } = functionDefinition;

if (handlerProp) {
return handlerProp;
}

if (!imageProp || !imageProp.command || imageProp.command.length < 1) {
const docsLink = 'https://www.serverless.com/blog/container-support-for-lambda';
throw new this.serverless.classes.Error(
`Either function.handler or function.image must be defined. Pass the handler name (i.e. 'index.handler') as the value for function.image.command[0]. For help see: ${docsLink}`
);
}

return imageProp.command[0];
};

const getHandlerFile = handler => {
// Check if handler is a well-formed path based handler.
const handlerEntry = /(.*)\..*?$/.exec(handler);
Expand Down Expand Up @@ -59,7 +76,7 @@ module.exports = {
};

const getEntryForFunction = (name, serverlessFunction) => {
const handler = serverlessFunction.handler;
const handler = getHandlerFileAndFunctionName(serverlessFunction);

const handlerFile = getHandlerFile(handler);
if (!handlerFile) {
Expand Down Expand Up @@ -213,7 +230,7 @@ module.exports = {
}),
funcName => {
const func = this.serverless.service.getFunction(funcName);
const handler = func.handler;
const handler = getHandlerFileAndFunctionName(func);
const handlerFile = path.relative('.', getHandlerFile(handler));
return {
handlerFile,
Expand Down
57 changes: 52 additions & 5 deletions tests/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,20 @@ describe('validate', () => {
}
}
]
},
dockerfunc: {
image: {
name: 'some-image-uri',
command: ['com.serverless.Handler']
},
events: [
{
http: {
method: 'POST',
path: 'mydockerfuncpath'
}
}
]
}
};

Expand Down Expand Up @@ -541,14 +555,15 @@ describe('validate', () => {
return expect(module.validate()).to.be.fulfilled.then(() => {
const lib = require('../lib/index');
const expectedLibEntries = {
'com.serverless': './com.serverless.js',
module1: './module1.js',
module2: './module2.js',
'handlers/func3/module2': './handlers/func3/module2.js',
'handlers/module2/func3/module2': './handlers/module2/func3/module2.js'
};

expect(lib.entries).to.deep.equal(expectedLibEntries);
expect(globSyncStub).to.have.callCount(4);
expect(globSyncStub).to.have.callCount(5);
expect(serverless.cli.log).to.not.have.been.called;
return null;
});
Expand Down Expand Up @@ -629,6 +644,24 @@ describe('validate', () => {
}
],
runtime: 'provided'
},
func4: {
artifact: 'artifact-func4.zip',
events: [
{
http: {
method: 'POST',
path: 'func4path'
}
},
{
nonhttp: 'non-http'
}
],
image: {
name: 'custom-image',
command: ['module4.func1handler']
}
}
};

Expand All @@ -650,11 +683,12 @@ describe('validate', () => {
const lib = require('../lib/index');
const expectedLibEntries = {
module1: './module1.js',
module2: './module2.js'
module2: './module2.js',
module4: './module4.js'
};

expect(lib.entries).to.deep.equal(expectedLibEntries);
expect(globSyncStub).to.have.callCount(2);
expect(globSyncStub).to.have.callCount(3);
expect(serverless.cli.log).to.not.have.been.called;
return null;
});
Expand Down Expand Up @@ -777,6 +811,15 @@ describe('validate', () => {
key: 'handlers/module2/func3/module2',
value: './handlers/module2/func3/module2.js'
}
},
{
handlerFile: 'com.serverless',
funcName: 'dockerfunc',
func: testFunctionsConfig.dockerfunc,
entry: {
key: 'com.serverless',
value: './com.serverless.js'
}
}
]);
return null;
Expand All @@ -788,11 +831,12 @@ describe('validate', () => {
module.serverless.service.functions = testFunctionsConfig;
globSyncStub.callsFake(filename => [_.replace(filename, '*', 'js')]);
return expect(module.validate()).to.be.fulfilled.then(() => {
expect(module.webpackConfig).to.have.lengthOf(4);
expect(module.webpackConfig).to.have.lengthOf(5);
expect(module.webpackConfig[0].output.path).to.equal(path.join('output', 'func1'));
expect(module.webpackConfig[1].output.path).to.equal(path.join('output', 'func2'));
expect(module.webpackConfig[2].output.path).to.equal(path.join('output', 'func3'));
expect(module.webpackConfig[3].output.path).to.equal(path.join('output', 'func4'));
expect(module.webpackConfig[4].output.path).to.equal(path.join('output', 'dockerfunc'));

return null;
});
Expand All @@ -813,19 +857,22 @@ describe('validate', () => {
module.serverless.service.functions = testFunctionsConfig;
globSyncStub.callsFake(filename => [_.replace(filename, '*', 'js')]);
return expect(module.validate()).to.be.fulfilled.then(() => {
expect(module.webpackConfig).to.have.lengthOf(4);
expect(module.webpackConfig).to.have.lengthOf(5);
expect(module.webpackConfig[0].devtool).to.equal('source-map');
expect(module.webpackConfig[1].devtool).to.equal('source-map');
expect(module.webpackConfig[2].devtool).to.equal('source-map');
expect(module.webpackConfig[3].devtool).to.equal('source-map');
expect(module.webpackConfig[4].devtool).to.equal('source-map');
expect(module.webpackConfig[0].context).to.equal('some context');
expect(module.webpackConfig[1].context).to.equal('some context');
expect(module.webpackConfig[2].context).to.equal('some context');
expect(module.webpackConfig[3].context).to.equal('some context');
expect(module.webpackConfig[4].context).to.equal('some context');
expect(module.webpackConfig[0].output.libraryTarget).to.equal('commonjs');
expect(module.webpackConfig[1].output.libraryTarget).to.equal('commonjs');
expect(module.webpackConfig[2].output.libraryTarget).to.equal('commonjs');
expect(module.webpackConfig[3].output.libraryTarget).to.equal('commonjs');
expect(module.webpackConfig[4].output.libraryTarget).to.equal('commonjs');
return null;
});
});
Expand Down