aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

sam deploy template-file parameter doesn't work as documented

Open jyoungs opened this issue 5 years ago • 7 comments

Description:

sam deploy -h says:

  -t, --template-file, --template PATH
                                  AWS SAM template which references built
                                  artifacts for resources in the template. (if
                                  applicable)  [default: template.[yaml|yml]]

If this was correct, and the default were template.yaml, then sending -t template.yaml would work exactly the same as not sending this property, right? It doesn't.

Running simply sam deploy deploys the built project as expected. sam deploy -t template.yaml deploys a package where the project isn't built (requirements are not pulled in).

This makes some sense- the documentation does say it should be a "template which references built artifacts", but my point is that the "default" listed contradicts that.

Steps to reproduce:

I just tested this with the helloworld/py38 app.

#run without -t
sam deploy --stack-name mystack --s3-bucket <redacted> --capabilities CAPABILITY_IAM

#run with -t 
sam deploy --template-file template.yaml --stack-name mystack --s3-bucket <redacted> --capabilities CAPABILITY_IAM

Observed result:

When run without -t, it contains the requirements.txt (requests package and sub-dependencies)

When run with -t, the lambda is deployed, but only has 3 files (init.py, app.py, requirements.txt) and no "built" dependencies.

Expected result:

I'm not sure when you'd want to do something like this, so perhaps this should fail, but at a minimum the documentation shouldn't say that it defaults to a value that it clearly does not.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: 11.2.3
  2. sam --version: 1.20.0
  3. AWS region: us-west-2

jyoungs avatar Mar 11 '21 16:03 jyoungs

A work around I'm using to this is to create a shell script that runs the SAM deploy for each template.

Continue to maintain separate template files for different stacks, and just before running SAM deploy save the desired template file as template.yaml, replacing any existing template.yaml file.

rupe120 avatar Mar 15 '21 12:03 rupe120

I have the same issue, The -t option with OR without deploying different build artefacts to Lambda. In a CI environment --guided is not an option. Currently we cant achieve maintaining multiple template files and hence multiple stacks from the same root folder.

Not understood the workaround suggested above.

vilunet avatar Mar 17 '21 01:03 vilunet

@vilunet the following is an example of what I'm doing to work around the issue. This makes the template.yaml a temporary file, only set to what you need just before running sam build and sam deploy.

del template.yaml
copy template-shared.yaml template.yaml

call sam build --use-container 

call sam deploy --config-file samconfig-shared-test.toml 

You can repeat the pattern for multiple templates in the same script if you want.

del template.yaml
copy template-shared.yaml template.yaml

call sam build --use-container 

call sam deploy --config-file samconfig-shared-test.toml 

del template.yaml
copy template-restapi.yaml template.yaml

call sam build --use-container 

call sam deploy --config-file samconfig-restapi-test.toml 

Btw, the syntax above is Windows Batch script. The call commands are not needed in most scripting contexts

rupe120 avatar Mar 17 '21 13:03 rupe120

@rupe120 - I believe this simpler approach would work:

sam build --use-container -t template-shared.yaml
call sam deploy --config-file samconfig-shared-test.toml 

sam build --use-container -t template-restapi.yaml
sam deploy --config-file samconfig-restapi-test.toml 

This trick is to just not send -t to the deploy command. Once the build is run with the correct template, deploy will find the correct packaged template via the build artifacts.

jyoungs avatar Mar 17 '21 14:03 jyoungs

@jyoungs Oh I didn't try that, I will see if that works for me. Thanks

rupe120 avatar Mar 17 '21 14:03 rupe120

thanks @rupe120 and @jyoungs - I got it.

vilunet avatar Mar 19 '21 10:03 vilunet

We have updated the documentation on sam deploy commands, especially the --template parameter. Please find it here: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-deploy.html After you run sam build with --template, the template file will be copied to a new folder .aws-sam/build under your project directory. Then running sam deploy without specifying any template, it will pick up the template under .aws-sam/build with the correct dependencies. If running sam deploy with --template, it will deploys only the template and the local resources that it points to. I will close issue as the document is updated.

wchengru avatar Apr 01 '21 00:04 wchengru