Last active
September 6, 2019 10:56
-
-
Save chris-kobrzak/c30bbc7d4cb58f43af1778e561505130 to your computer and use it in GitHub Desktop.
Injecting dependencies to an AWS Lambda function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dependency injection helper | |
const injectLambdaDependencies = (handle, dependencyMap) => (event, context) => { | |
const enhancedContext = Object.assign({}, context, dependencyMap) | |
return handle(event, enhancedContext) | |
} | |
// Dependencies, possibly returned by a factory | |
const dependencyMap = { doSomething: () => 'foo bar' } | |
// Lambda function | |
async function handle(event, context) { | |
console.log('context', context) | |
} | |
// Enhanced Lambda - this is what needs to be exported as the actual Lambda function | |
const handleWithDependencies = injectLambdaDependencies(handle, dependencyMap) | |
// When `handleWithDependencies` is invoked, `doSomething` will now be available on the `context` object. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does it also make your serverless environment variables available to the lambda function?