Reputation:
I'm trying to deploy an AWS Lambda function with the SAM CLI tool, from MacOS, not using Docker containers.
requirements.txt
file, in the same directory as my Lambda function fileboto3
botostubs
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
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"
}
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.
Upvotes: 21
Views: 19641
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
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
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
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:
Upvotes: 11
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
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