aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.51k stars 1.17k forks source link

using template_file parameter publishes the entire project from the root folder rather than the publish folder #2664

Closed gbanister closed 2 years ago

gbanister commented 3 years ago

Description:

My C# Lambda function is deployed (sam deploy) correctly without specifying template_file parameter. But, if I include the template_file, the package that gets deployed is the entire project directory, rather than just the publish directory which is at the path src\projectname\bin\Release\.netcoreapp3.1\publish.

My samconfig.toml with the template_file parameter looks like this:

version = 0.1
[default]
[default.deploy]
[default.deploy.parameters]
stack_name = "Vlms-SNS-Lamda-SFDC"
s3_bucket = "aws-sam-cli-managed-default-samclisourcebucket-12ss8v9kxng5r"
s3_prefix = "Vlms-SNS-Lamda-SFDC"
region = "us-west-2"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"
template_file = "template.yaml"

I see the same behavior if I use the -t parameter rather than specifying the template_file in the samconfig.toml sam validate -t template.yaml

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

  1. OS: OSX
  2. sam --version: 1.15.0
  3. AWS region: us-west-2
  4. C# Lambda

Add --debug flag to command you are running

Debug output

➜  VlmsSFLambda git:(master) ✗ sam deploy --debug
2021-02-25 22:10:30,922 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
Uploading to Vlms-SNS-Lamda-SFDC/1d076bc4a51d535354a667ea5330f999  894847 / 894847.0  (100.00%)

        Deploying with following values
        ===============================
        Stack name                 : Vlms-SNS-Lamda-SFDC
        Region                     : us-west-2
        Confirm changeset          : True
        Deployment s3 bucket       : aws-sam-cli-managed-default-samclisourcebucket-12ss8v9kxng5r
        Capabilities               : ["CAPABILITY_IAM"]
        Parameter overrides        : {}

Initiating deployment
=====================
2021-02-25 22:10:34,204 | No Parameters detected in the template
2021-02-25 22:10:34,229 | 1 resources found in the template
2021-02-25 22:10:34,229 | Found Serverless function with name='VlmsSNSFunction' and CodeUri='.'
Uploading to Vlms-SNS-Lamda-SFDC/45f8110cbad430dfd7662d71a5a96ab8.template  1466 / 1466.0  (100.00%)

Waiting for changeset to be created..

CloudFormation stack changeset
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Operation                                                                             LogicalResourceId                                                                     ResourceType                                                                          Replacement                                                                         
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Modify                                                                              VlmsSNSFunctionVlmsSFLambda                                                           AWS::SNS::Subscription                                                                Conditional                                                                         
* Modify                                                                              VlmsSNSFunction                                                                       AWS::Lambda::Function                                                                 False                                                                               
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Changeset created successfully. arn:aws:cloudformation:us-west-2:193491626127:changeSet/samcli-deploy1614312634/703ad1db-e6ae-465e-8997-d74755c967bf
gbanister commented 3 years ago

I made this easier to reproduce. See this repo: https://github.com/gbanister/HelloLambda/tree/master/HelloLambda sam deploy will package and publish the correct files. sam deploy -t template.yaml will package the entire project folder

I've also observed the sam package command also packages the entire project folder rather than the build products from the publish folder: sam package --template-file template.yaml --output-template-file packaged.yaml --s3-bucket hello-lambda-package

gbanister commented 3 years ago

I figured out how to do what I needed to do.
I have multiple environments defined in my samconfig.tomi and I have a template yaml file for each environment. To build, package, and deploy to the non-default environments, it works if I use these commands:

sam build -t template-develop.yaml --config-env develop
sam package -t ./.aws-sam/build/template.yaml --output-template-file packaged.yaml --s3-bucket sam-packages-develop
sam deploy -t packaged.yaml --stack-name My-Lambda-develop  

if there happens to be an easier way, let me know.

timoguin commented 3 years ago

I was hit by this too, and it took me a while to figure out. Using build/deploy worked as expected. However, if I used package/publish and then deployed from the SAR, it would throw errors about the deployment package being too big. Once I downloaded the packaged source from S3, I was able to see that my entire git repo had been uploaded, including all the metadata in the .git/ directory.

The behavior I expected was for sam package to use the artifacts from sam build.

Edit: Like @gbanister noted above, I was also able to resolve this issue by telling sam package to use the template from .aws-sam/build/template.yaml instead of template.yaml in the repo root.

wchengru commented 2 years ago

sam deploy and sam package commands use the template in .aws-sam/build/template.yaml by default. The relative paths of the local resources are resolved in the new template. If the --template is specified for these command, the local resources that it points to is not built. In this case, the whole project will be published. To resolve this, we suggest to run sam build with --template to specify the template you want to build, and then run sam deploy or sam package without --template option Please see the notes of --template options in these documents:

xazhao commented 2 years ago

Closing because it has been inactive for 2 weeks. Feel free to reopen it if you have any other questions.