Skip to content

Commit

Permalink
Fix AWS Lambda handler configuration for Python, Ruby, and custom han…
Browse files Browse the repository at this point in the history
…dlers
  • Loading branch information
ryanblock committed Aug 16, 2023
1 parent 1950235 commit e8b69a8
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 12 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

---

## [3.6.1] 2023-08-15

### Fixed

- Fixed AWS Lambda handler configuration for Python, Ruby, and custom handlers

---

## [3.6.0] 2023-08-02

### Added
Expand Down
12 changes: 10 additions & 2 deletions src/config/pragmas/populate-lambda/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let { sep } = require('path')
let { basename, sep } = require('path')
let { deepFrozenCopy } = require('@architect/utils')
let read = require('../../../read')
let getLambda = require('./get-lambda')
Expand Down Expand Up @@ -132,7 +132,7 @@ function populate (type, pragma, inventory, errors, plugin) {
}

// Now we know the final source dir + runtime + handler: assemble handler props
let handlerProps = getHandler({ config, src, build, errors })
let handlerProps = getHandler({ config, build, errors, src })

let lambda = {
name,
Expand All @@ -144,6 +144,14 @@ function populate (type, pragma, inventory, errors, plugin) {
configFile,
pragma: type !== 'customLambdas' ? type : null,
}
// Ensure the correct handler configuration is being used
// If the config is the same as the default, regenerate the setting based on the returned handlerFile, as Python / Ruby may have it set to `lambda.`
if (config.handler === defaultProjectConfig().handler) {
let { handlerFile } = handlerProps
let file = basename(handlerFile).split('.')[0]
config.handler = `${file}.handler`
}

// Final tidying of any undefined properties
Object.keys(lambda).forEach(k => !is.defined(lambda[k]) && delete lambda[k])

Expand Down
95 changes: 85 additions & 10 deletions test/unit/src/config/pragmas/populate-lambda/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,18 @@ test('Plugin population errors', t => {
}, /Setter plugin exception/, 'Failing setter threw')
})

test('Per-function AWS/ARC config', t => {
t.plan(4)
let inventory = defaultConfig()
test('Per-function AWS/ARC config (including custom handlers)', t => {
t.plan(13)
let arc, config, errors, inventory, lambdas, modified
inventory = defaultConfig()
inventory._project.cwd = '/nada'
inventory._project.src = '/nada/src'
let configPath = `${inventory._project.cwd}/src/events/configured-event/config.arc`
let config = `@aws

// Node.js default
config = `@aws
timeout 10
memory 128
runtime python3.8
@arc
custom setting
Expand All @@ -292,15 +294,88 @@ custom setting
'unconfigured-event',
'configured-event',
]
let arc = { events: inventory.events }
let errors = []
let lambdas = populateLambda.events({ arc, inventory, errors })
arc = { events: inventory.events }
errors = []
lambdas = populateLambda.events({ arc, inventory, errors })
t.deepEqual(lambdas[0].config, inventory._project.defaultFunctionConfig, 'Config was unmodified')
modified = {
timeout: 10,
memory: 128,
runtime: `nodejs16.x`,
handler: 'index.handler',
custom: 'setting',
}
t.deepEqual(lambdas[1].config, { ...inventory._project.defaultFunctionConfig, ...modified }, 'Config was correctly upserted')
t.notOk(errors.length, 'No errors returned')
mockFs.restore()

// Node.js custom configured handler
config = `@aws
timeout 10
memory 128
handler lambda.handler
@arc
custom setting
`
mockFs({ [configPath]: config })
errors = []
lambdas = populateLambda.events({ arc, inventory, errors })
t.deepEqual(lambdas[0].config, inventory._project.defaultFunctionConfig, 'Config was unmodified')
modified = {
timeout: 10,
memory: 128,
runtime: `nodejs16.x`,
handler: 'lambda.handler',
custom: 'setting',
}
t.deepEqual(lambdas[1].config, { ...inventory._project.defaultFunctionConfig, ...modified }, 'Config was correctly upserted')
t.notOk(errors.length, 'No errors returned')
mockFs.restore()

// Python
config = `@aws
timeout 10
memory 128
runtime python3.8
@arc
custom setting
`
mockFs({ [configPath]: config })
errors = []
lambdas = populateLambda.events({ arc, inventory, errors })
t.deepEqual(lambdas[0].config, inventory._project.defaultFunctionConfig, 'Config was unmodified')
let modified = {
modified = {
timeout: 10,
memory: 128,
runtime: `python3.8`,
custom: 'setting'
handler: 'lambda.handler',
custom: 'setting',
}
t.deepEqual(lambdas[1].config, { ...inventory._project.defaultFunctionConfig, ...modified }, 'Config was correctly upserted')
t.notOk(errors.length, 'No errors returned')
mockFs.restore()

// Ruby
config = `@aws
timeout 10
memory 128
runtime ruby2.7
@arc
custom setting
`
mockFs({ [configPath]: config })
errors = []
lambdas = populateLambda.events({ arc, inventory, errors })
t.deepEqual(lambdas[0].config, inventory._project.defaultFunctionConfig, 'Config was unmodified')
modified = {
timeout: 10,
memory: 128,
runtime: `ruby2.7`,
handler: 'lambda.handler',
custom: 'setting',
}
t.deepEqual(lambdas[1].config, { ...inventory._project.defaultFunctionConfig, ...modified }, 'Config was correctly upserted')
t.notOk(errors.length, 'No errors returned')
Expand Down

0 comments on commit e8b69a8

Please sign in to comment.