Skip to content
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

Docs lack examples or explanation of how to export and import values between stacks #10480

Closed
jpSimkins opened this issue Sep 22, 2020 · 4 comments
Labels
documentation This is a problem with documentation. feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged.

Comments

@jpSimkins
Copy link

I am new to the CDK and do not use CloudFormation as I prefer Terraform over CloudFormation. I mention this only because it seems if you have CloudFormation experience, this may be easy to identify how to do this but for someone with none, this seems to be impossible although there are numerous discussions about being able to... but no working examples.

One issue that I am still trying to identify is how I can export values from a stack so that I can import them in another stack.

I currently use CfnOutput to output the values but this doesn't seem to allow me to import them into another stack.

I see this ticket: #603 that has expirewd links and explains about Outputs & Parameters, and Exports & Fn::ImportValues. I think the most useful part of that ticket is: #603 (comment)

I am able to make outputs but I still cannot figure out how to import the outputs so I can use them in another stack. I am also considering using SSM as this seems to be the easiest way to do this but I feel this is a hack.

This leads me to another question that is a result of this design. What is the best approach to designing stacks?
I don't think having a single stack for the entire AWS account is ideal. I was first using nested stacks and passing the variables of each stack into each nested stack as needed. This works but it doesn't seem ideal as every deploy I do is the entire account, not just the part I am working on. Is this concern even worth worrying about?

I typically have a base stack which is the VPC, RDS, and a bastion. I then have a few other nested stacks that are in the actual project. I can pass in the base stack and I have getters that can be used to get the VPC or RDS details. Again, this is not what I want.

What I am trying to do is have my base stack use outputs. I then want to import these outputs in each stack that needs them. I can use this same design for all systems and this seems much cleaner and easier to manage to me.


This is a 📕 documentation issue

@jpSimkins jpSimkins added documentation This is a problem with documentation. feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Sep 22, 2020
@SomayaB SomayaB assigned ghost Sep 22, 2020
@SomayaB
Copy link
Contributor

SomayaB commented Sep 22, 2020

Hi @jpSimkins, you should be able to just implicitly import resources from one stack to another, see the docs and this PR. I agree that we should probably have a section in the docs that talks about best approaches when designing a multi stack app. In the meantime, I suggest looking at the Cloudformation docs and this helpful user's guide.

@jpSimkins
Copy link
Author

Thank you @SomayaB

What I was trying to accomplish is something where I do not need to be aware of the ids or anything. Ideally, I wanted to point to a stack and then an output value of the stack. It seems this is not possible with CDK.

I tried to use Fn::ImportValue but I have yet to get this to work as I haven't seen a working example on the CDK. I thought this may be close to what I was trying to accomplish.

Terraform has a remote states system that is more easily malleable for IaC. This is where I am getting this design from and from what I have seen, it's just not something possible with the CDK. That is what I needed to identify.

So in order to use the CDK for IaC, I need to keep track of IDs and use them throughout the code. Thank you for the links, I see that using a cdk.json file may be useful for this approach or even a constants file with these IDs would suffice, I was just looking for a more elegant solution.

Thanks for your insights on this.

@jpSimkins
Copy link
Author

jpSimkins commented Dec 3, 2020

To update this ticket in-case future searches find this:

To use Fn.ImportValue you must first have an Output in the stack using CfnOutput. For this to work, you must define an exportName for CfnOutput.

e.g.:

Output in StackA:

new CfnOutput(this, 'VpcId', {
  value: this._sg_AllowRDSAccess.securityGroupId,
  exportName: 'StackA-VpcId' // This is the name you will use for the import
});

Stack B usage:

const VPC_ID = Fn.importValue('StackA-VpcId');

One more thing to make note of, and something that makes using ImportValue useless IMHO, is that for some constructs, you cannot us Fn.importValue as this would throw an error.

The VPC example I provided above, is such a case. I am forced to hard-code the VpcID in my code because using Fn.ImportValue will cause the error:

All arguments to Vpc.fromLookup() must be concrete (no Tokens)

Meaning, the token that is a placeholder for the value will throw this error.

I find this severely limits the usage of these as they tend to break in the CDK and hard-coding these values solves that issue.

Not sure if there is any concerns to solve this issue but I find this limitation to render this useless.

@github-actions
Copy link

github-actions bot commented Dec 3, 2020

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This is a problem with documentation. feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

2 participants