13

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?

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
red888
  • 27,709
  • 55
  • 204
  • 392
  • Could you show your folder structure and CloudFormation template? I assume you don't have a separate folder for your source code and target artifact? I suggest you create a source and target folder and let your CloudFormation template only point to the target folder for CodeUri for your Lambda function(s). The docs for the AWS CLI do NOT suggest any exclusion option like you want: https://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/index.html – s.hesse Jan 23 '18 at 18:28

1 Answers1

15

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.

Lloyd
  • 8,204
  • 2
  • 38
  • 53
  • So, when I add a CodeUri: ./src, `sam build` starts failing - it fails to find package.json in the root of the project. Deploying the entire project directory makes it so the project is not editable in the web console (size). – Quad64Bit Dec 09 '21 at 13:33
  • So a solution here is to separate out your node libraries into a separate layer. You will add the layer information as a separate resource and define your dependencies similarly to how you defined your CodeUri. Here is a good description https://aws.amazon.com/blogs/compute/working-with-aws-lambda-and-lambda-layers-in-aws-sam/ – AhDev Feb 22 '22 at 15:15