Closed rongshengfang closed 4 years ago
Transferring to the SAM CLI repo, which handles sam package
.
I have this same problem and I wish there was a native builtin function to calculate the SHA of a file (e.g. Terraform has filesha256()). What I have been doing for my workaround is in my deploy script, after I run my SAM package, but before SAM/cft deploy, I loop through the binaries/code that need to be deployed and I run the following command to get the SHA256:
SHA256=$(openssl dgst -sha256 -binary cmd/$function/release/$function | openssl enc -base64 | tr -d "=+/")
$function is the name of the function in the SAM as well as the name of the binary (GoLang for us, but you could hash any code file). After I get the SHA256 I then insert the "AutoPublishCodeSha256" property in the packaged.yaml with the SHA value using yq. This looks like this:
yq w -i packaged.yaml "Resources.$function.Properties.AutoPublishCodeSha256" "$SHA256"
This will cause SAM to not create a new version if the SHA is the same. It will still upload the code unfortunately and deploy a $LATEST Version, but your alias will still point to the latest "versioned" change (e.g 1,2,3 etc.)
This should no longer happen with version of SAM CLI after 0.42.0. We now sha the contents of the file: https://github.com/awslabs/aws-sam-cli/pull/1789
Closing
Description:
When
CodeUri
is set to a local directory where the Lambda function code resides,sam package
uploads the package to the s3 bucket even if there hasn't been any changes to the local directory. This causes a file to be uploaded to the s3 bucket every timesam package
is run, even if there isn't any change to the Lambda function code.Is there any way for
sam
to only upload the file when there are changes?This is how I use the
sam package
command:Please see below for the function definition: