简体   繁体   English

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

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

My English may be strange.我的英语可能很奇怪。 If there are places where it doesn't make sense, please ask me.如果有不明白的地方请追问。

What we want to achieve我们想要达到的目标

I want to build an environment using aws cdk(python).我想使用 aws cdk(python) 构建一个环境。 I want to separate the vpc stack from the aurora stack.我想将 vpc 堆栈与 aurora 堆栈分开。 To do this, I want to add a resource (subnet id) created on vpc's stack to aurora's I want to reference it in the stack.为此,我想将在 vpc 的堆栈上创建的资源(子网 ID)添加到 aurora 我想在堆栈中引用它。

problem问题

#!/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()

I've written the code based on the ↓ document, but I get an error when I run it.我已经根据↓文档编写了代码,但是运行时出现错误。

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

I have confirmed that I will deploy with vpcStack, auroraStack only.我已经确认我将使用 vpcStack 进行部署,仅 auroraStack。 However, I get the following error.但是,我收到以下错误。 AttributeError: The 'vpcStack' object has no attribute 'outputSbnt01' AttributeError:“vpcStack”object 没有属性“outputSbnt01”

What I've tried我试过的

I tried it and set outputSbnt01 in Cfnoutput, but I get the same error.我尝试了它并在 Cfnoutput 中设置了 outputSbnt01,但我得到了同样的错误。 There is a similar question ↓ and I tried, but I got the same error.有一个类似的问题↓我试过了,但我得到了同样的错误。

AWS CDK: how do I reference cross-stack resources in same app? AWS CDK:如何在同一应用程序中引用跨堆栈资源?

Thanks for watching.感谢收看。

Your calls in app.py would look like this same as you had:你在 app.py 中的调用看起来和你一样:

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

Verify that you have assigned the outputSbnt01 variable in stack vpcStack :验证您是否已在堆栈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

Accept the object in the auroraStack接受 auroraStack 中的auroraStack

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

You can now reference the variable as sbntid in auroraStack .您现在可以在auroraStack sbntid

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

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