Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
Change layer version usage (#69)
Browse files Browse the repository at this point in the history
* Use latest layer version for all supported runtime

* Add type check for 'userLayerVersion'

* Changes according to CR comments
  • Loading branch information
bcaglayan authored Apr 5, 2022
1 parent aeda46b commit e237d31
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 113 deletions.
59 changes: 32 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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'
}
Expand Down Expand Up @@ -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, ' +
Expand Down
97 changes: 11 additions & 86 deletions src/layers.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down

0 comments on commit e237d31

Please sign in to comment.