-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best practices for cross-stack CDK to CFN and vice versa #603
Comments
Hi @mipearson, There are a lot of dimensions to your question, and it kind of depends on what you want to do exactly. I just wrote a whole response assuming you meant to share resources between CDK apps and plain CloudFormation templates, but upon rereading your comment and code I now realize you might mean sharing resources between higher-level CDK constructs and lower-level CDK constructs (i.e., use a "higher-level" VPC construct with some lower-level direct CloudFormation resources). I will post both of my responses below. |
Sharing between CDK apps and CloudFormation templatesYou got it. If there is information that needs to be shared between stacks, the mechanisms we have are We could also use SSM Parameter Store values, which work much like Exports but without the "foreign key constraints" that Exports bring. CDK to CloudFormationIf you define a VPC inside a CDK app and want to use it from a CFN template, it actually functions much the same as how you would share the template between plain CFN templates. You would output/export in the one template and parameter/import in the other. The exporting works by calling If you're unhappy about the default names of the Exports (understandable since they are designed to be consumed transparently), you're free to add some CloudFormation to CDKSo you already have an existing VPC (deployed through CloudFormation or otherwise) that you want to consume in a CDK application. As you figured out, what you want to do is get a vpcId, availabilityZones, publicSubnetIds, privateSubnetIds Again, use your favorite way of getting those values in there. You now have 3 options:
Of all these, Exports and Imports will give you the most transparent experience. And from your example, I love how you abstract away the importing of the VPC inside a const vpc = OurStandardVPC.obtain(this);
new ThingThatNeedsAVPC(..., { vpc }); Or similar, and not have to worry where the VPC is coming from. It might be constructed on the spot, it might be loaded from another environment. |
Sharing between higher-level and lower-level ConstructsIf this is what you're trying to do, it depends on how you want to deploy: in a single stack or across multiple stacks. Multiple stacksIf it's across multiple stacks, the solution will be basically the same as what I described in my previous post, except the CloudFormation template will not be handwritten but generated by CDK. The mechanism used will be the same. To make matters simpler, in the consuming stack you could forego the Single stackThis would be even easier, because you can simply access the properties of |
So ... I kind of mean both! :) As in if we start migrating from our existing solution to aws-cdk we're going to need to both go CFN to CDK (ie, to refer to a VPC defined elsewhere) and CDK to CDK w/ primitives. Thanks for the feedback, good to know I'm on the right track. If you'd like to use what I've got in the gist as an example be my guest, let me know if you need me to sign a CLA or anything like that. |
Is there an example of this somewhere? ie, naming my own stack outputs and then passing the variable to another CDK stack, similar to what's done with VpcNetworkRefProps. Or, to phrase it another way, what's special about the |
If you take a look at the definition of
The result is that an Export will be created, and the returned value is the Import that will eventually take on the Export's value at deployment time. The thing is, since this Output is created as a child of another construct, its LogicalID will be a long generated string with a unique identifier at the end. If you were to create the By the way, looking at this code I'm noticing that the list of |
👍 |
I wish this was made a bit plainer in the documentation as it looks like I have basically duplicated the import / export functionality in my stack with the following. The one suggestion I would make is that the documentation explicitly mentions using isolated subnets for things like RDS clusters however when it comes to creating a cluster the RDS class uses an option
The good news is that most of the time that I've tried to do something with the CDK and bumped up against a limitation I have found that there is already support to work around it. |
I apologize for all rough edges you're running into. We're very grateful for your investment though--it's specifically because we need people putting the library through its paces to figure out where our sharp design and documentation edges are. To your point, the subnet selection is being addressed here: #610 |
Don't apologise the library is clearly marked at pre-production so rough edges are to be expected. Already with the CDK I have been able to shave over 500 lines off our existing CF template. I only wish I could devote more time at work to adding some more features as this sort of library building is quite enjoyable. |
Also great news about the subnet selection. When I come across something like this or the lack of tagging I have always it seems found that you're a step ahead and have something planned for it already. |
See #1525 |
I'm not sure how much of this is still true after the refactoring of export/import. |
Yeah - for cross-stack going between CDK and non-CDK I'm mostly entering values by hand right now anyway. |
This documentation section will change completely when #1546 lands. |
Okay, I'll wait on #1546 |
Closing. |
I wanted to see whether something was at all possible - whether a VPC created using a CDK construct could be used with a LoadBalancer created via cloudformation and vice versa.
This is important for me as:
accessLoggingPolicy
on the load balancer)The example is here: https://gist.github.com/mipearson/aeaf303b0770c25f8b5f6e360594cfbf
Is this what is recommended for solving this sort of problem?
The text was updated successfully, but these errors were encountered: