jereesh thomas
jereesh thomas

Reputation: 163

AWS bedrock access issue from Python

I have the following code to access bedrock from a jupyter notebook

bedrock_client = boto3.client(
      'bedrock',
      region_name = "us-east-1",
      aws_access_key_id = "xxxxxx",
      aws_secret_access_key = "xxxxxxxxxxxxxxx"
  )

FM_list = bedrock_client.list_foundation_models() print(FM_list)

But it shows the following error

ClientError: An error occurred (UnrecognizedClientException) when calling the ListFoundationModels operation: The security token included in the request is invalid.

Here is the full exception

ClientError                               Traceback (most recent call last)
<ipython-input-17-9a9891b67e85> in <cell line: 7>()
      5           aws_secret_access_key = "xxxxxxxxxxxxxxxxxxxxxxxxx"
      6       )
----> 7 FM_list = bedrock_client.list_foundation_models()
      8 print(FM_list)

1 frames
/usr/local/lib/python3.10/dist-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
   1007             )
   1008             error_class = self.exceptions.from_code(error_code)
-> 1009             raise error_class(parsed_response, operation_name)
   1010         else:
   1011             return parsed_response

I have tried with different tokens

I have copied the token from AWS from the as below - the tokens are for a organization sso account enter image description here

Upvotes: 0

Views: 1067

Answers (1)

lsc
lsc

Reputation: 338

If you click Command line or programmatic access in your screenshot, you'll see something similar to:

export AWS_ACCESS_KEY_ID=ASIAIOSFODNN7EXAMPLE
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
export AWS_SESSION_TOKEN=AQoDYXdzEJr...<remainder of session token>

Set these environmental variables (see doc if you are on Windows). Then execute your Python script:

import boto3

bedrock_client = boto3.client(
      "bedrock",
      region_name = "us-east-1"
)

FM_list = bedrock_client.list_foundation_models()
print(FM_list)

Upvotes: 0

Related Questions