简体   繁体   中英

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). I want to separate the vpc stack from the aurora stack. 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.

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

I have confirmed that I will deploy with vpcStack, auroraStack only. However, I get the following error. AttributeError: The 'vpcStack' object has no attribute 'outputSbnt01'

What I've tried

I tried it and set outputSbnt01 in Cfnoutput, but I get the same error. 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?

Thanks for watching.

Your calls in app.py would look like this same as you had:

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

Verify that you have assigned the outputSbnt01 variable in stack vpcStack :

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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