Skip to content

Instantly share code, notes, and snippets.

@jorotenev
Last active March 29, 2019 11:53
Show Gist options
  • Save jorotenev/32222f365c8c51e477b94778be408794 to your computer and use it in GitHub Desktop.
Save jorotenev/32222f365c8c51e477b94778be408794 to your computer and use it in GitHub Desktop.
Use a custom version of boto3 in AWS Lambda. Handy when a zip was deployed to Lambda containing a specific version of boto3. Without this, the default boto3 will be used because of the PATH env var precedence. this snippet would insert the current directory of the file to the path (assuming he snippet is in a file in the root of the project cont…
import os, logging, sys
if os.getenv('LAMBDA_TASK_ROOT', None): # check if we are in lambda
# add the current folder to the path of python
# assumes that during build time we installed in the current folder packages from a, say, requirements.txt
# we need this so that our packages are found before the already existing ones in the lambda environment
print("setting the path when in lambda")
curr_file_dir_path = os.path.dirname(os.path.abspath(__file__))
print(curr_file_dir_path)
sys.path.insert(0, curr_file_dir_path)
import boto3
print(f'boto3 version is {boto3.__version__}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment