1

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.

Rojer Mckee
  • 19
  • 1
  • 2
  • 1
    Does this answer your question? [How to create VPC that can be shared across stacks?](https://stackoverflow.com/questions/57623766/how-to-create-vpc-that-can-be-shared-across-stacks) – Pedreiro Jul 09 '20 at 04:41
  • Does this answer your question? [How to import existing VPC in aws cdk?](https://stackoverflow.com/questions/59301265/how-to-import-existing-vpc-in-aws-cdk) – Amit Baranes Jul 09 '20 at 16:17

1 Answers1

1

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.

Adam
  • 1,061
  • 2
  • 8
  • 6
  • Do not forget to reference `sbntid` in the other stack. Because just passing the parameter does not work, you have to reference it for the CDK to make sense of the order to execute the CFN stacks – iSenne Aug 12 '21 at 05:54