I'm doing a cross-account pipeline for a SAM application. The same application uses a lambda function (hello_world) with aws_lambda_powertools and a simple rest API. This is a sample application deployed with "sam init"
The pipeline works and deploys the resources, but the lambda function is missing the extra modules like requests or aws_lambda_powertools. When testing i get the error "errorMessage": "Unable to import module 'app': No module named 'aws_lambda_powertools'",
In the lambda function, I have
from aws_lambda_powertools import Logger
from aws_lambda_powertools import Tracer
from aws_lambda_powertools import Metrics
......
and there is a hello_world\requirements.txt file with
requests
aws-lambda-powertools[tracer]
The pipeline has a Build stage made with AWS CodeBuild, where I use a buildspec.yaml file.
This is the code in the build:
- echo "Starting SAM packaging `date` in `pwd`"
- pip install --upgrade pip
- pip install pipenv --user
- pip install awscli aws-sam-cli
- pip install -r requirements.txt
- aws cloudformation package --template-file template.yaml --s3-bucket $ArtifactBucket --output-template-file packaged-template.yml
#- sam package --template-file template.yaml --s3-bucket $ArtifactBucket --output-template-file packaged-template.yml --region $AWS_REGION
If i use aws cloudformation package .... the pipeline deploys the code but without aws-lambda-powertools modules. I guess this is normal since is not supposed to pack modules.
If i use - sam package --template-file template.... i get this error Error: Cannot use both --resolve-s3 and --s3-bucket parameters. Please use only one.
THis is dont understand since i do no use --resolve-s3 in my command
My question will be - How to include the modules from the requirments.txt file in the AWS CodeBuild building process so it deploys lambda with all the dependencies?
The solution
since this is a SAM app you have samconfig.toml. Edit it, change resolve_s3 to false and you will not have the Error: Cannot use both --resolve-s3 and --s3-bucket parameters. Please use only one
commands:
date
in pwd
"I added sam build - to build the lamba dependencies and use this path .aws-sam/build/template.yaml instead of template.yaml . This is where we have new build cloudformation template (after sam build).
*** i also added samconfig.toml to git ignore list
The only strange thing is that now I cannot see the lambda function in the console(i know - old habits) because "The deployment package of your Lambda function "***" is too large to enable inline code editing. However, you can still invoke your function". The pack has 12 MB and I don't know if that is normal. Any comment will be appreciated.