Skip to content

Commit 4d29560

Browse files
authored
fix(bedrock): use the AWS_REGION environment variable for the Bedrock model provider region if set and boto_session is not passed (#39)
1 parent 2056888 commit 4d29560

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/strands/models/bedrock.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import logging
7+
import os
78
from typing import Any, Iterable, Literal, Optional, cast
89

910
import boto3
@@ -96,7 +97,8 @@ def __init__(
9697
Args:
9798
boto_session: Boto Session to use when calling the Bedrock Model.
9899
boto_client_config: Configuration to use when creating the Bedrock-Runtime Boto Client.
99-
region_name: AWS region to use for the Bedrock service. Defaults to "us-west-2".
100+
region_name: AWS region to use for the Bedrock service.
101+
Defaults to the AWS_REGION environment variable if set, or "us-west-2" if not set.
100102
**model_config: Configuration options for the Bedrock model.
101103
"""
102104
if region_name and boto_session:
@@ -108,7 +110,7 @@ def __init__(
108110
logger.debug("config=<%s> | initializing", self.config)
109111

110112
session = boto_session or boto3.Session(
111-
region_name=region_name or "us-west-2",
113+
region_name=region_name or os.getenv("AWS_REGION") or "us-west-2",
112114
)
113115
client_config = boto_client_config or BotocoreConfig(user_agent_extra="strands-agents")
114116
self.client = session.client(

tests/strands/models/test_bedrock.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import unittest.mock
23

34
import boto3
@@ -99,6 +100,16 @@ def test__init__with_custom_region(bedrock_client):
99100
mock_session_cls.assert_called_once_with(region_name=custom_region)
100101

101102

103+
def test__init__with_environment_variable_region(bedrock_client):
104+
"""Test that BedrockModel uses the provided region."""
105+
_ = bedrock_client
106+
os.environ["AWS_REGION"] = "eu-west-1"
107+
108+
with unittest.mock.patch("strands.models.bedrock.boto3.Session") as mock_session_cls:
109+
_ = BedrockModel()
110+
mock_session_cls.assert_called_once_with(region_name="eu-west-1")
111+
112+
102113
def test__init__with_region_and_session_raises_value_error():
103114
"""Test that BedrockModel raises ValueError when both region and session are provided."""
104115
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)