-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lambda: Cannot create ES Module function w/Code.fromInline #27980
Comments
Yes this could be a solution. We'll look into this. |
Another, IMO easier solution would be, to add new NodejsFunction(stack, 'MyFunc', {
runtime: Runtime.NODEJS_LATEST,
code: Code.fromInline("import { DynamoDBClient } from '@aws-sdk/client-dynamodb'"),
handler: 'index.handler',
bundling: {
format: OutputFormat.ESM
}
}) |
Is it even possible to set filename or extension when using |
Hmm... I may not follow. Could you elaborate? When we zip, we zip the whole folder. There, we don't care about the extension. new NodejsFunction(stack, 'MyFunc', {
runtime: Runtime.NODEJS_LATEST,
code: Code.fromInline("import { DynamoDBClient } from '@aws-sdk/client-dynamodb'"),
handler: 'index.handler',
bundling: {
format: OutputFormat.ESM,
esbuildArgs: {
"--outfile": "index.mjs"
}
}
}) |
Hi @jolo-dev aws-cdk/packages/aws-cdk-lib/aws-lambda/lib/function.ts Lines 902 to 911 in c66e197
so the constraints of CloudFormation Is there any reason not to use code from a file? With code from a file, you can use NodejsFunction to get ESM code. |
@tmokmss You're right. I once had a situation where I needed a simple Lambda to pass the event to trigger something. |
Ran into this same limitation. CDK created index.js, not index.mjs when I used the fromInline function. |
This is a consequence of the underlying CloudFormation support for inline Lambda functions. Please see aws-cloudformation/cloudformation-coverage-roadmap#1420 for updates. |
Describe the bug
Lambda Node Runtimes support loading ESM code since the
nodejs14.x
runtime.If there is a single file in the zip, it needs to have the
.mjs
extension to be loaded as an ES module. Other option is providing apackage.json
file with"type": "module"
in it.Currently, there is no way to tell CDK to set the file extension when
lambda.Code.fromInline
is used:Expected Behavior
Not getting
Runtime.UserCodeSyntaxError
from the lambda invocation.Current Behavior
Error on lambda invoke:
only because the file name in the deployment package is
index.js
.Reproduction Steps
Deploy and invoke the function.
This function handler doesn't do anything useful, other than importing from the SDK as an ES Module. It will deploy successfully and invocations will fail.
Possible Solution
Add a new argument in
lambda.Code.fromInline
that specifies the filename,index.mjs
would fix it for the example.Additional Information/Context
No response
CDK CLI Version
any
Framework Version
No response
Node.js Version
14,16,18
OS
any
Language
TypeScript
Language Version
No response
Other information
No response
The text was updated successfully, but these errors were encountered: