aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.66k stars 3.92k forks source link

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

Closed wormym1011 closed 1 year ago

wormym1011 commented 1 year ago

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:

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

pahud commented 1 year ago

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?

wormym1011 commented 1 year ago

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[bot] commented 1 year ago

⚠️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 commented 11 months ago

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