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

Lambda Custom Runtime via Docker Support #1246

Closed
wants to merge 11 commits into from
6,180 changes: 3,082 additions & 3,098 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
"Bryan Vaz (https://github.com/bryanvaz)",
"Justin Ng (https://github.com/njyjn)",
"Fernando Alvarez (https://github.com/jefer590)",
"Grady Rogers (https://github.com/rogersgt)",
"Eric Carter (https://github.com/ericctsf)"
],
"engines": {
Expand Down
6 changes: 4 additions & 2 deletions src/ServerlessOffline.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import process, { exit } from 'node:process'
import { log } from '@serverless/utils/log.js'
import { getHandlerName } from './utils/index.js'
import {
commandOptions,
CUSTOM_OPTION,
Expand Down Expand Up @@ -275,10 +276,11 @@ export default class ServerlessOffline {
events.forEach((event) => {
const { http, httpApi, schedule, websocket } = event

if ((http || httpApi) && functionDefinition.handler) {
const handlerName = getHandlerName(functionDefinition)
if ((http || httpApi) && handlerName) {
const httpEvent = {
functionKey,
handler: functionDefinition.handler,
handler: handlerName,
http: http || httpApi,
}

Expand Down
5 changes: 3 additions & 2 deletions src/lambda/LambdaFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
DEFAULT_LAMBDA_TIMEOUT,
supportedRuntimes,
} from '../config/index.js'
import { createUniqueId } from '../utils/index.js'
import { createUniqueId, getHandlerName } from '../utils/index.js'

const { ceil } = Math
const { entries, fromEntries } = Object
Expand Down Expand Up @@ -76,7 +76,8 @@ export default class LambdaFunction {
// TODO FIXME look into better way to work with serverless-webpack
const servicepath = resolve(servicePath, options.location || '')

const { handler, name, package: functionPackage = {} } = functionDefinition
const { name, package: functionPackage = {} } = functionDefinition
const handler = getHandlerName(functionDefinition)

// this._executionTimeout = null
this.#functionKey = functionKey
Expand Down
44 changes: 44 additions & 0 deletions src/utils/__tests__/getHandlerName.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import getHandlerName from '../getHandlerName.js'

const tests = [
{
input: {
image: {
command: ['app.handler'],
name: 'node14',
},
events: [
{
httpApi: {
method: 'get',
path: '/',
},
},
],
},
expected: 'app.handler',
},
{
input: {
handler: 'app.handler',
events: [
{
httpApi: {
method: 'get',
path: '/',
},
},
],
},
expected: 'app.handler',
},
]

describe('getHandlerName', () => {
tests.forEach(({ input, expected }) => {
test('Should return the correct handler name', () => {
const handlerValue = getHandlerName(input)
expect(handlerValue).toEqual(expected)
})
})
})
16 changes: 16 additions & 0 deletions src/utils/getHandlerName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default function getHandlerName(functionDefinition) {
const { handler, image } = functionDefinition

if (handler) {
return handler
}

const { command } = image
if (!command || command.length < 1) {
throw new Error(
`Unable to determine handler name. Be sure to include a "command" property when using a docker image as a custom runtime. See https://www.serverless.com/blog/container-support-for-lambda`,
)
}

return command[0]
}
2 changes: 1 addition & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export { default as parseMultiValueQueryStringParameters } from './parseMultiVal
export { default as parseQueryStringParameters } from './parseQueryStringParameters.js'
export { default as parseQueryStringParametersForPayloadV2 } from './parseQueryStringParametersForPayloadV2.js'
export { default as splitHandlerPathAndName } from './splitHandlerPathAndName.js'

export { default as getHandlerName } from './getHandlerName.js'
// export { default as baseImage } from './baseImage.js'

const { isArray } = Array
Expand Down