AWS Lambda Console - Upgrade boto3 version
Asked Answered
C

3

13

I am creating a DeepLens project to recognise people, when one of select group of people are scanned by the camera.

The project uses a lambda, which processes the images and triggers the 'rekognition' aws api.

  • When I trigger the API from my local machine - I get a good response

  • When I trigger the API from AWS console - I get failed response

Problem

After much digging, I found that the 'boto3' (AWS python library) is of version:

  • 1.9.62 - on my local machine

  • 1.8.9 - on AWS console

Question

Can I upgrade the 'boto3' library version on the AWS lambda console ?? If so, how ?

Cameraman answered 12/12, 2018 at 6:3 Comment(0)
P
14

If you don't want to package a more recent boto3 version with you function, you can download boto3 with each invocation of the Lambda. Remember that /tmp/ is the directory that Lambda will allow you to download to, so you can use this to temporarily download boto3:

import sys
from pip._internal import main

main(['install', '-I', '-q', 'boto3', '--target', '/tmp/', '--no-cache-dir', '--disable-pip-version-check'])
sys.path.insert(0,'/tmp/')

import boto3
from botocore.exceptions import ClientError

def handler(event, context):
    print(boto3.__version__)
Putsch answered 18/7, 2019 at 16:38 Comment(1)
Note that this will increase cold start times and invocation cost because of the increased processing timeOtterburn
J
4

You can achieve the same with either Python function with dependencies or with a Virtual Environment.

These are the available options other than that you also try to contact Amazon team if they can help you with up-gradation.

Januaryjanuisz answered 12/12, 2018 at 6:25 Comment(2)
Thanks, but the new deployment fails without any errors. Meaning custom runtime debugging is trickier compared to the provided runtime.Cameraman
Note that if you're using the Serverless Framework and serverless-python-requirements plugin, it won't deploy boto3 and botocore by default even if they are in your requirements. You need to set the noDeploy parameter in the configuration and not put boto3 and botocore in itNubile
D
3

I know, you're asking for a solution through Console, but this is not possible (as of my knowledge).

To solve this you need to provide the boto3 version you require to your lambda (either with the solution from user1998671 or with what Shivang Agarwal is proposing). A third solution is to provide the required boto3 version as a layer for the lambda. The big advantage of the layer is that you can re-use it for all your lambdas.
This can be achieved by following the guide from AWS (the following is mainly copied from the linked guide from AWS):

IMPORTANT: Make sure to adjust boto3-mylayer with a for you suitable name.

  1. Create a lib folder by running the following command:
LIB_DIR=boto3-mylayer/python
mkdir -p $LIB_DIR
  1. Install the library to LIB_DIR by running the following command:
pip3 install boto3 -t $LIB_DIR
  1. Zip all the dependencies to /tmp/boto3-mylayer.zip by running the following command:
cd boto3-mylayer
zip -r /tmp/boto3-mylayer.zip .
  1. Publish the layer by running the following command:
aws lambda publish-layer-version --layer-name boto3-mylayer --zip-file fileb:///tmp/boto3-mylayer.zip

The command returns the new layer's Amazon Resource Name (ARN), similar to the following one:

arn:aws:lambda:region:$ACC_ID:layer:boto3-mylayer:1

To attach this layer to your lambda execute the following:

aws lambda update-function-configuration --function-name <name-of-your-lambda> --layers <layer ARN>

To verify the boto version in your lambda you can simply add the following two print commands in your lambda:

print(boto3.__version__)
print(botocore.__version__)
Diseur answered 25/7, 2022 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.