(lambda): cross-region reference identifiers of `currentVersion` differ depends on `-e` flag and asset bundling
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the bug
I'm testing the new "weak" cross-stack reference (`Fn::GetStackOutput`) between Lambda@Edge function with `NodejsFunction` and CloudFront distribution. There are 2 stacks, main stack (ap-northeast-1) and edge-function stack (us-east-1).
(🎉thanks #37800)
When I deploy both stacks at once, everything works fine.
But when I deploy the main stack with `-e` flag, the referencing identifier is changed and the deployment fails since the output is missing.
### Regression Issue
- [ ] Select this option if this issue appears to be a regression.
### Last Known Working CDK Library Version
_No response_
### Expected Behavior
The reference identifier persists across deployments.
### Current Behavior
The reference identifier differs when `-e` flag set.
### Reproduction Steps
#### CDK app
``` ts
import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as nodejs from 'aws-cdk-lib/aws-lambda-nodejs';
import * as ssm from 'aws-cdk-lib/aws-ssm';
const app = new cdk.App({
postCliContext: { "@aws-cdk/core:defaultCrossStackReferences": "weak" },
});
const mainStack = new cdk.Stack(app, 'MainStack', {
env: { region: 'ap-northeast-1' },
crossRegionReferences: true,
});
const edgeStack = new cdk.Stack(app, 'EdgeStack', {
env: { region: 'us-east-1' },
});
// defines Lambda@Edge function
const edgeFunction = new nodejs.NodejsFunction(edgeStack, 'EdgeFunction', {
entry: 'edge-function.ts',
runtime: lambda.Runtime.NODEJS_24_X,
currentVersionOptions: { removalPolicy: cdk.RemovalPolicy.RETAIN },
});
// consume edgeArn (required by CloudFront)
new ssm.StringParameter(mainStack, 'EdgeArnRef', {
stringValue: edgeFunction.currentVersion.edgeArn,
});
```
#### Steps to reproduce
``` console
$ cdk deploy --all
(both stacks are successfully deployed)
$ cdk diff
Bundling asset EdgeStack/EdgeFunction/Code/Stage...
(...)
Stack MainStack
There were no differences
(...)
Stack EdgeStack
There were no differences
$ cdk diff MainStack -e
(...)
Stack MainStack
Resources
[~] AWS::SSM::Parameter EdgeArnRef EdgeArnRefEE824AD8
└─ [~] Value
└─ [~] .Fn::GetStackOutput:
└─ [~] .OutputName:
├─ [-] PublishOutputRefEdgeFunctionCurrentVersion326158C2c545fdad30378ecb264d313a8241e5f3CD22A0F4
└─ [+] PublishOutputRefEdgeFunctionCurrentVersion326158C2dd8e72d97e0a708b370b11788ea1bab83D3D7E12
✨ Number of stacks with differences: 1
$ cdk deploy MainStack -e
(...)
MainStack: deploying... [1/1]
❌ MainStack failed: DeploymentError: Resource updates failed:
└─ MainStack
└─ EdgeArnRef
└─ Resource (AWS::SSM::Parameter EdgeArnRefEE824AD8)
🛑 TemplateError: Fn::GetStackOutput references output
PublishOutputRefEdgeFunctionCurrentVersion326158C2dd8e72d97e0a708b370b11788ea1bab83D3D7E12 from stack EdgeStack, but
this output was not found. The output may have been deleted.
Source Location: ...new StringParameter2 in aws-cdk-lib...
new EdgeFunction (.../edge-function.ts:24:5)
(.../cdk-test.ts:82:1)
...node internals, ts-node, ts-node, ts-node...
```
### Possible Solution
`currentVersion` seems to generate its identifier from props and the asset hash.
`cdk deploy MainStack -e` does not perform asset bundling since the asset is not in `MainStack`, then the asset hash is blank (or some static value), then the consumed identifier differs from initial deployment.
### Additional Information/Context
WORKAROUND: Deploying both stack explicitly will succeed.
``` console
$ cdk deploy MainStack EdgeStack -e
Bundling asset EdgeStack/EdgeFunction/Code/Stage...
(...)
✅ EdgeStack (no changes)
(...)
✅ MainStack (no changes)
```
### AWS CDK Library version (aws-cdk-lib)
2.254.0
### AWS CDK CLI version
2.1121.0 (build 7abdd4e)
### Node.js Version
v24.15.0
### OS
Linux
### Language
TypeScript
### Language Version
_No response_
### Other information
_No response_
Contributor guide
Research direction
Start by reproducing the behavior with the CDK app in cdk-test.ts and the Lambda entry point in edge-function.ts, comparing cdk diff MainStack -e with cdk deploy MainStack EdgeStack -e. Trace NodejsFunction.currentVersion, cross-region output naming, and asset bundling; done means the output identifier remains stable and deploying MainStack alone with -e succeeds.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, nodejs, typescript
- Domain
- cloud, infrastructure
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100