The docs say they do exactly the same thing:
Both the sam package and sam deploy commands described in this section are identical to their AWS CLI equivalent commands aws cloudformation package and aws cloudformation deploy, respectively.
But my experience suggests that isn't quite true. When I run sam deploy
for my Java-based Lambda gets an artifact uploaded to S3 with this type of zip structure:
And this executes nicely in Lambda. But when I follow the Lambda + CodePipeline docs and use aws cloudformation
, I get this zip structure (which is essentially my git repo):
Note: this alternative structure happens even outside of CodeBuild, so I am doubtful it has anything to do with what is in buildspec.yml
. I can reproduce the two structures simply by invoking the two different package commands on my desktop.
Any ideas why there is a difference? I'd like to understand it, especially since the docs say they should be the same, before I switch my buildspec.yml
to use sam deploy
.
Thanks!
'sam package' is as dumb as 'aws cloudformation package'. The difference come from 'sam build' which will build a new 'template.yaml' and place under '{app_dir}/.aws-sam/build/template.yaml with the dependencies.
This new 'template.yaml' is used by 'sam package' (instead of '{app_dir}/template.yaml') thus adding the required dependencies in the package. Please investigate the directory '{app_dir}/.aws-sam/build/' to learn more.
If you run 'cloudformation package' in the directory '{app_dir}/.aws-sam/build' you will get the same result as 'sam package'.
Use 'sam package' with --debug to see which template file it is reading:
$ sam package --output-template-file packaged.yaml --s3-bucket {bucket_name} --debug
Using SAM Template at /home/ec2-user/sam-app/.aws-sam/build/template.yaml
'aws cloudformation package' will just read the template file from where you specify:
$ aws cloudformation package --template-file template.yaml --output-template-file packaged.yaml --s3-bucket {my-bucket}
I had an issue with aws cloudformation package ...
which did not update the DefinitionUri
to a corresponding S3 URI of the SAM template I had as follows for a Step function:
DemoStepFunction:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri: stepfunctions/demo_step_1.json
sam package...
solved the problem and created the correct Cloudformation template:
DemoStepFunction:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri:
Bucket: pipeline-demo-test1
Key: cassdasd6565casdawwebf5f3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With