Feature Request: Add ability for custom stacks to reference each other's output
Is your feature request related to a problem? Please describe.
The issue that I'm facing is that I'd like to be able to do the following:
- Using a stack configuration, create a custom data source
- Using another stack configuration, create a custom resolver which uses data source defined in stack 1.
- Continues on for as many stacks might need to reference stack 1's resources.
In order to do this, though, subsequent stacks would need to wait on the first stack to finish so that the resource exists. It is possible to work around this limitation by adding multiple data sources pointing to the same underlying resource, but it would be fantastic to be able to create one data source (or role or whatever) and share it amongst many other stacks. For my particular use case, I have a Lambda function which does some data validation not possible in VTL templates. I'd like to just create that Data Source once.
Describe the solution you'd like
Normally this is what DependsOn would be used for, but I don't believe it works across stack templates (or at least I couldn't get it to). I'd like to be able to Output a value from a custom stack, and then add a Parameter to a sibling custom stack which references the output value. According to option 2 of this answer on Stack Overflow, an implicit dependency would be created and order of operations would work correctly. In order for this to work properly, the parent stack would need to be able to supply the child stack with the proper reference as a Parameter value.
Example:
// custom stack 1
{
"Resources": {
"ThisThing": {
"Name": "FooThing"
}
},
"Outputs": {
"FooName": {
"Value": {
"Fn::GetAtt": [
"ThisThing",
"Name"
]
}
}
}
}
// custom stack 2
{
"Parameters": {
"FooThingName" : {
"Type" : "String",
"Description" : "The name of the foo thing"
}
}
}
// parent stack (auto-generated)
{
"Resources": {
"ChildStack2": {
"Type" : "AWS::CloudFormation::Stack",
"Properties" : {
"Parameters" : {
"FooThingName" : { "Fn::GetAtt" : "Outputs.FooThingName" }
}
}
}
}
}
Describe alternatives you've considered
There really isn't a work around to cross-stack dependencies without the parent stack passing data between children as far as I've found.
Additional context N/A
@lookitsatravis So are these stacks used as a custom stack as a part of your AppSync API?
@kaustavghosh06 - yes. After the release of @aws-amplify/cli 1.1.0 I added a stacks directory inside amplify/backend/api/[name] where the type is a GraphQL/AppSync API.
Any reason you can't use Option 1 in the StackOverflow answer? I.e. output the values as myparameter-${env} from one stack and reference it from another one? That seems to be the way Amplify's own stacks are communicating variables.
I guess the creation order might be an issue. We're planning to create an RDS database for use with Lambda functions. We're not yet sure can we do this using Amplify, or do we need to manually deploy the DB stack first before deploying with Amplify (question at #1426).
I'm looking for something similar. We have a S3 storage and a Lambda created from the Amplify CLI. We want to execute the Lambda on ObjectCreated events, but I can't figure out how to connect the two resources, i.e. how do we reference the Lambda from the S3 cloudformation?
See zjullion's comment on issue #1481, it might help. I was able to import variables output by other resources into a customized Lambda CF stack. Not sure does it work for custom stacks though.
I also need a way to do this. Without a solution, all of the custom stacks that reference a common resource must be consolidated into a single stack, which is not desirable.
Creating exports and imports doesn't work without being able to control the creation sequence.
The solution @plaa references regarding Lambda stacks indeed doesn't work for custom stacks as these do not have entries in the backend-config.json file.
For anyone looking at this issue, I would strongly encourage you to allow amplify-cli to control any custom stacks by adding the stack to amplify - see my comment and the amplify-cli documentation
In this manner, you can control the order in which the stacks are deployed using the DependsOn attribute. My team has multiple custom stacks, controls the order in which they are deployed, and has resources referenced between different stacks. As @plaa mentioned, see my other comment for details on the format for passing outputs between these stacks. (Note that this formatting isn't documented anywhere as of right now, but it absolutely does work).
+1
As @zjullion mentioned Amplify CLI support passing outputs inter-stack for even your custom stacks. Documentation is out here - https://docs.amplify.aws/cli/usage/customcf
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.