Skip to content

How do I upgrade boto3 and botocore in Lambda to access newer AI models?

3 minute read
1

I want to install the latest version of boto3 and botocore in AWS Lambda to access the latest AI models in Amazon Bedrock.

Short description

If you use a Lambda function with Python to invoke an Amazon Bedrock model, then you might receive the "Error: "errorMessage": "Unknown service: 'bedrock'" error message. To resolve this issue, upgrade the boto3 and botocore libraries to the latest versions in Lambda.

Note: When you use Amazon Bedrock foundation models, you must adhere to the seller's pricing terms.

Resolution

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version. The following AWS CLI commands work for Linux, Unix, and macOS operating systems.

Prerequisite: Make sure that you can access the Amazon Bedrock foundation models.

To upgrade boto3 and botocore, complete the following steps:

  1. Run the following command to create a temporary folder:

    LIB_DIR=boto3-mylayer/python
    mkdir -p $LIB_DIR

    Note: Replace boto3-mylayer with your temporary folder name.

  2. Run the following command to install the boto3 library to LIB_DIR:

    pip3 install boto3==1.42.44 -t $LIB_DIR
    pip3 install botocore==1.42.44 -t $LIB_DIR
  3. Run the following command to zip all dependencies to /tmp/boto3-mylayer.zip:

    cd boto3-mylayer
    zip -r /tmp/boto3-mylayer.zip .

    Note: Replace boto3-mylayer with your temporary folder name.

  4. To publish the layer, run the following publish-layer-version AWS CLI command:

    aws lambda publish-layer-version --layer-name boto3-mylayer --zip-file fileb:///tmp/boto3-mylayer.zip

    Note: Replace layer-name with your Lambda layer name and boto3-mylayer with your temporary folder name. In the output, note the layer's Amazon Resource Name (ARN).

  5. Create a Lambda function.

  6. To attach the layer that you created to the Lambda function, run the following update-function-configuration command:

    aws lambda update-function-configuration --function-name MY_FUNCTION --layer_ARN

    Note: Replace layer_ARN with the layer ARN. For more information, see Adding layers to functions.

  7. To test your update, run the following code to invoke Anthropic Claude Opus 4.6:

    import boto3
    import botocore
    import json
    
    def lambda_handler(event, context):
      print("Boto3 version:", boto3.__version__)
      print("Botocore version:", botocore.__version__) 
      bedrock_runtime = boto3.client(service_name='bedrock-runtime', region_name='us-east-1', endpoint_url='https://bedrock-runtime.us-east-1.amazonaws.com')
      
      # event = {
      #       "anthropic_version": "bedrock-2023-05-31",
      #       "max_tokens": 4000,
      #       "top_k": 250,
      #       "top_p": 1,
      #       "messages": [
      #           {
      #               "role": "user",
      #               "content": "Why is the sky blue?"
      #           }
      #       ],
      #       "stop_sequences": ["Command:"]
      #   }
      print(f"EVENT: {event}")
      bedrock_str = json.dumps(event)
      print(f"BEDROCK_STR: {bedrock_str}")
      modelId = 'global.anthropic.claude-opus-4-5-20251101-v1:0'
      
      bodyprompt = {
            "anthropic_version": "bedrock-2023-05-31",
            "max_tokens": 4000,
            "top_k": 250,
            "top_p": 1,
            "messages": [
                {
                    "role": "user",
                    "content": "Why is the sky blue?"
                }
            ],
            "stop_sequences": ["Command:"]
        }
      response = bedrock_runtime.invoke_model(body=bedrock_str, modelId=modelId, accept='application/json', contentType='application/json')
    #   response = bedrock_runtime.invoke_model(body= json.dumps(bodyprompt), modelId=modelId, accept='application/json', contentType='application/json')
      response_body = json.loads(response.get('body').read())
      print(response_body)
      
      return {'statusCode': 200, 'body': json.dumps(response_body)}

    Note: Replace region_name with your AWS Region and endpoint_url with the endpoint for Amazon Bedrock.