question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cross Stack References using Functions should save the functions output in origin stack

See original GitHub issue

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave “+1” or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

A cross stack reference using a function should have the output of that function saved in a TerraformLocal, e.g.


class OriginStack extends TerraformStack {
  public value: string;
  constructor(scope: Construct, name: string) {
    super(scope, name);

    const resource = new SomeResource(..)
    this.value = Fn.join(",", [resource.id, Fn.timestamp()])
  }
}

class ConsumerStack extends TerraformStack {
  constructor(scope: Construct, name: string, value: string) {
    super(scope, name);

    const resource = new SomeResource(this, "val", {
      field: value
    })
  }
}

const origin = new OriginStack(app, "origin")
// A cross stack reference is created
new ConsumerStack(app, "consumer1", origin.value);
new ConsumerStack(app, "consumer2", origin.value);

The resulting JSON will have a cross stack reference to the origin Stacks resource id, but the output of the terraform function is not saved in the Terraform state. This means if consumer1 and consumer2 are deployed they could have a different value for the field since timestamp is invoked two separate times.

The fix for this is probably to add a TerraformLocal around all cross-stack functions.

References

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
DanielMSchmidtcommented, Jan 21, 2022

From my perspective the problem is not how to store the computational result in the terraform state, this can be done by a TerraformOutput or a LocalExpression and both should work just fine (with drawbacks mentioned above).

The main problem is that the current idea of cross stack references is to create a reference on resolution. This means on resolve we determine where we have references from outside the current stack and we create cross stack references. To achieve this we made references aware of their stack so we can find these occasions. This was pretty seamless and UX-wise a non-event since we control all the calls to create references.

The problem here is that function calls are written by the user and have no inherent knowledge of the stack they are in (it didn’t matter so far and it doesn’t matter except for these edge cases where you for whatever reason really want to tie a function result to a certain stack).

From my point of view we have a few options here

Option 1: Explicit API

We advise the user to use a TerraformLocal in case they want to pin a function result to a stack. (and add a test to verify locals work through cross stack references, I think there is no explicit test so far)

Option 2: Add stack argument to each function call

Cumbersome for everybody since ever function would get a stack as the first argument.

Option 3: Make Fn stateful

class MyStack {
  constructor(){
   const fn = new Fn(this) // <= we get the stack here
   this.timestamp = fn.timestamp() // <= we get the same function calls here
  }
}

I think I’d vote for option 1 since it will only add overhead for folks running in this edge case. But I’d be happy to reconsider if we have more folks running into issues with this 😃

EDIT: We will start with option 1

0reactions
github-actions[bot]commented, Nov 29, 2022

I’m going to lock this issue because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you’ve found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refer to resource outputs in another AWS CloudFormation stack
Use the Export output field and the Fn::ImportValue intrinsic function to create cross-stack references across AWS CloudFormation stacks.
Read more >
Cross-Stack References - SST docs
When you pass a construct from one Stack to another stack and reference it there; CDK will create a stack export with an...
Read more >
Stacks - CDK for Terraform - HashiCorp Developer
This tutorial guides you through a multi-stack application. You can specify multiple stacks in your application. For example, you may want a separate ......
Read more >
tutorials.pdf - OriginLab
You will find references to buttons found on various toolbars in many of the tutorials in this guide. These buttons are shortcuts to...
Read more >
CDK tips, part 3 – how to unblock cross-stack references
All you have to do is pass an object from one Stack to another, and reference it there. The CDK will generate a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found