[Python] Unable to run code to invoke Bedrock Model

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to run this code to validate my access to a Bedrock model

    import boto3
    import json

    bedrock_runtime = boto3.client("bedrock-runtime", region_name="us-east-2")


    response = bedrock_runtime.invoke_model(
    #modelId="anthropic.claude-3-sonnet-20240229-v1:0",
    modelId = "arn:aws:bedrock:us-east-2:436759792746:inference-profile/us.anthropic.claude-sonnet-4-20250514-v1:0",
    body=b'{"prompt":"Hello, Claude!", "max_tokens":100}',
    contentType="application/json",
    accept="application/json"
    )
    print(response['body'].read())


    This is the IAM policy for the AWS CLI profile user which is being used to invoke the code

    {
    "Version": "2012-10-17",
    "Statement": [
    {
    "Sid": "AllowListingBedrockModels",
    "Effect": "Allow",
    "Action": [
    "bedrock:ListFoundationModels"
    ],
    "Resource": "*"
    },
    {
    "Sid": "AllowInvokeClaude4",
    "Effect": "Allow",
    "Action": [
    "bedrock:InvokeModel",
    "bedrock:InvokeModelWithResponseStream"
    ],
    "Resource": [
    "arn:aws:bedrock:us-west-2::foundation-model/anthropic.claude-opus-4-20250514-v1:0",
    "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-sonnet-3-20240229-v1:0",
    "arn:aws:bedrock:us-east-2::foundation-model/anthropic.claude-sonnet-4-20250514-v1:0",
    "arn:aws:bedrock:us-west-2::foundation-model/anthropic.claude-sonnet-4-20250514-v1:0",
    "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-sonnet-4-20250514-v1:0",
    "arn:aws:bedrock:us-west-2:436759792746:inference-profile/us.anthropic.claude-sonnet-3-20240229-v1:0",
    "arn:aws:bedrock:us-west-2:436759792746:inference-profile/us.anthropic.claude-opus-4-20250514-v1:0",
    "arn:aws:bedrock:us-east-2:436759792746:inference-profile/us.anthropic.claude-sonnet-4-20250514-v1:0",
    "arn:aws:bedrock:us-east-1:436759792746:inference-profile/us.anthropic.claude-sonnet-4-20250514-v1:0",
    "arn:aws:bedrock:us-west-2:436759792746:inference-profile/us.anthropic.claude-sonnet-4-20250514-v1:0"
    ]
    }
    ]
    }


    But I am still getting this error

    Traceback (most recent call last):

    File "/Users/mayukhghosh/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Code/amazon-bedrock-agents-quickstart/streamlit_agent/test_model_access.py", line 7, in <module>
    response = bedrock_runtime.invoke_model(
    File "/Users/mayukhghosh/anaconda3/lib/python3.10/site-packages/botocore/client.py", line 595, in _api_call
    return self._make_api_call(operation_name, kwargs)
    File "/Users/mayukhghosh/anaconda3/lib/python3.10/site-packages/botocore/context.py", line 123, in wrapper
    return func(*args, **kwargs)
    File "/Users/mayukhghosh/anaconda3/lib/python3.10/site-packages/botocore/client.py", line 1058, in _make_api_call
    raise error_class(parsed_response, operation_name)
    botocore.errorfactory.AccessDeniedException: An error occurred (AccessDeniedException) when calling the InvokeModel operation: You don't have access to the model with the specified model ID.

    Continue reading...

Compartilhe esta Página