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_cognito): (The static method cognito.UserPool.from_user_pool_arn does not work inside a Custom Construct) #26299

Closed
siddhartha2202 opened this issue Jul 10, 2023 · 2 comments
Labels
@aws-cdk/aws-cognito Related to Amazon Cognito bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@siddhartha2202
Copy link

siddhartha2202 commented Jul 10, 2023

Describe the bug

If you create a user pool as of cognito user pool arn from a ConstructsStack(Stack) everything works fine

cognito.UserPool.from_user_pool_arn(
            self,
            "LHUserPool",
            user_pool_arn=(
                "arn:aws:cognito-idp:us-west-2:*********"
                + ":userpool/*********"
            )
        )

But if you execute the previous instrucción inside a Custom Construct send the following error message:

@jsii/kernel.RuntimeError: @jsii/kernel.SerializationError: Passed to parameter scope of static method aws-cdk-lib.aws_cognito.UserPool.fromUserPoolArn: Unable to deserialize value as constructs.Construct

├── 🛑 Failing value is an object
│ {
│ id: 'LHUserPool',
│ existing_user_pool_arn: 'arn:aws:cognito-idp:us-west-2::userpool/****'
│ }
╰── 🔍 Failure reason(s):
╰─ Value does not have the "$jsii.byref" key

Expected Behavior

It shouldn't send any error

Current Behavior

Throws errors if trying to create a UserPool from an arn from a Custom Constructor

Reproduction Steps

  1. Write the following Custom Construct
from constructs import Construct
from attrs import define, field
from aws_cdk import (
    aws_cognito as cognito,
)


@define
class UserPoolBuilder(Construct):
    id: str = field()
    existing_user_pool_arn: str = field()

    def __init__(
        self, scope: Construct, id: str, existing_user_pool_arn: str = None
    ) -> None:
        super().__init__(scope, id)
        self.__attrs_init__(id, existing_user_pool_arn)

        if self.existing_user_pool_arn is not None:
            self.lh_user_pool = cognito.UserPool.from_user_pool_arn(
                scope=self, id=self.id, user_pool_arn=self.existing_user_pool_arn
            )

2.- Invoke this Construct from a Stack

from aws_cdk import (
    Stack,
    aws_cognito as cognito
)
from constructs import Construct
import lh_constructs.authorization_construct as auth_cons

class LhConstructsStack(Stack):
    def __init__(self, scope: Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        auth_cons.UserPoolBuilder(
            self,
            "UserPoolBuilder",
            existing_user_pool_arn=(
                "arn:aws:cognito-idp:us-west-2:********"
                + ":userpool/**********"
            )
        )

3.- The following error appears:

jsii.errors.JavaScriptError:
@jsii/kernel.RuntimeError: @jsii/kernel.SerializationError: Passed to parameter scope of static method aws-cdk-lib.aws_cognito.UserPool.fromUserPoolArn: Unable to deserialize value as constructs.Construct
├── 🛑 Failing value is an object
│ {
│ id: 'UserPoolBuilder',
│ existing_user_pool_arn: 'arn:aws:cognito-idp:us-west-2:*:userpool/'
│ }
╰── 🔍 Failure reason(s):
╰─ Value does not have the "$jsii.byref" key
at Kernel._ensureSync (/tmp/tmp21ftd_ny/lib/program.js:10369:27)
at Kernel.sinvoke (/tmp/tmp21ftd_ny/lib/program.js:9790:34)
at KernelHost.processRequest (/tmp/tmp21ftd_ny/lib/program.js:11544:36)
at KernelHost.run (/tmp/tmp21ftd_ny/lib/program.js:11504:22)
at Immediate._onImmediate (/tmp/tmp21ftd_ny/lib/program.js:11505:46)
at process.processImmediate (node:internal/timers:476:21)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/workspace/latamhub/lh-constructs/app.py", line 10, in <module>
    LhConstructsStack(
  File "/workspace/latamhub/lh-constructs/.venv/lib/python3.9/site-packages/jsii/_runtime.py", line 112, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/workspace/latamhub/lh-constructs/lh_constructs/lh_constructs_stack.py", line 35, in __init__
    auth_cons.UserPoolBuilder(
  File "/workspace/latamhub/lh-constructs/.venv/lib/python3.9/site-packages/jsii/_runtime.py", line 112, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/workspace/latamhub/lh-constructs/lh_constructs/authorization_construct.py", line 23, in __init__
    cognito.UserPool.from_user_pool_arn(
  File "/workspace/latamhub/lh-constructs/.venv/lib/python3.9/site-packages/aws_cdk/aws_cognito/__init__.py", line 15599, in from_user_pool_arn
    return typing.cast(IUserPool, jsii.sinvoke(cls, "fromUserPoolArn", [scope, id, user_pool_arn]))
  File "/workspace/latamhub/lh-constructs/.venv/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 149, in wrapped
    return _recursize_dereference(kernel, fn(kernel, *args, **kwargs))
  File "/workspace/latamhub/lh-constructs/.venv/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 418, in sinvoke
    response = self.provider.sinvoke(
  File "/workspace/latamhub/lh-constructs/.venv/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 381, in sinvoke
    return self._process.send(request, InvokeResponse)
  File "/workspace/latamhub/lh-constructs/.venv/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 340, in send
    raise RuntimeError(resp.error) from JavaScriptError(resp.stack)
RuntimeError: @jsii/kernel.SerializationError: Passed to parameter scope of static method aws-cdk-lib.aws_cognito.UserPool.fromUserPoolArn: Unable to deserialize value as constructs.Construct
├── 🛑 Failing value is an object
│      {
│        id: 'UserPoolBuilder',
│        existing_user_pool_arn: 'arn:aws:cognito-idp:us-west-2:********:userpool/*********'
│      }
╰── 🔍 Failure reason(s):
    ╰─ Value does not have the "$jsii.byref" key

Subprocess exited with error 1

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.86.0 (build 1130fab)

Framework Version

No response

Node.js Version

Node.js v18.16.0

OS

Linux

Language

Python

Language Version

Python 3.9.16

Other information

No response

@siddhartha2202 siddhartha2202 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 10, 2023
@github-actions github-actions bot added the @aws-cdk/aws-cognito Related to Amazon Cognito label Jul 10, 2023
@siddhartha2202
Copy link
Author

Hi,

Apparently using Custom Constructs are not compatible with the attrs library. If I remove this library it works perfectly

@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.

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 bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

1 participant