Lambda Function Code.FromBucket() does not update "Code Entry Type" in AWS console
Description
When CDK stack with Lambda function with property Code.FromBucket(..) is used, then the lambda when deployed for the first time, works fine. When an update to lambda is made and then the stack deploy is not updating the deployed lambda in console. It has the old code in AWS console. S3 bucket does have the latest changes. Reason being the "Code entry type" is not pointing to S3 for the lambda in AWS console?
Reproduction Steps
Error Log
Environment
- CLI Version :
- Framework Version:
- OS :
- Language :
Other
This is :bug: Bug Report
This beahviour is coming from CloudFormation and the behaviour of updates.
Quoting from https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-whatis-howdoesitwork.html#w2ab1b5c17c17 - "CloudFormation compares the modified template with the original template and generates a change set. The change set lists the proposed changes. After reviewing the changes, you can execute the change set to update your stack or you can create a new change set. "
Since the updated template your deployed did not change any values in AWS::Lambda::Function, CloudFormation will not make any updates to the Lambda function.
To achieve the behaviour you want, there are two options, either
- manage your lambda code via the CDK using
Code.fromAsset()where the code will be uploaded to S3 by the CDK when you runcdk deployor, - specify the
objectVersionproperty inCode.fromBucket()- in which case this property needs to be updated whenever the stack is deployed after the lambda function code is updated.
@nija-at per the Code.fromBucket(() and using the objectVersion - do you have any recommendation on how to retrieve a given objects version such that it can be passed to this method.
Would you recommend using S3Client API to retrieve the latest version ? Is there a more CDK centric way to do this ?
I assume it's better to pass in parameter or context value to the cdk deploy?
Also any approach has to be C# compatible It's my implementation scenario!
Additional it would be great if the "terse" (possibly generated) documentation noted the constrains and need to use versioning.
So basically this means that:
Code.fromBucket()is completely useless,- that fact is not documented anywhere,
- this issue exists since at least two years, and
- it has been closed without even considering to fix it...
Wow!
This should be documented, thanks for letting us know
Thanks for reopening this.
I agree that this limitation should be documented. That's a good thing. But even better, it would be nice if that problem could be fixed.
I wonder, what is the use case for the current implementation of Code.fromBucket()? I am only learning about the CDK currently, but given all the magic that happens with fromAsset(), I fully expected it to check by itself, if the contents of the bucket changed, by looking at a checksum or the objectVersion or whatever.
I also have no idea on how to work around this. Code.fromAsset() I cannot use, as the code is in a different repository. The other suggestion involved passing an objectVersion, but that would involve the SDK, which is asynchronous, and #8273 tells us that asynchronous calls are not possible with CDK.
This is somewhat frustrating, I based my architecture of the new stack I am currently developing on the assumption that fromBucket works as I expected, and now as I thought I'm finished, I figured out that it doesn't work, and I have to change my architecture, and I don't even know in which direction to go.
I wonder, what is the use case for the current implementation of Code.fromBucket()
We would recommend using fromAsset() when possible. If it's not possible to keep your function code in the same app/repo as your CDK code (I'm curious why this would be the case), then you would need to implement a workaround of your own to ensure the Function stays updated with what's in the Bucket. This could be done through a pipeline which is triggered when either the CDK repo or the function repo change (using fromAsset in this case).
If still using Code.fromBucket(), you would need to implement a lookup of your own, preferably in a deterministic way (e.g. looking up before running synth with a shell script, storing context appropriately in a file, retriggering another lookup only happens manually) rather than making an async call during synth (this is possible, e.g. with custom resources, just highly recommended against), and specifying the objectVersion accordingly to that context.
@peterwoodworth I'd suggest that folks have their reasons for their needs and that something can always be learned from this with perhaps less opinion.
In our case that might be different from @marc-guenther's case, whist we do have all our lambda code in the same repo as the CDK code we are able to build it with CDK as it requires a very custom build process that CDK does not support. We actually use Nuke.Build as our build pipeline via GitHub.workflows and invoke the CDK from there.
Our Nuke build uploads the build artifact Zip file to a an S3 bucket, and we then pass that and version information as you rightly indicate into the CDK as a context values (Json base-64 encoded) that we decode in our StackProps extension, to be used in the Code.FromBucket.
Our build process in Nuke.Build, that does use the SDK, picks up the latest version of all the lambdas (x18) we are deploying in a similar manner.
This may help @marc-guenther.
It would be great to not have to use a bucket with versioning to do this as it adds complextity where it would not otherwise perhaps be needed, as we now have to lifecycle manage versions on the bucket. It just moves the problem someplace else, that at least we can work around - for now!
It's only a shame the same does not work for Source.Bucket as part of Amazon.CDK.AWS.S3.Deployment https://github.com/aws/aws-cdk/issues/23688 that does not have the same version argument! So in their case its unsolvable!
Consistency across the entire CDK would be a wonderful thing!
We would have a similar use case and run into this issue.
Our plan was: Maintaining a lambda (optimized for performance), which is shared with multiple teams and can be configured with environment variables (which results in new lambdas in their accounts with our code).
The lambda code is maintained/updated in a s3 bucket. Currently when teams would redeploy their lambda, they would not deploy the latest available code.
Its not really feasible to tell teams to redeploy their lambdas each time we make an update.
Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.