Using JavaScript ESM in Serverless Framework functions - aws-lambda

I'm using the modern import/export syntax in my NodeJS functions for the Serverless Framework.
After deployment, when I try and invoke the URL I see HTTP 502 {"message": "Internal server error"} error. The CloudWatch logs say:
require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
When I inspect the code through the AWS Console > Lambda, I can see the Serverless Framework has generated a s_getDns.js file for my function, and inside it's using the legacy "require" syntax:
var serverlessSDK = require('./serverless_sdk/index.js');
serverlessSDK = new serverlessSDK({
// ...
});
I'm using v3.23.0 of the Serverless Framework.
I have tried:
Added "type": "module" to the packages.json file
Used the nodejs16.x runtime
(which lead to the error described above)
I have also tried manually editing the s_getDns.js file in the Lambda console to use the new import/export syntax, however I then start getting errors from the serverless_sdk/index.js file (which is minified, so I haven't been able to refactor that one).
This issue comment suggests that both AWS and the Serverless Framework now support ESM out of the box. I'm not sure why it's not working in my case.
What is the issue, or what can I do to troubleshoot this further?

It looks like you're using Serverless Dashboard plugin in your application (you have app and org defined in your config file) and at the moment, ESM modules are not supported by that integration: https://github.com/serverless/serverless/issues/11424#issuecomment-1266584167
You have several options:
Stop using the Serverless Dashboard plugin
Use CJS instead of ESModules
Write a thin wrapper for your handlers in CJS that imports the rest of your code as ESModules

Related

Default AWS Lambda source code for .NET scratch build

I am trying to find the code for the default .NET AWS Lambda function. It does not appear to be one of the Lambda templates and I can't see it in the repository.
I'm talking about the code deployed when "Author from scratch" is selected - before any code is uploaded to the function.
Does anyone know where that source is made available?
I am specifically requesting the source of the .NET Lambda which returns "Hello from Lambda!" when the root Function URL is requested.

Download Gitlab's graphql schema

I'm trying to get hold of gitlab_schema.graphql, i.e. the schema for Gitlab's graphql API. Can't find it anywhere, does anyone have any pointers?
You can use apollo CLI for this: apollo client:download-schema --endpoint=https://gitlab.yourcompany.com/api/graphql schema.graphql or just omit filename and it will download to JSON
It doesn't look like GitLab shares the schema directly, just this Reference Documentation generated from the schema, and the GraphiQL Explorer / IDE (more info about GraphiQL Explorer here).
I found the schema is an artifact in the Gitlab CI pipe (graphql-schema-dump job), for example here: https://gitlab.com/gitlab-org/gitlab/-/jobs/1602029144.
Not perfect, but works.
Here's alternative, if you are working with Rust lang. For Rust developers, you need to install the crate named graphql_client_cli via cargo. Example: cargo install graphql_client_cli.
Next, you can run the graphql client you just downloaded, to get the schema (either in JSON or GraphQL format). Example: graphql-client --authorization 'Bearer <bearer token>' https://<your gitlab url site>/api/graphql.
See the GraphQL Client CLI for more options.

Passing code from CodePipeline to PythonFunction

I'm trying to create a CDK app that will deploy a pipeline-stack and a lambda-stack. Similar to the tutorial here. I'm trying to implement a basic CI/CD application that is triggered with every push to a Github Enterprise Repo.
I chose to use PythonFunction from (#aws-cdk/aws-lambda-python) instead of function from #aws-cdk/aws-lambda because PythonFunction builds the dependencies from requirements.txt. I have various lambdas that use different packages (like awswrangler, pandas, requests, etc.).
But, PythonFunction does not support CfnParametersCode (Where the code is passed through CDK instead of being read from an asset).
What other option do I have to pass my code from GithubEnterprise to
the PythonFunction?
If function from #aws-cdk/aws-lambda is the only option I have, how
can I include the packages from requirements.txt
This does seem like an option for #aws-cdl/aws-lambda, but how would I pass my code from Github? This example relates to building from asset code.
I apologize if I'm missing something obvious, I just started working with AWS CDK last week.
First of all I would recommend to take a look at pipelines.CdkPipeline which is able to deal with Assets. That means you can directly use lambda.Code.from_asset instead of overriding CfnParametersCode in the Pipeline.
Regarding your other question, you can deal with the requirements by installing them into your lambda folder during the build step with: pip install -r requirements.txt -t .
CfnParametersCode gives you the ability to upload your code from an S3 file.
You can do the same via lambda.Code.fromBucket.
Taking your link from the third point (https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-lambda#bundling-asset-code) You just need to use lambda.Code.fromBucket instead of code: lambda.Code.fromAsset. Docs can be found here: https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-lambda

How to get Rest Api endpoints, name and url's, in the way Azure Functions do?

Sometimes you get over a feature that you just like. When creating a Azure Function project and debugs it, it writes out this nice info in the console.
Now that I am writing the frontend, I have found this information very useful.
Functions:
CoWorkers: [GET,POST] http://localhost:7071/api/CoWorkers
GetManager: [GET,POST] http://localhost:7071/api/GetManager
SetManager: [GET,POST] http://localhost:7071/api/SetManager
UserInfo: [GET,POST] http://localhost:7071/api/UserInfo
...
I would like to have this on every asp.net rest api project that I am coding.
Anyone knows how to get it?
EDIT: OpenApi/Swagger is providing this information as pawelek91 says and I should have mentioned that I want it in the console: "Just because I like that feature".
use HttpOption request or swagger (if you can install it on your backend)
For console access to your web API, try the new HttpRepl tool at:
https://learn.microsoft.com/en-us/aspnet/core/web-api/http-repl/
It's a dotnet CLI tool that is installed separately (install using dotnet tool install -g Microsoft.dotnet-httprepl from the CL). Then you can "browse" your API using this command line tool.
As noted in the docs, it requires .Net Core 2.1+.

how to get the node_modules folder in an aws lambda function

So I am learning how to create alexa skills and I have create two sucessfully following the steps below but for some reason when I follow these steps now I am missing the node_modules folder and the package.json. This is causing the following error when I test my skill:
Unable to import module 'index': Error
The steps I am following are:
- go to the aws console select lambda
- select create new function
- blueprints -> select alexa-skill-kit-sdk-factskill -> configure
- select existing role -> lambda_basic_execution
- create function
It was my understanding that the alexa-skill-kit-sdk-factskill came with these dependencies. Is there a way to add them in manually if so how? I have tried to find a console but I don't see where that is in the lambda workspace.
Here is a screen shots of the project where it did add the folder and json file
Here is on of my current project
I have had this problem, but unfortunately, Lambda console doesn't give us console to install nodejs dependencies.
So, you have to actually deploy your lambda function. There are many ways to do that, but more popular are:
Node Lambda package (for node)
Create ZIP and deploy it in console (Not recommended because of size limitation)
AWS CLI
As you're learning the Alexa Skills, I would suggest you the way Amazon suggests devs to create and organise Alexa Skill and Lambda function.
Use ASK CLI
For creating new Alexa Skill.
This will create a directory, which will have:
Skill's intent Schema
Skill's lambda function
Plus, you can pass a template to create skill for a specific template. Here is list of templates created by Amazon devs.
For eg, command for creating Skill code, for a fact-template skill
ask new [--template [template-name] [--url <url>]]
[-n|--skill-name <name>]
[-p| --profile <profile>] [--lambda-name <lambda-name>]
Using ASK CLI, will give you the advantage of deploying your skill and lambda simultaneously.
I know ASK CLI is a lot to take in so quickly, but once you get the hang of it, it is a very useful tool for Alexa developers.
You can zip the whole directory content locally and upload the zip to
lambda. But yeah you can't edit the code in the console then.
Remember that your handler file name must be mentioned as it is in Handler section.
Like if your file name is app.js then app.handler.

Resources