Skip to content
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

(@aws-cdk/aws-cognito): import 'punycode/' #14290

Closed
ams671 opened this issue Apr 21, 2021 · 5 comments
Closed

(@aws-cdk/aws-cognito): import 'punycode/' #14290

ams671 opened this issue Apr 21, 2021 · 5 comments
Assignees
Labels
@aws-cdk/aws-cognito Related to Amazon Cognito guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@ams671
Copy link

ams671 commented Apr 21, 2021

❓ General Issue

The Question

about "@aws-cdk/aws-cognito/lib/user-pool.ts".
I don't think punycode's trailing slash is necessary.

current code
import { toASCII as punycodeEncode } from 'punycode/';
expected code
import { toASCII as punycodeEncode } from 'punycode';

Environment

  • CDK CLI Version: v1.100.0
  • Module Version:
  • Node.js Version:
  • OS:
  • Language (Version):

Other information

An error occurred when importing '@aws-cdk/aws-cognito' in the Lambda function handler.

Lambda Runtime Error
{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'punycode/'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    ...
}
@ams671 ams671 added guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. labels Apr 21, 2021
@github-actions github-actions bot added the @aws-cdk/aws-cognito Related to Amazon Cognito label Apr 21, 2021
@nija-at
Copy link
Contributor

nija-at commented Apr 21, 2021

I don't think punycode's trailing slash is necessary.

Actually, the trailing slash is crucial. See #11099 (comment)

I'm not sure I understand what issue you're running into. Can you describe your actual issue?

@nija-at nija-at added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Apr 21, 2021
@ams671
Copy link
Author

ams671 commented Apr 22, 2021

I understand.
Thank you for the information.

I solved my issue by creating Lambda Layer for punycode module.

information

I was tring to deploy CDK's stack from within Lambda function handler.

sample code (Lambda function handler)

import { App, Stack } from '@aws-cdk/core'
import { UserPool } from '@aws-cdk/aws-cognito'
import { SdkProvider } from 'aws-cdk/lib/api/aws-auth'
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments'

exports.handler = async (event: any) => {
    const app = new App()
    const stack = new Stack(app, 'SampleStack')

    new UserPool(stack, 'SampleUserPool')

    const deployer = new CloudFormationDeployments({
        sdkProvider: await SdkProvider.withAwsCliCompatibleDefaults()
    })

    await deployer.deployStack({
        stack: app.synth().stacks[0],
        quiet: true
    })

    return {
        statusCode: 200,
        body: 'ok'
    }
}

It remains require("punycode/") when processed this code by esbuild,
it causes "Error: Cannot find module 'punycode/'".

esbuild result

...
 var punycode_1 = require("punycode/");
...

I understood that punycode/ isn't core module,
so I solved by creating Lambda Layer for punycode module

@ams671 ams671 closed this as completed Apr 22, 2021
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@danielholmes
Copy link
Contributor

I had this error as well and was able to fix it through some code changes. For me it was an error in my code organisation.

In my case it was to do with mixing devDependencies/cdk code with my runtime lambda code.

e.g. I had some files similar to:

package.json

{
 ...
  "devDependencies": {
    "@aws-cdk/aws-lambda-nodejs": "^1.114.0",
    "@aws-cdk/aws-cognito": "^1.114.0",
    ...
  }
}

users/index.ts

export { default as Construct } from "./construct";
export { default as getViewerResolver } from "./getViewerResolver";

users/construct.ts

import { UserPool } "@aws-cdk/aws-cognito"
// ...

api/construct.ts

// ...
class ApiConstruct extends Construct {
  constructor() {
    //...
    const defaultResolverLambda = new NodejsFunction(
      this,
      "DefaultGraphQlResolver",
      {
        runtime: Runtime.NODEJS_14_X,
        entry: path.join(__dirname, "..", "users", "index.ts"),
        handler: "getViewerResolver"
      }
    );
  }
}

including that users/index.ts file as an entry therefore included the users/construct.ts which tried to include some cdk cognito code which wasn't part of my runtime dependencies.

So the fix was to make sure my construct/cdk code was completely separate from my runtime lambda code.

@gorjusborg
Copy link

gorjusborg commented Jan 20, 2022

Thanks for posting this @danielholmes, I had the same issue.

We had some Custom::Trigger that would run to populate user data on deploy. Unfortunately, the lambda handler was thrown in the same module as some CDK code that was loosely related. This caused esbuild to bundle aws-cdk-lib/aws-cognito in the lambda, which declares a 'bundledDependency' on 'punycode/', but apparently it isn't bundled in the cdk v2 library (hence the require).

This popped up on upgrade to CDK v2 after working with CDK v1 with an error like Runtime.ImportModuleError: Error: Cannot find module 'punycode/'.

The answer for me was to isolate the lambda code so that NO CDK code was referenced (even transitively) by module imports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-cognito Related to Amazon Cognito guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

4 participants