diff --git a/src/index.js b/src/index.js index d3a1126..84340b1 100644 --- a/src/index.js +++ b/src/index.js @@ -361,17 +361,12 @@ class ServerlessThundraPlugin { this.warnNoLayerInfoExistsForLang(lang) } const { delegatedHandlerEnvVarName, layerAwsAccountNo } = layerInfo - const userLayerVersion = getUserLayerVersion(func, service, lang) const { layerName, - defaultLayerVersion, thundraHandlerName, needHandlerDelegation, customRuntime, - } = - typeof layerInfo[lang] === 'function' - ? layerInfo[lang](func, service, userLayerVersion) - : layerInfo[lang] + } = layerInfo[lang]; let skipHandlerDelegation = false const delegatedHandler = func.environment[delegatedHandlerEnvVarName] @@ -416,6 +411,32 @@ class ServerlessThundraPlugin { return } + if (!skipLayerAddition) { + const userLayerVersion = getUserLayerVersion(func, service, lang) + if (userLayerVersion) { + if (typeof userLayerVersion === 'string' + && userLayerVersion.toLowerCase() === 'latest') { + func.layers.push(this.latestLayerArnMap[func.runtime]) + } else if (!this.isValidLayerVersion(userLayerVersion)) { + this.warnNoLayerInfoExistsForLangAndVersion(lang, userLayerVersion) + } else { + const layerRegion = service.provider.region + const layerARN = getLayerARN( + layerRegion, + layerAwsAccountNo, + layerName, + userLayerVersion + ) + + func.layers.push(layerARN) + } + } else { + func.layers.push(this.latestLayerArnMap[func.runtime]) + } + } else { + this.warnLayerAlreadyExists(funcName) + } + if (needHandlerDelegation) { if (!skipHandlerDelegation) { func.environment[delegatedHandlerEnvVarName] = func.handler @@ -424,27 +445,7 @@ class ServerlessThundraPlugin { this.warnHandlerDelegationSkipped(funcName) } } - - if (!skipLayerAddition) { - const layerRegion = service.provider.region - const layerVersion = this.isValidLayerVersion(userLayerVersion) - ? userLayerVersion - : defaultLayerVersion - const layerARN = getLayerARN( - layerRegion, - layerAwsAccountNo, - layerName, - layerVersion - ) - if (layerVersion === 'latest') { - func.layers.push(this.latestLayerArnMap[func.runtime]) - } else { - func.layers.push(layerARN) - } - } else { - this.warnLayerAlreadyExists(funcName) - } - + if (customRuntime) { func.runtime = 'provided' } @@ -595,6 +596,10 @@ class ServerlessThundraPlugin { this.log('No layer information exist for given lang: ' + lang) } + warnNoLayerInfoExistsForLangAndVersion(lang, version) { + this.log(`No layer information exist for given lang: ${lang} given version: ${version}`) + } + warnHandlerDelegationSkipped(funcName) { this.log( 'Thundra handler was already set and delegated to original handler, ' + diff --git a/src/layers.js b/src/layers.js index 82424a0..4acbb84 100644 --- a/src/layers.js +++ b/src/layers.js @@ -1,105 +1,30 @@ const get = require('lodash.get') exports.layerInfo = { - java: getJavaLayerProps, + java: { + layerName: 'thundra-lambda-java-layer', + thundraHandlerName: + 'io.thundra.agent.lambda.core.handler.ThundraLambdaHandler', + needHandlerDelegation: true, + }, dotnetcore: { layerName: 'thundra-lambda-dotnetcore21-layer', - defaultLayerVersion: '6', needHandlerDelegation: false, customRuntime: true, }, python: { layerName: 'thundra-lambda-python-layer', - defaultLayerVersion: '38', thundraHandlerName: 'thundra.handler.wrapper', needHandlerDelegation: true, }, - node: getNodeLayerProps, - layerAwsAccountNo: 269863060030, - delegatedHandlerEnvVarName: 'thundra_agent_lambda_handler', -} - -function getNodeLayerProps(func, service, userLayerVersion) { - const optsWithCR = { - layerName: 'thundra-lambda-node-layer', - defaultLayerVersion: '70', - needHandlerDelegation: false, - customRuntime: true, - } - - const optsWithoutCR = { + node: { layerName: 'thundra-lambda-node-layer', - defaultLayerVersion: '70', needHandlerDelegation: true, thundraHandlerName: - '/opt/nodejs/node_modules/@thundra/core/dist/handler.wrapper', - } - - const optsMinified = { - layerName: 'thundra-lambda-node-layer-minified', - defaultLayerVersion: '70', - needHandlerDelegation: true, - thundraHandlerName: 'thundra_handler.wrapper', - } - - try { - const withoutCRVersionThreshold = 32 - const minifiedLayerThreshold = 57 - - const eligibleForWithoutCR = - userLayerVersion === undefined || - userLayerVersion === 'latest' || - Number(userLayerVersion) > withoutCRVersionThreshold - - const useCustomRuntime = - get(func, 'custom.thundra.useCustomRuntime') || - get(service, 'custom.thundra.useCustomRuntime') || - false - - const versionStr = func.runtime.split('nodejs')[1].split('.')[0] - const version = Number(versionStr) - - if (!eligibleForWithoutCR || useCustomRuntime || version <= 8) { - return optsWithCR - } - - const eligibleForMinified = - userLayerVersion === undefined || - userLayerVersion === 'latest' || - Number(userLayerVersion) >= minifiedLayerThreshold - - return eligibleForMinified ? optsMinified : optsWithoutCR - } catch (e) { - return optsWithCR - } -} - -function getJavaLayerProps(func, service, userLayerVersion) { - const optsWithoutCR = { - layerName: 'thundra-lambda-java-layer', - defaultLayerVersion: '56', - thundraHandlerName: - 'io.thundra.agent.lambda.core.handler.ThundraLambdaHandler', - needHandlerDelegation: true, - } - - const optsWithCR = { - layerName: 'thundra-lambda-java-layer', - defaultLayerVersion: '56', - customRuntime: true, - needHandlerDelegation: false, - } - - const useCustomRuntime = - get(func, 'custom.thundra.useCustomRuntime') || - get(service, 'custom.thundra.useCustomRuntime') || - false - - if (useCustomRuntime) { - return optsWithCR - } - - return useCustomRuntime ? optsWithCR : optsWithoutCR + 'thundra_handler.wrapper', + }, + layerAwsAccountNo: 269863060030, + delegatedHandlerEnvVarName: 'THUNDRA_AGENT_LAMBDA_HANDLER', } exports.getLayerARN = (region, accountNo, name, version) => {