Search code examples
amazon-web-servicesaws-lambdaaws-sam-cli

How do I excluded folder from sam deploy command?


I run this to deploy my lambda:

sam package --template-file prod_template.yaml --s3-bucket mybucket --output-template-file packaged-template.yaml
sam deploy --template-file packaged-template.yaml --stack-name mystack --capabilities CAPABILITY_IAM

That works but this code is version controlled and sam is also uploading the .git folder. How do I have sam ignore some folders like I can with gitignore?


Solution

  • You need to check you're supplying a valid CodeUri path in your template, it should look something like this:

    AWSTemplateFormatVersion: '2010-09-09'
    Transform: AWS::Serverless-2016-10-31
    Resources:
      Followers:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: ./src
          Handler: lambda.handler
          Runtime: nodejs12.x
          Timeout: 300
    

    The AWS docs state that if the CodeUri is not supplied, the entire working directory will be zipped and uploaded (which I think is what you're experiencing).

    If you specify a file [in CodeUri], the command directly uploads it to the S3 bucket. If you specify a folder, the command zips the folder and then uploads the .zip file. For most resources, if you don't specify a path, the command zips and uploads the current working directory.