user189198
user189198

Reputation:

AWS SAM CLI ignoring my Python dependencies during build, package, and deploy

I'm trying to deploy an AWS Lambda function with the SAM CLI tool, from MacOS, not using Docker containers.

Requirements.txt

boto3
botostubs

Deploy Script (PowerShell)

sam build --template-file $InputTemplate
sam package --region $AWSRegion --template-file $InputTemplate --profile $ProfileName --s3-bucket $BucketName --output-template-file $OutputTemplate
sam deploy --region $AWSRegion --profile $ProfileName --template-file $OutputTemplate --stack-name $StackName --capabilities CAPABILITY_NAMED_IAM

Actual Behavior

SAM CLI is ignoring my requirements.txt file, and only deploying my source code. This results in the following error when I test my function.

{
  "errorMessage": "Unable to import module 'xxxxxxxxxxxxxx': No module named 'botostubs'",
  "errorType": "Runtime.ImportModuleError"
}

Expected Behavior

SAM CLI packages up the declared Python dependencies, in requirements.txt, along with my source code.

Question: How can I ensure that the SAM CLI downloads and packages my Python dependencies, along with my source code? I followed the documentation, to the best of my knowledge.

https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-build.html

Upvotes: 21

Views: 19641

Answers (6)

leosok
leosok

Reputation: 332

I use poetry for the dependencies, and use it to create a requirements.txt

poetry export -f requirements.txt --output ./app/requirements.txt --without-hashes

I had used diffent python versions in pyproject.toml (poetry) and template.yaml (sam). So sam would not install any of the requirements:

fastapi==0.109.2 ; python_version >= "3.11" and python_version < "3.12" because they were not compatible with the pyhton3.10 specified in the yaml.

Maybe someone will have the same bug one day...

Upvotes: 2

trex
trex

Reputation: 573

Make sure your 'requirements.txt' file is right under the path specified in 'CodeUri' attribute of the Lambda in your template file.

Upvotes: 5

Michal Szymanski
Michal Szymanski

Reputation: 23

Mmy solution was:
specify --template-file in sam build
run sam deploy without --template-file option

It works but everytime starts script, it ask about confirmation about deploying changset - it is not a problem when I use script but problem appears when it is executed by CI/CD.

Upvotes: 0

Alex M981
Alex M981

Reputation: 2374

I had similar problem and root cause of my failure was that I was specifying --template-file template.yml. As per this issue https://github.com/awslabs/aws-sam-cli/issues/1252 SAM CLI looks for the code uri specified in my template.yml and uploads only the function code.

So my solution was:

  1. specify --template-file in sam build
  2. run sam deploy without --template-file option

Upvotes: 11

Cornelius Roemer
Cornelius Roemer

Reputation: 7819

Had a similar problem. Resolved it by changing directory into the build folder (doesn't use these directory shell variables)

sam build --use-container
cd .aws-sam/build/
sam package --template-file template.yaml --s3-bucket sdd-s3-basebucket --output-template-file packaged.yaml
sam deploy --template-file ./packaged.yaml --stack-name prod --capabilities CAPABILITY_IAM --region eu-central-1

Upvotes: 4

user189198
user189198

Reputation:

Just figured it out, reading about the sam build command in a bit more depth. I didn't realize that it was creating a subfolder called .aws-sam/build/ and storing the modified template there.

I updated my commands and paths, and it is working just fine now.

$InputTemplate = "$PSScriptRoot/cloudformation.yml"
$BuiltTemplate = "$PSScriptRoot/.aws-sam/build/template.yaml"
$BucketName = 'xxxxxxx'
$AWSRegion = 'xxxxxx'
$StackName = 'xxxxxx'

# Package and deploy the application
sam build --template-file $InputTemplate
sam package --region $AWSRegion --template-file $BuiltTemplate --profile $ProfileName --s3-bucket $BucketName
sam deploy --region $AWSRegion --profile $ProfileName --template-file $BuiltTemplate --stack-name $StackName --capabilities CAPABILITY_NAMED_IAM --s3-bucket $BucketName

Upvotes: 22

Related Questions