aws / aws/aws-cdk

synth: Unresolved condition dependency using CfnCondition from another stack

Open
#26,560 2 comments 0 reactions 0 assignees View on GitHub
bug effort/medium p2 package/tools
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

Using a `CfnCondition` created in another stack causes an unresolved dependency. The condition is defined in the other stack while the stack that is using it does not have "conditions" defined in CloudFormation.

### Expected Behavior

I would expect the condition to be created in all the stacks that need it. Unless a condition can be an output of a stack that can be consumed by another stack? I'm unsure of the inner workings.

### Current Behavior

Current stack B uses the condition `dsdnsconditionemptysubdomainstaging` created in another stack A:
```
"Fn::Join": [
"api",
[
".",
{
"Fn::If": [
"dsdnsconditionemptysubdomainstaging",
{
"Fn::ImportValue": "ds-dns-staging:ExportsOutputRefdnsDomain91549E00"
},
{
"Fn::ImportValue": "ds-dns-staging:ExportsOutputRefdnsDomain91549E00"
}
]
}
]
]
},
```

while only the other stack A has the condition defined:
```
"Conditions": {
"dsdnsconditionemptysubdomainstaging": {
"Fn::Equals": [
{
"Ref": "dnsSubdomain"
},
""
]
},
```

### Reproduction Steps

```
App app = new App();
StackA stackA = new StackA(app);
new StackB(app, stackA);
app.synth();
```
```
class StackA extends Stack {
String fqdn;
public StackA (...) {
fqdn = Fn.join(
"...",
List.of(Fn.conditionIf(
// If subdomain is empty
CfnCondition.Builder.create(scope, "condition-empty-subdomain")
.expression(Fn.conditionEquals(dnsSubdomainParam.getValueAsString(), ""))
.build()
.getLogicalId(),
// Then supply the domain only
dnsDomainParam.getValueAsString(),
// Else prefix the domain with a dot to separate the subdomain
Fn.join(".", List.of(dnsDomainParam.getValueAsString()))
).toString()));
}
}
```
```
class StackB extends Stack {
public StackB (..., StackA stackA) {
// Use stackA.fqdn
}
}
```

Error msg:
```
Template error: unresolved condition dependency dsdnsconditionemptysubdomainstaging in Fn::If
```

### Possible Solution

Workaround is to create the CfnCondition in every stack.

### Additional Information/Context

_No response_

### CDK CLI Version

2.77.0

### Framework Version

_No response_

### Node.js Version

16

### OS

osx 14

### Language

Java

### Language Version

Java 11

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the Java reproduction using App, StackA, StackB, and app.synth(), then inspect the synthesized CloudFormation for the condition in StackA and its Fn::If use in StackB. Done means the cross-stack condition no longer produces an unresolved condition dependency and the synthesized stacks remain valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, java
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.