0

So I am using AWS SAM to build and deploy some function to AWS Lambda. Because of my slow connection speed uploading functions is very slow, so I decided to create a Layer with requirements in it. So the next time when I try to deploy function I will not have to upload all 50 mb of requirements, and I can just use already uploaded layer.

Problem is that I could not find any parameter which lets me to just ignore requirements file and just deploy the source code.

Is it even possible?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

2 Answers2

0

I hope I understand your question correctly, but if you'd like to deploy a lambda without any dependencies you can try two things:

  • not running sam build before running sam deploy
  • having an empty requirements.txt file. Then sam build simply does not include any dependencies for that lambda function.

Of course here I assume the layer is already present in AWS and is not included in the same template. If they are defined in the same template, you'd have to split them into two stacks. One with the layer which can be deployed once and one with the lambda referencing that layer.

Unfortunately sam build has no flag to ignore requirements.txt as far as I know, since the core purpose of the command is to build dependencies.

LRutten
  • 1,634
  • 7
  • 17
  • Thanks, I thought build was also creating files mandatory for using deploy. But now I have another problem, it also tries to upload virtual environment directory, is there something like .samignore ? like it is for git to ignore some files. – Mate Bersenadze Feb 22 '21 at 11:36
  • You can control the files you upload to your lambda by modifying the `CodeUri` property of your lambda function. If you have three files, `/template.yml` `/src/lambda_code.py` and `/src/requirements.txt` You can specify a `CodeUri: src` with a `Handler: lambda_code.lambda_handler`. Then only the files in the `src/` folder will be uploaded to your lambda function. – LRutten Feb 22 '21 at 18:25
0

For everyone using image container, this is the solution I have found. It drastically improve the workflow.

Dockerfile [it skip if requirments.txt is unchanged]

FROM public.ecr.aws/lambda/python:3.8 AS build

COPY requirements.txt ./
RUN python3.8 -m pip install -r requirements.txt -t .

COPY app.py ./
COPY model /opt/ml/model

CMD ["app.lambda_handler"]

What I have changed?

This was the default Dockerfile

FROM public.ecr.aws/lambda/python:3.8

COPY app.py requirements.txt ./
COPY model /opt/ml/model

RUN python3.8 -m pip install -r requirements.txt -t .

CMD ["app.lambda_handler"]

This solution is based on https://stackoverflow.com/a/34399661/5723524

Francesco Taioli
  • 2,687
  • 1
  • 19
  • 34