Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BotocoreDependencyFix (Resolves Lambda & AWS X-Ray Demo import error) #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cshester
Copy link

@cshester cshester commented May 22, 2024

Resolved package dependency errors with botocore that resulted in 502 Bad Gateway errors from Lambda invocation; repackaged function.zip file now functions as expected, while the prior version fails due to version dependency issues.

Demo currently fails due to this import error:
[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': cannot import name 'DEPRECATED_SERVICE_NAMES' from 'botocore.docs' (/var/task/botocore/docs/__init__.py)

Demo video URL: https://learn.cantrill.io/courses/1101194/lectures/46260482

Tested the modified function.zip version and it works, but it is best to replicate this solution yourself for security reasons rather than approving and merging this pull request.

Detailed Resolution Steps to replicate solution:

1) Set Up a Virtual Environment:

# Navigate to the project directory
cd /path/to/lambda/project
# Create a virtual environment
python3 -m venv venv
# Activate the virtual environment
source venv/bin/activate

2) Install Dependencies:

# Install boto3 and a specific version of botocore 
pip install boto3 botocore==1.27.19  # Example of a compatible version, adjust as needed 
# Install other dependencies
pip install aws-xray-sdk requests

3) Package the Dependencies:

# Navigate to the site-packages directory
cd venv/lib/python3.8/site-packages  # Adjust the Python version if necessary 
# Zip the contents
zip -r9 /path/to/lambda/project/function.zip .

4) Add the Lambda Function Code:

# Navigate to the Lambda function directory
cd /path/to/lambda/project
# Add the lambda function code to the ZIP file
zip -g function.zip lambda_function.py

5) Upload the ZIP to Lambda:

# Upload the function.zip to AWS Lambda via the AWS Console or using the AWS CLI 
aws lambda update-function-code --function-name dog-image-scraper --zip-file fileb://function.zip

6) Verification:

Test the Lambda Function:
Invoke the Lambda function and check if the botocore import error persists.
Monitor CloudWatch logs for any new errors.

Resolved package dependency errors with botocore; repackaged function.zip file after resolving import errors.

Detailed Resolution Steps to replicate solution:
1) Set Up a Virtual Environment:
# Navigate to the project directory
cd /path/to/lambda/project
# Create a virtual environment
python3 -m venv venv
# Activate the virtual environment
source venv/bin/activate

2) Install Dependencies:
# Install boto3 and a specific version of botocore
pip install boto3 botocore==1.27.19  # Example of a compatible version, adjust as needed
# Install other dependencies
pip install aws-xray-sdk requests

3) Package the Dependencies:
# Navigate to the site-packages directory
cd venv/lib/python3.8/site-packages  # Adjust the Python version if necessary
# Zip the contents
zip -r9 /path/to/lambda/project/lambda_function.zip .

4) Add the Lambda Function Code:
# Navigate to the Lambda function directory
cd /path/to/lambda/project
# Add the lambda function code to the ZIP file
zip -g lambda_function.zip lambda_function.py

5) Upload the ZIP to Lambda:
# Upload the function.zip to AWS Lambda via the AWS Console or using the AWS CLI
aws lambda update-function-code --function-name dog-image-scraper --zip-file fileb://function.zip

6) Verification:
Test the Lambda Function:
Invoke the Lambda function and check if the botocore import error persists.
Monitor CloudWatch logs for any new errors.
@cshester
Copy link
Author

Just a note: as this change is for the function.zip file, it is best from a security perspective to replicate the solution rather than merge these changes; this will ensure the repository owner is fully informed as to the changes made. That said, I only made the changes detailed above.

@jasonnowlin
Copy link

@cshester Thank you for this fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants