Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sam package vs aws cloudformation package

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:

  • META-INF
  • com/example/etc, class files
  • logback.groovy, other resources
  • lib, which contains all my jars

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):

  • README.md
  • build/
  • build.gradle
  • buildspec.yml
  • events
  • gradle/
  • gradlew
  • gradlew.bat
  • src/
  • template.yml

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!

like image 259
Patrick Lightbody Avatar asked Nov 12 '19 16:11

Patrick Lightbody


2 Answers

'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}
like image 180
shariqmaws Avatar answered Sep 21 '22 18:09

shariqmaws


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
like image 27
mjlowky Avatar answered Sep 21 '22 18:09

mjlowky