aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

Local Python package not being included in build directory

Open jamie-burks opened this issue 5 years ago • 5 comments

Description:

I have a local Python package that is not being included in the build directory created by sam build. We use conda as our package manager, and after running conda update boto3 today this stopped working. No other changes were made at the time this broke, just the update to boto3 which also updated several other standard packages. We have tried re-creating the Conda virtual environment from scratch with no change in behavior.

Steps to reproduce:

  1. Create new Coda virtual environment, e.g. conda env create -f conda.yml where conda.yml contains
name: api
channels:
  - conda-forge
  - defaults
dependencies:
  - osmnx
  - boto3
  - ptvsd
  - etc ...
prefix: /Users/jb/miniconda3/envs/api
  1. Activate the Conda virtual environment, e.g. conda activate api
  2. Setup local editable package:
cd stdapplib
conda develop .
cd ..
  1. Run sam build:
aws build \
    --build-dir .aws-sam-api/build \
    --template-file deploy_scripts/api_template.yaml \
    --parameter-overrides \
        AppName=api \
        etc... \
    --base-dir api

The api directory has source code and a requirements.txt file with the local package and other packages listed:

-i https://pypi.org/simple
./stdapplib
aws-xray-sdk~=2.4.3
boto3~=1.11.2
botocore~=1.14.2
etc ...

The stdapplib directory contains README.md, setup.py, and requirements.txt files, a stdapplib directory containing the package source code (including an empty __init__.py), and a stdapplib.egg-info directory.

Observed result:

The output build directory .aws-sam/build/Lambda does not contain stdapplib but it does contain other standard packages listed in requirements.txt such as boto3.

Debug output from sam build does not list stdapplib anywhere:

2021-03-22 15:15:18,233 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2021-03-22 15:15:18,351 | 'build' command is called
2021-03-22 15:15:18,405 | Collected default values for parameters: {}
2021-03-22 15:15:18,437 | 3 resources found in the template
2021-03-22 15:15:18,437 | Found Serverless function with name='ApiDispatcher' and CodeUri='../api/'
2021-03-22 15:15:18,437 | Collected default values for parameters: {}
2021-03-22 15:15:18,749 | Instantiating build definitions
2021-03-22 15:15:18,752 | Same function build definition found, adding function (Previous: BuildDefinition(python3.8, ../api/, Zip, bf91e3a61b62ac11416040268507dd10, 3f706796-8dc8-4a15-a98c-2594c7c30dbf, {}, []), Current: BuildDefinition(python3.8, ../api/, Zip, , c3ef9704-3219-4753-88e7-32731e40d430, {}, []), Function: Function(name='ApiDispatcher', functionname='dev_v1_api_dispatcher', runtime='python3.8', memory=512, timeout=30, handler='api_api.api_dispatcher', imageuri=None, packagetype='Zip', imageconfig=None, codeuri='../api/', environment={'Variables': {'APP_REGION': 'us-east-1', 'ENV_TYPE': 'dev', 'LOG_LEVEL': 'DEBUG', 'AWS_XRAY_SDK_ENABLED': 'true', <redacted>
2021-03-22 15:15:18,753 | Building codeuri: ../api/ runtime: python3.8 metadata: {} functions: ['ApiDispatcher']
2021-03-22 15:15:18,753 | Building to following folder /Users/jb/Projects/api/.aws-sam-admin/build/ApiDispatcher
2021-03-22 15:15:18,754 | Loading workflow module 'aws_lambda_builders.workflows'
2021-03-22 15:15:18,757 | Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
2021-03-22 15:15:18,759 | Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
2021-03-22 15:15:18,761 | Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
2021-03-22 15:15:18,763 | Registering workflow 'GoDepBuilder' with capability 'Capability(language='go', dependency_manager='dep', application_framework=None)'
2021-03-22 15:15:18,764 | Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
2021-03-22 15:15:18,768 | Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
2021-03-22 15:15:18,770 | Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
2021-03-22 15:15:18,771 | Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
2021-03-22 15:15:18,773 | Registering workflow 'CustomMakeBuilder' with capability 'Capability(language='provided', dependency_manager=None, application_framework=None)'
2021-03-22 15:15:18,773 | Found workflow 'PythonPipBuilder' to support capabilities 'Capability(language='python', dependency_manager='pip', application_framework=None)'
2021-03-22 15:15:18,804 | Running workflow 'PythonPipBuilder'
2021-03-22 15:15:18,804 | Running PythonPipBuilder:ResolveDependencies
2021-03-22 15:15:18,862 | calling pip download -r /Users/jb/Projects/api/api/requirements.txt --dest /var/folders/4k/m_c5978n55n9c0f69d2kbwd80000gn/T/tmpbb9_ivg6
2021-03-22 15:15:25,133 | Full dependency closure: {pytz==2020.5(wheel), boto3==1.11.17(wheel), six==1.13.0(wheel), docutils==0.15.2(wheel), python-dateutil==2.8.1(wheel), botocore==1.14.17(wheel), certifi==2019.11.28(wheel), chardet==3.0.4(wheel), urllib3==1.25.11(wheel), jsonpickle==1.5.2(wheel), s3transfer==0.3.6(wheel), aws-xray-sdk==2.4.3(wheel), wrapt==1.11.2(sdist), requests==2.24.0(wheel), jmespath==0.9.5(wheel), ptvsd==4.3.2(wheel), idna==2.10(wheel), dwollav2==2.2.0(sdist), stripe==2.55.2(wheel), future==0.18.2(sdist)}
2021-03-22 15:15:25,134 | initial compatible: {s3transfer==0.3.6(wheel), pytz==2020.5(wheel), aws-xray-sdk==2.4.3(wheel), boto3==1.11.17(wheel), six==1.13.0(wheel), docutils==0.15.2(wheel), python-dateutil==2.8.1(wheel), requests==2.24.0(wheel), ptvsd==4.3.2(wheel), idna==2.10(wheel), botocore==1.14.17(wheel), jmespath==0.9.5(wheel), certifi==2019.11.28(wheel), stripe==2.55.2(wheel), chardet==3.0.4(wheel), urllib3==1.25.11(wheel), jsonpickle==1.5.2(wheel)}
2021-03-22 15:15:25,134 | initial incompatible: {dwollav2==2.2.0(sdist), wrapt==1.11.2(sdist), future==0.18.2(sdist)}
2021-03-22 15:15:25,134 | Downloading missing wheels: {dwollav2==2.2.0(sdist), wrapt==1.11.2(sdist), future==0.18.2(sdist)}
2021-03-22 15:15:25,134 | calling pip download --only-binary=:all: --no-deps --platform manylinux2014_x86_64 --implementation cp --abi cp38 --dest /var/folders/4k/m_c5978n55n9c0f69d2kbwd80000gn/T/tmpbb9_ivg6 dwollav2==2.2.0
2021-03-22 15:15:25,707 | calling pip download --only-binary=:all: --no-deps --platform manylinux2014_x86_64 --implementation cp --abi cp38 --dest /var/folders/4k/m_c5978n55n9c0f69d2kbwd80000gn/T/tmpbb9_ivg6 wrapt==1.11.2
2021-03-22 15:15:26,258 | calling pip download --only-binary=:all: --no-deps --platform manylinux2014_x86_64 --implementation cp --abi cp38 --dest /var/folders/4k/m_c5978n55n9c0f69d2kbwd80000gn/T/tmpbb9_ivg6 future==0.18.2
2021-03-22 15:15:26,804 | compatible wheels after second download pass: {s3transfer==0.3.6(wheel), pytz==2020.5(wheel), aws-xray-sdk==2.4.3(wheel), boto3==1.11.17(wheel), six==1.13.0(wheel), ptvsd==4.3.2(wheel), docutils==0.15.2(wheel), idna==2.10(wheel), python-dateutil==2.8.1(wheel), stripe==2.55.2(wheel), botocore==1.14.17(wheel), requests==2.24.0(wheel), jmespath==0.9.5(wheel), certifi==2019.11.28(wheel), chardet==3.0.4(wheel), urllib3==1.25.11(wheel), jsonpickle==1.5.2(wheel)}
2021-03-22 15:15:26,804 | Build missing wheels from sdists (C compiling True): {wrapt==1.11.2(sdist), dwollav2==2.2.0(sdist), future==0.18.2(sdist)}
2021-03-22 15:15:26,804 | calling pip wheel --no-deps --wheel-dir /var/folders/4k/m_c5978n55n9c0f69d2kbwd80000gn/T/tmpbb9_ivg6 /var/folders/4k/m_c5978n55n9c0f69d2kbwd80000gn/T/tmpbb9_ivg6/wrapt-1.11.2.tar.gz
2021-03-22 15:15:28,254 | calling pip wheel --no-deps --wheel-dir /var/folders/4k/m_c5978n55n9c0f69d2kbwd80000gn/T/tmpbb9_ivg6 /var/folders/4k/m_c5978n55n9c0f69d2kbwd80000gn/T/tmpbb9_ivg6/dwollav2-2.2.0.tar.gz
2021-03-22 15:15:29,277 | calling pip wheel --no-deps --wheel-dir /var/folders/4k/m_c5978n55n9c0f69d2kbwd80000gn/T/tmpbb9_ivg6 /var/folders/4k/m_c5978n55n9c0f69d2kbwd80000gn/T/tmpbb9_ivg6/future-0.18.2.tar.gz
2021-03-22 15:15:30,859 | compatible after building wheels (no C compiling): {pytz==2020.5(wheel), boto3==1.11.17(wheel), six==1.13.0(wheel), docutils==0.15.2(wheel), python-dateutil==2.8.1(wheel), botocore==1.14.17(wheel), certifi==2019.11.28(wheel), chardet==3.0.4(wheel), urllib3==1.25.11(wheel), jsonpickle==1.5.2(wheel), s3transfer==0.3.6(wheel), aws-xray-sdk==2.4.3(wheel), requests==2.24.0(wheel), jmespath==0.9.5(wheel), ptvsd==4.3.2(wheel), idna==2.10(wheel), dwollav2==2.2.0(wheel), stripe==2.55.2(wheel), future==0.18.2(wheel)}
2021-03-22 15:15:30,859 | Build missing wheels from sdists (C compiling False): {wrapt==1.11.2(sdist)}
2021-03-22 15:15:30,859 | calling pip wheel --no-deps --wheel-dir /var/folders/4k/m_c5978n55n9c0f69d2kbwd80000gn/T/tmpbb9_ivg6 /var/folders/4k/m_c5978n55n9c0f69d2kbwd80000gn/T/tmpbb9_ivg6/wrapt-1.11.2.tar.gz
2021-03-22 15:15:31,914 | compatible after building wheels (C compiling): {pytz==2020.5(wheel), boto3==1.11.17(wheel), six==1.13.0(wheel), docutils==0.15.2(wheel), python-dateutil==2.8.1(wheel), botocore==1.14.17(wheel), certifi==2019.11.28(wheel), chardet==3.0.4(wheel), urllib3==1.25.11(wheel), jsonpickle==1.5.2(wheel), s3transfer==0.3.6(wheel), aws-xray-sdk==2.4.3(wheel), wrapt==1.11.2(wheel), requests==2.24.0(wheel), jmespath==0.9.5(wheel), ptvsd==4.3.2(wheel), idna==2.10(wheel), dwollav2==2.2.0(wheel), stripe==2.55.2(wheel), future==0.18.2(wheel)}
2021-03-22 15:15:31,914 | Final compatible: {pytz==2020.5(wheel), boto3==1.11.17(wheel), six==1.13.0(wheel), docutils==0.15.2(wheel), python-dateutil==2.8.1(wheel), requests==2.24.0(wheel), botocore==1.14.17(wheel), jmespath==0.9.5(wheel), certifi==2019.11.28(wheel), chardet==3.0.4(wheel), urllib3==1.25.11(wheel), jsonpickle==1.5.2(wheel), s3transfer==0.3.6(wheel), aws-xray-sdk==2.4.3(wheel), ptvsd==4.3.2(wheel), idna==2.10(wheel), dwollav2==2.2.0(wheel), stripe==2.55.2(wheel), wrapt==1.11.2(wheel), future==0.18.2(wheel)}
2021-03-22 15:15:31,914 | Final incompatible: {wrapt==1.11.2(wheel)}
2021-03-22 15:15:31,914 | Final missing wheels: set()
2021-03-22 15:15:32,868 | PythonPipBuilder:ResolveDependencies succeeded
2021-03-22 15:15:32,868 | Running PythonPipBuilder:CopySource
2021-03-22 15:15:32,874 | PythonPipBuilder:CopySource succeeded

Build Succeeded

Built Artifacts  : .aws-sam-admin/build
Built Template   : .aws-sam-admin/build/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke -t .aws-sam-admin/build/template.yaml
[*] Deploy: sam deploy --guided --template-file .aws-sam-admin/build/template.yaml
    
2021-03-22 15:15:32,904 | Sending Telemetry: {'metrics': [{'commandRun': {'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'duration': 14670, 'exitReason': 'success', 'exitCode': 0, 'requestId': '691383d3-bfb0-44d6-87f8-729ab32820b5', 'installationId': '07d3d29b-e32f-4695-b32c-d2f628993df1', 'sessionId': 'adca501d-f5b7-4cf2-b80e-4a633d112c46', 'executionEnvironment': 'CLI', 'pyversion': '3.8.7', 'samcliVersion': '1.15.0'}}]}
2021-03-22 15:15:34,361 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)

Expected result:

Expect to see stdapplib include in build directory as it has been in the past. Build directory is being fully re-created by sam build based on directory timestamps.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: macOS 10.15.7
  2. sam --version: 1.15.0
  3. AWS region: us-east-1

Add --debug flag to command you are running

jamie-burks avatar Mar 22 '21 20:03 jamie-burks

Adding -e to requirements.txt does cause a version of the package to be included in the build directory, but not the correct one. It's pulling an old version from the package build directory instead of the source directory. Removing the build directory causes it to pull from the source directory, but why would that behavior change? Is there some switch to override that behavior? Seems strange this would change after only updating boto3 and related packages.

jamie-burks avatar Mar 22 '21 21:03 jamie-burks

This behaviour is related to how pip instals the dependent packages from the requirements file, is not related to how sam cli is building the lambda function code.

I think the best way to handle this case is to have the local package (stdapplib) as a layer for your lambda function, so you can update your template to something like the following:

    mainFunc:
        Type: AWS::Serverless::Function 
        Properties:
            CodeUri: hello-world/
            Handler: app.lambdaHandler
            Layers:
              - !Ref StdAppLibLayer
    StdAppLibLayer:
        Type: AWS::Serverless::LayerVersion
        Properties:
            LayerName: main-func-dependencies
            ContentUri: stdapplib/
            CompatibleRuntimes:
              - python3.7
              - python3.8
        Metadata:
            BuildMethod: python3.8 # the correct python version to build the lib package

then you can run sam build && sam local invoke commands to build and local test

moelasmar avatar Mar 29 '21 06:03 moelasmar

please let us know if this helps you to answer your question, or if you have more questions

moelasmar avatar Mar 29 '21 06:03 moelasmar

We have not worked with layers yet, but from what I'm seeing it should work. We reference this package from multiple SAM templates, but it looks like we just need the ARN to reference it from multiple templates. We reference other resources via their ARN, so we should be able to make this work. Thanks.

jamie-burks avatar Mar 29 '21 13:03 jamie-burks

@jamie-burks .. you are welcome, I am resolving this issue, please reopen the issue, if you have any more questions

moelasmar avatar Mar 29 '21 21:03 moelasmar