Skip to content

Help to Call bedrock using boto3 from inside lambda function. Getting error 'wrong endpoint' / 'no endpoint'

0

I am trying to call boto3 client bedrock (invoke_model or converse). I am using boto3 1.35.35 and tried with 1.34.34. I tried for the lambda function python 10, 11 and 12. I already tried to pass the parameter endpoint_url with no effect. The error happens when I try to create the client object. code:

config = Config( read_timeout=600, # 3 minutes in seconds connect_timeout=600, ) bedrock_clientConverse = boto3.client('bedrock-runtime', aws_access_key_id=####", aws_secret_access_key="######", region_name='us-east-1', config=config )

This code works fine when I am calling from an EC2 instance that I have. The lambda function has the same VPC/subnet/security groups as my EC2 instance.

The lambda role has all permissions to bedrock { "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": "bedrock:", "Resource": [ "", "*" ] } ] }

But I get the errors: In the lambda execution panel: { "errorMessage": "Invalid endpoint: bedrock-runtime.us-east-1.amazonaws.com", "errorType": "ValueError", "requestId": "",

and in the cloudwatch log: "invalid endpoint"

2024-10-13T15:38:35.593Z INIT_START Runtime Version: python:3.12.v36 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:188d9ca2e2714ff5637bd2bbe06ceb81ec3bc408a0f277dab104c14cd814b081 2024-10-13T15:38:36.345Z [DEBUG] 2024-10-13T15:38:36.345Z ++++++++++++++++++++++++++++++++++++++++ 2024-10-13T15:38:36.346Z [DEBUG] 2024-10-13T15:38:36.346Z Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane 2024-10-13T15:38:36.348Z [DEBUG] 2024-10-13T15:38:36.348Z Changing event name from before-call.apigateway to before-call.api-gateway 2024-10-13T15:38:36.349Z [DEBUG] 2024-10-13T15:38:36.349Z Changing event name from request-created.machinelearning.Predict to request-created.machine-learning.Predict 2024-10-13T15:38:36.351Z [DEBUG] 2024-10-13T15:38:36.351Z Changing event name from before-parameter-build.autoscaling.CreateLaunchConfiguration to before-parameter-build.auto-scaling.CreateLaunchConfiguration 2024-10-13T15:38:36.351Z [DEBUG] 2024-10-13T15:38:36.351Z Changing event name from before-parameter-build.route53 to before-parameter-build.route-53 2024-10-13T15:38:36.352Z [DEBUG] 2024-10-13T15:38:36.352Z Changing event name from request-created.cloudsearchdomain.Search to request-created.cloudsearch-domain.Search 2024-10-13T15:38:36.352Z [DEBUG] 2024-10-13T15:38:36.352Z Changing event name from docs..autoscaling.CreateLaunchConfiguration.complete-section to docs..auto-scaling.CreateLaunchConfiguration.complete-section 2024-10-13T15:38:36.355Z [DEBUG] 2024-10-13T15:38:36.355Z Changing event name from before-parameter-build.logs.CreateExportTask to before-parameter-build.cloudwatch-logs.CreateExportTask 2024-10-13T15:38:36.355Z [DEBUG] 2024-10-13T15:38:36.355Z Changing event name from docs..logs.CreateExportTask.complete-section to docs..cloudwatch-logs.CreateExportTask.complete-section 2024-10-13T15:38:36.355Z [DEBUG] 2024-10-13T15:38:36.355Z Changing event name from before-parameter-build.cloudsearchdomain.Search to before-parameter-build.cloudsearch-domain.Search 2024-10-13T15:38:36.356Z [DEBUG] 2024-10-13T15:38:36.356Z Changing event name from docs..cloudsearchdomain.Search.complete-section to docs..cloudsearch-domain.Search.complete-section 2024-10-13T15:38:36.367Z [DEBUG] 2024-10-13T15:38:36.367Z Loading JSON file: /opt/python/botocore/data/endpoints.json 2024-10-13T15:38:36.394Z [DEBUG] 2024-10-13T15:38:36.394Z Loading JSON file: /opt/python/botocore/data/sdk-default-configuration.json 2024-10-13T15:38:36.394Z [DEBUG] 2024-10-13T15:38:36.394Z Event choose-service-name: calling handler <function handle_service_name_alias at 0x7f4c5fd8aa20> 2024-10-13T15:38:36.430Z [DEBUG] 2024-10-13T15:38:36.430Z Loading JSON file: /opt/python/botocore/data/bedrock-runtime/2023-09-30/service-2.json.gz 2024-10-13T15:38:36.449Z [DEBUG] 2024-10-13T15:38:36.449Z Loading JSON file: /opt/python/botocore/data/bedrock-runtime/2023-09-30/endpoint-rule-set-1.json.gz 2024-10-13T15:38:36.450Z [DEBUG] 2024-10-13T15:38:36.450Z Loading JSON file: /opt/python/botocore/data/partitions.json 2024-10-13T15:38:36.451Z [DEBUG] 2024-10-13T15:38:36.451Z Event creating-client-class.bedrock-runtime: calling handler <function add_generate_presigned_url at 0x7f4c5fc5bec0> 2024-10-13T15:38:36.451Z [DEBUG] 2024-10-13T15:38:36.451Z Creating a regex based endpoint for bedrock-runtime, us-east-1 2024-10-13T15:38:36.455Z [ERROR] ValueError: Invalid endpoint: bedrock-runtime.us-east-1.amazonaws.com Traceback (most recent call last): File "/var/lang/lib/python3.12/importlib/init.py", line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1387, in _gcd_import File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 935, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 995, in exec_module File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed File "/var/task/lambda_function.py", line 19, in <module> bedrock_clientConverse = boto3.client('bedrock-runtime', File "/opt/python/boto3/init.py", line 92, in client return _get_default_session().client(*args, **kwargs) File "/opt/python/boto3/session.py", line 297, in client return self._session.create_client( File "/opt/python/botocore/session.py", line 997, in create_client client = client_creator.create_client( File "/opt/python/botocore/client.py", line 165, in create_client client_args = self._get_client_args( File "/opt/python/botocore/client.py", line 524, in _get_client_args return args_creator.get_client_args( File "/opt/python/botocore/args.py", line 138, in get_client_args endpoint = endpoint_creator.create_endpoint( File "/opt/python/botocore/endpoint.py", line 408, in create_endpoint raise ValueError(f"Invalid endpoint: {endpoint_url}")

[ERROR] ValueError: Invalid endpoint: bedrock-runtime.us-east-1.amazonaws.com Traceback (most recent call last): File "/va

2 Answers
1

Hello Andre, sorry to hear about your challenges with invoking the Amazon Bedrock runtime endpoint. I have faced similar problems in the past as well, these are the steps I went through to resolve my issue:

  1. Update the AWS SDK: Ensure you're using the latest version of the AWS SDK for Python (Boto3). The Bedrock service is relatively new, and older versions of the SDK may not support it.
  2. Check your Lambda function's runtime: Make sure you're using a Python runtime that's compatible with the latest Boto3 version.
  3. Verify the service name: When initializing the Boto3 client, use 'bedrock-runtime' as the service name, not 'bedrock-agent-runtime'. For example:
import boto3
 
client = boto3.client('bedrock-runtime', region_name='us-east-1')
  1. Confirm the region: Ensure you're specifying the correct AWS region where Bedrock is available.
  2. Check Lambda execution role: Make sure the IAM role associated with your Lambda function has the necessary permissions to interact with Amazon Bedrock.
  3. Review Lambda function code: Double-check that you're using the correct method names and parameters when calling Bedrock runtime operations.
  4. Lambda layer: If you're using a Lambda layer for the Boto3 library, ensure it's up to date and includes support for Bedrock.
  5. CloudWatch Logs: Check the CloudWatch Logs for your Lambda function to see if there are any additional error messages or stack traces that could provide more insight into the issue.

One of the issues I encountered was that I had to add a resource-based policy statement on the Lambda.

  1. Go into Lambda function
  2. Select Configuration tab
  3. Select Permission menu item
  4. Scroll down to Resource-based Policy Statements and click Add Permissions button
  5. Click AWS Service radio button
  6. Choose Other from the Service dropdown
  7. Enter anything for Statement ID
  8. Enter bedrock.amazonaws.com for the Principal
  9. Enter your Bedrock Agent's ARN as the Source ARN
  10. Select lambda:InvokeFunction as the Action
  11. Click Save

Hope this helps. If not, I'd recommend opening an AWS Support Case here: https://support.console.aws.amazon.com/support

AWS

answered 2 years ago

0

Hi,

What is the LLM that you try to call ? Make sure that it is supported in us-east-1: currently, not all regions support all models.

You should definitely remove aws_access_key_id=####", aws_secret_access_key="######", from config when running in Lamdba. The credentials to call Bedrock from the Lambda should come only from the Lambda execution role. (BTW, same for your EC2 instance even if code works). It is bad practice to store credentials in source code for multiple reasons (security, credentials rotation, etc.)

Best,

Didier

EXPERT

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.