繁体   English   中英

如何使用 aws cdk 访问不同堆栈中的资源?

[英]How to accessing resources in a different stack using aws cdk?

我的英语可能很奇怪。 如果有不明白的地方请追问。

我们想要达到的目标

我想使用 aws cdk(python) 构建一个环境。 我想将 vpc 堆栈与 aurora 堆栈分开。 为此,我想将在 vpc 的堆栈上创建的资源(子网 ID)添加到 aurora 我想在堆栈中引用它。

问题

#!/usr/bin/env python3
from aws_cdk import core
from test.aurora import auroraStack
from test.vpc import vpcStack
app = core.App()
prod = core.Environment(account="123456789012", region="us-east-1")
vpcStack(app, "Vpc", env=prod)
auroraStack(app, "Aurora", env=prod, sbntid=vpcStack.outputSbnt01)
app.synth()

我已经根据↓文档编写了代码,但是运行时出现错误。

https://docs.aws.amazon.com/cdk/latest/guide/resources.html#resource_stack

我已经确认我将使用 vpcStack 进行部署,仅 auroraStack。 但是,我收到以下错误。 AttributeError:“vpcStack”object 没有属性“outputSbnt01”

我试过的

我尝试了它并在 Cfnoutput 中设置了 outputSbnt01,但我得到了同样的错误。 有一个类似的问题↓我试过了,但我得到了同样的错误。

AWS CDK:如何在同一应用程序中引用跨堆栈资源?

感谢收看。

你在 app.py 中的调用看起来和你一样:

vpcStack(app, "Vpc", env=prod)
auroraStack(app, "Aurora", env=prod, sbntid=vpcStack.outputSbnt01)

验证您是否已在堆栈vpcStack中分配了outputSbnt01变量:

class vpcStack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)
        outputSbnt01 = ec2.Subnet()
        self.outputSbnt01 = outputSbnt01

接受 auroraStack 中的auroraStack

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

您现在可以在auroraStack sbntid

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM