Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,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, 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)

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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("""
Expand Down
25 changes: 23 additions & 2 deletions 01-features/08-agents-that-transact/00-getting-started/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
Loading