From 0a84aad47038c3859097b02db37cc360951a2c71 Mon Sep 17 00:00:00 2001 From: Fahad Farrukh Date: Fri, 12 Jun 2026 23:22:01 -0400 Subject: [PATCH] fix(payments): align boto3 region with AWS_REGION from .env MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tutorial .env files set AWS_REGION, but boto3 reads AWS_DEFAULT_REGION instead — AWS_REGION is the env var the AWS SDK for JavaScript honors, not the Python SDK. So a .env that sets AWS_REGION=us-west-2 is silently ignored by boto3, which then falls back to whatever ~/.aws/config has (commonly us-east-1). Resources get created in the wrong region without the script ever printing a warning. Add a small `resolve_region` helper in utils.py that promotes AWS_REGION to AWS_DEFAULT_REGION when the operator hasn't set AWS_DEFAULT_REGION explicitly, then returns the resolved value. The three tutorial scripts that hit the bug (deploy_payment_agent.py, bazaar_gateway_agent.py, utils.setup_cognito_user_pool) now call it before any boto3.Session() so all clients pick up the same region. The setup scripts (setup_agentcore_payments.py, multi_provider_setup.py) already pass region_name= explicitly to boto3.Session, so they aren't affected. --- .../deploy_payment_agent.py | 4 +-- .../bazaar_gateway_agent.py | 5 ++-- .../00-getting-started/utils.py | 25 +++++++++++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/01-features/08-agents-that-transact/00-getting-started/02-deploy-to-agentcore-runtime/deploy_payment_agent.py b/01-features/08-agents-that-transact/00-getting-started/02-deploy-to-agentcore-runtime/deploy_payment_agent.py index 5ac498534..f2d9115dd 100644 --- a/01-features/08-agents-that-transact/00-getting-started/02-deploy-to-agentcore-runtime/deploy_payment_agent.py +++ b/01-features/08-agents-that-transact/00-getting-started/02-deploy-to-agentcore-runtime/deploy_payment_agent.py @@ -38,7 +38,7 @@ from dotenv import load_dotenv sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from utils import load_tutorial_env, print_summary, update_env_file +from utils import load_tutorial_env, print_summary, resolve_region, update_env_file ENV_FILE = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), ".env") load_dotenv(ENV_FILE, override=True) @@ -46,10 +46,10 @@ HERE = os.path.dirname(os.path.abspath(__file__)) # ── Verify AWS credentials ──────────────────────────────────────────────────── +REGION = resolve_region() session = boto3.Session() identity = session.client("sts").get_caller_identity() account_id = identity["Account"] -REGION = session.region_name or os.environ.get("AWS_REGION", "us-west-2") print(f"Authenticated as: {identity['Arn']}") print(f"Account: {account_id}") print(f"Region: {REGION}") diff --git a/01-features/08-agents-that-transact/00-getting-started/04-agent-with-coinbase-bazaar-via-gateway/bazaar_gateway_agent.py b/01-features/08-agents-that-transact/00-getting-started/04-agent-with-coinbase-bazaar-via-gateway/bazaar_gateway_agent.py index 8fef4f2c8..32ba36e9c 100644 --- a/01-features/08-agents-that-transact/00-getting-started/04-agent-with-coinbase-bazaar-via-gateway/bazaar_gateway_agent.py +++ b/01-features/08-agents-that-transact/00-getting-started/04-agent-with-coinbase-bazaar-via-gateway/bazaar_gateway_agent.py @@ -45,17 +45,18 @@ from dotenv import load_dotenv sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from utils import load_tutorial_env, print_summary +from utils import load_tutorial_env, print_summary, resolve_region ENV_FILE = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), ".env") load_dotenv(ENV_FILE, override=True) # ── Verify AWS credentials ──────────────────────────────────────────────────── +REGION = resolve_region() session = boto3.Session() identity = session.client("sts").get_caller_identity() print(f"Authenticated as: {identity['Arn']}") print(f"Account: {identity['Account']}") -print(f"Region: {session.region_name}") +print(f"Region: {REGION}") # ── Step 1: Gateway Setup (manual) ─────────────────────────────────────────── print(""" diff --git a/01-features/08-agents-that-transact/00-getting-started/utils.py b/01-features/08-agents-that-transact/00-getting-started/utils.py index a83c522b1..a3c18a594 100644 --- a/01-features/08-agents-that-transact/00-getting-started/utils.py +++ b/01-features/08-agents-that-transact/00-getting-started/utils.py @@ -35,6 +35,28 @@ # ═════════════════════════════════════════════════════════════════ +def resolve_region(default="us-west-2"): + """Resolve the AWS region the tutorials should use, and align boto3 with it. + + boto3 reads `AWS_DEFAULT_REGION` from the environment but ignores + `AWS_REGION` (the env var name used by the AWS SDK for JavaScript and by + the tutorial `.env` files). Without this helper, a `.env` that sets + `AWS_REGION=us-west-2` is silently ignored and `boto3.Session()` falls + back to whatever `~/.aws/config` says, which is often a different region. + + Promote `AWS_REGION` to `AWS_DEFAULT_REGION` if the operator hasn't set + `AWS_DEFAULT_REGION` explicitly, then return the resolved value. + """ + if "AWS_REGION" in os.environ and "AWS_DEFAULT_REGION" not in os.environ: + os.environ["AWS_DEFAULT_REGION"] = os.environ["AWS_REGION"] + return ( + os.environ.get("AWS_DEFAULT_REGION") + or os.environ.get("AWS_REGION") + or boto3.Session().region_name + or default + ) + + def load_payment_env(env_file=".env"): """Load .env file and return config dict.""" load_dotenv(env_file, override=True) @@ -442,8 +464,7 @@ def setup_cognito_user_pool(pool_name="AgentCorePaymentsPool"): Returns dict with pool_id, client_id, client_secret, token_url. """ - session = boto3.Session() - region = session.region_name + region = resolve_region() cognito = boto3.client("cognito-idp", region_name=region) pool_resp = cognito.create_user_pool(