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

Stack: I can not use stack as param in another stack #26053

Closed
wormym1011 opened this issue Jun 20, 2023 · 4 comments
Closed

Stack: I can not use stack as param in another stack #26053

wormym1011 opened this issue Jun 20, 2023 · 4 comments
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@wormym1011
Copy link

wormym1011 commented Jun 20, 2023

Describe the bug

Code from app.py

import os

import aws_cdk as cdk
from edla_vpc.VPCStack import VPCStack
from edla_vpc.VPCRoutingStack import VPCRoutingStack

app = cdk.App()
VPCStack(app, "VPCStack")
VPCRoutingStack(app, "VPCRoutingStack", vpc_stack=VPCStack)

Code from VPCStack.py

#!/usr/bin/env python3
from aws_cdk import (
    Stack,
    CfnOutput,
    aws_ec2 as ec2,
)
from constructs import Construct
import os
from dotenv import load_dotenv

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

        self.vpc = ec2.Vpc(self, "VPC", .....)
    
    @property
    def get_vpc_id(self) -> str:
        return self.vpc.vpc_id
    
    
    @property
    def get_Ivpc(self):
        return self.vpc

Code from VPCRoutingStack.py

#!/usr/bin/env python3
from aws_cdk import (
    Stack,
    CfnOutput,
    aws_ec2 as ec2
)
from constructs import Construct
import os
from dotenv import load_dotenv

load_dotenv()
VPC_CIDR = os.getenv("VPC_CIDR")
DX_TGW_ID = os.getenv("DX_TGW_ID")
REGION = os.getenv("REGION")

class VPCRoutingStack(Stack):
   
    def __init__(self, scope: Construct, id: str, vpc_stack, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)
        # str(vpc_stack.get_vpc_id) something likes <property object at 0x10d389e50>	
        CfnOutput(self, id, value=str(vpc_stack.get_vpc_id), export_name="tired")
        
        tgw_attachment = ec2.CfnTransitGatewayAttachment(
            self,
            "DXTransitGatewayAttachment",
            subnet_ids=[
                vpc_stack.get_etl_subnet_id_1a,
                vpc_stack.get_etl_subnet_id_1b,
                vpc_stack.get_etl_subnet_id_1c
            ],
            transit_gateway_id=DX_TGW_ID,
            vpc_id=vpc_stack.get_vpc_id,
        )

Error return:

  File "/Users/wormym/cdp-cdk/.venv/lib/python3.8/site-packages/aws_cdk/__init__.py", line 5918, in __init__
    check_type(argname="argument value", value=value, expected_type=type_hints["value"])
  File "/Users/wormym/cdp-cdk/.venv/lib/python3.8/site-packages/typeguard/__init__.py", line 785, in check_type
    raise TypeError(
TypeError: type of argument value must be str; got property instead

Expected Behavior

Actually I have many nested stack before from cdkv1 to v2 and migrated v1 to v2 as well. All working as expected till now. but this is new project and suddenly I faced this problem.
From my point of view the @Property already string, and it should be get the real value while runtime of cdk.

Current Behavior

It returned error

Traceback (most recent call last): File "app.py", line 13, in <module> VPCRoutingStack(app, "VPCRoutingStack", vpc_stack=VPCStack) File "/Users/wormym/cdp-cdk/.venv/lib/python3.8/site-packages/jsii/_runtime.py", line 112, in __call__ inst = super().__call__(*args, **kwargs) File "/Users/wormym/cdp-cdk/edla_vpc/VPCRoutingStack.py", line 21, in __init__ CfnOutput(self, id, value=vpc_stack.get_vpc_id, export_name="cccc") File "/Users/wormym/cdp-cdk/.venv/lib/python3.8/site-packages/jsii/_runtime.py", line 112, in __call__ inst = super().__call__(*args, **kwargs) File "/Users/wormym/cdp-cdk/.venv/lib/python3.8/site-packages/aws_cdk/__init__.py", line 5755, in __init__ props = CfnOutputProps( File "/Users/wormym/cdp-cdk/.venv/lib/python3.8/site-packages/aws_cdk/__init__.py", line 5918, in __init__ check_type(argname="argument value", value=value, expected_type=type_hints["value"]) File "/Users/wormym/cdp-cdk/.venv/lib/python3.8/site-packages/typeguard/__init__.py", line 785, in check_type raise TypeError( TypeError: type of argument value must be str; got property instead

Reproduction Steps

init cdk with python language
Create 3 files that I explained:

  • app.py
  • VPCStack.py
  • VPCRoutingStack.py
    Then run cdk deploy and error occurred

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.84

Framework Version

No response

Node.js Version

node 16/18

OS

os x / ubuntu

Language

Python

Language Version

3.8

Other information

requirements.txt:
aws-cdk-lib==2.84.0
constructs==10.2.55
python-dotenv

@wormym1011 wormym1011 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 20, 2023
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Jun 20, 2023
@pahud pahud self-assigned this Jun 20, 2023
@pahud
Copy link
Contributor

pahud commented Jun 20, 2023

Looks like the error comes from CfnOutput but I am not very sure. Let's simplify the provided sample.

Are you able to run this?

from aws_cdk import (
    Stack, CfnOutput,
    aws_ec2 as ec2,
)
from constructs import Construct

class VpcStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        
        self.vpc = ec2.Vpc.from_lookup(self, 'Vpc', is_default=True)
        
    @property
    def get_vpc_id(self) -> str:
        return self.vpc.vpc_id
        

class ConsumerStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, vpc_stack, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        
        CfnOutput(self, construct_id, value=str(vpc_stack.get_vpc_id))

On cdk deploy --all you should see the vpcId in the output. It works for me. Are you able to deploy that?

@pahud pahud added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jun 20, 2023
@pahud pahud removed their assignment Jun 20, 2023
@wormym1011
Copy link
Author

wormym1011 commented Jun 21, 2023

Yes, it works like my code has been implemented as well

# str(vpc_stack.get_vpc_id) something likes <property object at 0x10d389e50>	
    CfnOutput(self, id, value=str(vpc_stack.get_vpc_id), export_name="tired")

same way with you

CfnOutput(self, construct_id, value=vpc_stack.get_vpc_id)

that returned

ConsumerStack | <property object at 0x1097f5a90>

But I need to inject the real data into some next steps likes real subnetid, real vpc_id, etc..
We can not input value <property object at 0x1097f5a90> into

subnet_ids=[
            vpc_stack.get_etl_subnet_id_1a,
            vpc_stack.get_etl_subnet_id_1b,
            vpc_stack.get_etl_subnet_id_1c
        ],
        transit_gateway_id=DX_TGW_ID,
        vpc_id=vpc_stack.get_vpc_id,

Normally it should be worked as I have done many times before, the real value will be put while cdk deploy running.

If I remove str cast then error returned:

    ConsumerStack(app, "**### ConsumerStack**", vpc_stack=DebugVpcStack, env=env_sing)
  File "/Users/wormym/cdp-cdk/.venv/lib/python3.8/site-packages/jsii/_runtime.py", line 112, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/Users/wormym/cdp-cdk/edla_vpc/VpcConsumer.py", line 12, in __init__
    CfnOutput(self, construct_id, value=vpc_stack.get_vpc_id)
  File "/Users/wormym/cdp-cdk/.venv/lib/python3.8/site-packages/jsii/_runtime.py", line 112, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/Users/wormym/cdp-cdk/.venv/lib/python3.8/site-packages/aws_cdk/__init__.py", line 5755, in __init__
    props = CfnOutputProps(
  File "/Users/wormym/cdp-cdk/.venv/lib/python3.8/site-packages/aws_cdk/__init__.py", line 5918, in __init__
    check_type(argname="argument value", value=value, expected_type=type_hints["value"])
  File "/Users/wormym/cdp-cdk/.venv/lib/python3.8/site-packages/typeguard/__init__.py", line 785, in check_type
    raise TypeError(
TypeError: type of argument value must be str; got property instead

Can you remove str() in Cfnoutput ?
Normally, it should be output string as well with readable text likes: vpc-xxxxxxxxx.
Not property likes

<property object at 0x1097f5a90>

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jun 21, 2023
@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.

@dil-anovosz
Copy link

@wormym1011 Could you solve this issue? I have the same but with event bridge schema target parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

No branches or pull requests

3 participants