[aws-lambda] Function.prototype.currentVersion does not work with Code.fromCfnParameters
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
`Function.prototype.currentVersion` does not work when the code of the Lambda function is created using `Code.fromCfnParameters()`. Given that the logical id of the version changes based on `md5` of the function configuration, a new version does not get created when the parameters of the stack change (since this happens at CloudFormation update time).
### Reproduction Steps
```ts
const stack = new Stack();
const func = new Function(stack, "MyFunction", {
runtime: Runtime.NODEJS_12_X,
handler: "index.handler",
code: Code.fromCfnParameters({
bucketNameParam: new CfnParameter(stack, "CodeBucket"),
objectKeyParam: new CfnParameter(stack, "CodeKey")
})
});
console.log(func.currentVersion.node.uniqueId);
```
This will always print `MyFunctionCurrentVersion7FAFE164` because the function configuration is static:
```json
{
"Resources": {
"MyFunction3BAA72D1": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": { "Ref": "CodeBucket" },
"S3Key": { "Ref": "CodeKey" }
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"MyFunctionServiceRole3C357FF2",
"Arn"
]
},
"Runtime": "nodejs12.x"
}
}
}
}
```
https://github.com/aws/aws-cdk/blob/eb6f3a9f7813f15ca1cdf1dcfc9bb36d93305a07/packages/%40aws-cdk/aws-lambda/lib/function-hash.ts#L5-L17
### What did you expect to happen?
Changing the parameters causes a new version to be created.
### What actually happened?
Changing the parameters causes the lambda function to be updated, but a new version is not created.
### Environment
- **CDK CLI Version :** `1.73.0`
- **Framework Version:** `1.73.0`
- **Node.js Version:** `12.13.0`
- **OS:** macOS `10.14.6`
- **Language (Version):** TypeScript `4.0.3`
### Other
The deprecation of `Function.prototype.addVersion()` makes this confusing since it suggests `Function.prototype.currentVersion` is the way to do Lambda versioning for all cases.
https://github.com/aws/aws-cdk/blob/eb6f3a9f7813f15ca1cdf1dcfc9bb36d93305a07/packages/%40aws-cdk/aws-lambda/lib/function.ts#L729-L735
However, it does not apply to all cases as shown in this bug. The non-deprecated workaround I found was to create an instance of `Version` directy:
```ts
new Version(func, "Version" + Date.now().toString(), {
lambda: func
});
```
This change was introduced in #6771 in response to #6750 and #5334
---
This is :bug: Bug Report
Contributor guide
Research direction
Start with packages/@aws-cdk/aws-lambda/lib/function-hash.ts and the currentVersion implementation in packages/@aws-cdk/aws-lambda/lib/function.ts, then reproduce the issue using the TypeScript example and changing the CloudFormation parameters. Done means parameter changes cause a new Lambda version to be created rather than only updating the function.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100