Deploying Lambda utilizing Amazon Bedrock

0

Hello,

I am attempting to deploy a lambda function running a docker container via CDK, that will be running an Amazon Bedrock client. I have been having issues with getting credentials. I have tried two different implementations, and may need some guidance

Implementation 1: Using LangChain's Bedrock Wrapper

Bedrock(client=client, credentials_profile_name="default", model_id="ai21.j2-grande-instruct", region_name='us-east-1', model_kwargs=model_kwargs)

Results in the following error when deployed

Could not load credentials to authenticate with AWS client. Please check that credentials in the specified profile name are valid. (type=value_error)
Traceback (most recent call last):
  File "/var/lang/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/var/task/bot_function/__main__.py", line 2, in <module>
    from bot_function.src.bot import BedrockLangChainBot
  File "/var/task/bot_function/src/bot.py", line 13, in <module>
    llm = bedrock()
  File "/var/task/bot_function/src/get_llm.py", line 18, in bedrock
    BEDROCK = Bedrock(credentials_profile_name="default", model_id="ai21.j2-grande-instruct", region_name='us-east-1', model_kwargs=model_kwargs)
  File "/var/lang/lib/python3.10/site-packages/langchain/load/serializable.py", line 74, in __init__
    super().__init__(**kwargs)
  File "/var/lang/lib/python3.10/site-packages/pydantic/main.py", line 341, in __init__
    raise validation_error

Implementation 2: Using the boto3 Bedrock client, and passing as a parameter to LangChain wrapper. Issues will also apply using Bedrock client directly

client = boto3.client('bedrock')
BEDROCK = Bedrock(client=client, credentials_profile_name="default", model_id="ai21.j2-grande-instruct", region_name='us-east-1', model_kwargs=model_kwargs)

Results in an unknown service error

[ERROR] UnknownServiceError: Unknown service: 'bedrock'. 

Please advise. I am able to run both implementations locally.

3 Answers
1

Hi, Bedrock is brand new (still in early preview mode). Boto3 client in Lambda runtime may be a bit behind, so still unaware of Bedrock. So, why don't you check version of Boto3 on Lambda runtime and compare with your local one. That may be the source of your problem.

Also check if you're aligned with versions on this page: https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html

Obviously, using a higher version of Python will bring you a newer version of Boto3.

Best,

Didier

profile pictureAWS
EXPERT
answered 10 months ago
0

I'm not sure if the standard Boto3 builds have the bedrock code yet. Even if you have the same version, make sure you pulled the custom builds for your docker container. I think that is what you are seeing in implementation 2.

In implementation 1, it seems more like the credentials were in an unexpected format. (type=value_error).

Jim

profile pictureAWS
EXPERT
answered 10 months ago
0

I had the same problem, as the bedrock is in beta stage it is not available in recent versions of boto3. To make it work, i have followed https://github.com/aws-samples/amazon-bedrock-workshop, you will find download.sh which gets latest boto3 which supports bedrock. I did build lambda layer with the boto3 downloaded from download.sh script from above github url and used it in my lambda service which worked!!

profile picture
answered 8 months 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.

Guidelines for Answering Questions