title: How do I add a Python library dependency for a Lambda function in CodeBuild
body: <p>I have a CodePipline that grabs code out of CodeCommit bundles it up in CodeBuild and then publishes it via CloudFormation.</p>
  <p>I want to use the Python package gspread and because it's not part of the standard AWS Linux image I need to install it.</p>
  <p>Currently when the code is run I get the error:</p>
  <ul>
  <li>[ERROR] Runtime.ImportModuleError: Unable to import module 'index': No module named 'gspread'</li>
  </ul>
  <p><strong>Code structure</strong></p>
  <pre><code>- buildspec.yml
  - template.yml
  package/
    - gspread/
    - gspread-3.6.0.dist-info/
    - (37 other python packages)
  source/
    - index.py
  </code></pre>
  <p>buildspec.yml -- EDITED</p>
  <pre><code>version: 0.2
  phases:
    install:
      commands:
        # Use Install phase to install packages or any pre-reqs you may need throughout the build (e.g. dev deps, security checks, etc.)
        - echo &quot;[Install phase]&quot;
        - pip install --upgrade pip
        - pip install --upgrade aws-sam-cli
        - sam --version
        - cd source
        - ls
        - pip install --target . gspread oauth2client
        # consider using pipenv to install everything in the environement and then copy the files installed into the /source folder
        - ls
      runtime-versions:
          python: 3.8
    pre_build:
      commands:
        # Use Pre-Build phase to run tests, install any code deps or any other customization before build
        # - echo &quot;[Pre-Build phase]&quot;
    build:
      commands:
        - cd ..
        - sam build
        
    post_build:
      commands:
        # Use Post Build for notifications, git tags and any further customization after build
        - echo &quot;[Post-Build phase]&quot;      
        - export BUCKET=property-tax-invoice-publisher-deployment
        - sam package --template-file template.yml --s3-bucket $BUCKET --output-template-file outputtemplate.yml
        - echo &quot;SAM packaging completed on `date`&quot;
  
  ##################################
  # Build Artifacts to be uploaded #
  ##################################
  artifacts:
    files:
      - outputtemplate.yml
    discard-paths: yes
  
  cache:
    paths:
    # List of path that CodeBuild will upload to S3 Bucket and use in subsequent runs to speed up Builds
      - '/root/.cache/pip'
  </code></pre>
  <p>The index.py file has more in it than this. But to show the offending line.</p>
  <p>-- index.py --</p>
  <pre><code>import os
  import boto3
  import io
  import sys
  import csv
  import json
  import smtplib
  import gspread #**&lt;--- Right here**
  
  def lambda_handler(event, context):
      print(&quot;In lambda_handler&quot;)
  </code></pre>
  <p><strong>What I've tried</strong></p>
  <ul>
  <li>Creating the /package folder and committing the gspread and other packages</li>
  <li>Running &quot;pip install gspread&quot; in the CodeBuild builds: commands:</li>
  </ul>
  <p>At the moment, I'm installing it everywhere and seeing what sticks. (nothing is currently sticking)</p>
  <p>Version: Python 3.8</p>