Skip to content

Instantly share code, notes, and snippets.

@chris-kobrzak
Last active September 6, 2019 10:56
Show Gist options
  • Save chris-kobrzak/c30bbc7d4cb58f43af1778e561505130 to your computer and use it in GitHub Desktop.
Save chris-kobrzak/c30bbc7d4cb58f43af1778e561505130 to your computer and use it in GitHub Desktop.
Injecting dependencies to an AWS Lambda function
// 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.
@prabhatthapa
Copy link

Does it also make your serverless environment variables available to the lambda function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment