#!/usr/bin/env bash
# Real demos for P193 "How AWS Bedrock Works". Made on the Mac (Docker Desktop) against REAL Amazon Bedrock in our own
# cloudexplainers AWS account (us-east-1), inside the owner's real-AWS demo budget (under $1 a month; this run costs
# well under one cent: about ten small Amazon Nova 2 Lite calls and a few guardrail checks).
#   laptop   Ana's laptop: stock Ubuntu 24.04, the AWS CLI v2 copied from the official amazon/aws-cli image, curl, jq, bc.
# Credentials: a one-hour STS session (assume-role from the Mac's default profile into the account's admin role) cut
# down by a session policy (lab/session-policy.json: list models, call models, guardrails, prices; nothing else),
# passed into the container as environment variables and never printed. No IAM user or access key is created.
# The guardrail the demo creates is tagged video=aws-bedrock-works and deleted at the end (cleanup trap).
# One real interactive bash in a pty (demo_driver.py).
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p193
ROLE=arn:aws:iam::346882411366:role/OrganizationAccountAccessRole
cleanup() {
  docker rm -f $P-laptop >/dev/null 2>&1 || true
  for g in $(aws --profile cloudexplainers --region us-east-1 bedrock list-guardrails \
      --query "guardrails[?name=='studio-assistant'].id" --output text 2>/dev/null); do
    aws --profile cloudexplainers --region us-east-1 bedrock delete-guardrail --guardrail-identifier "$g" \
      && echo "deleted guardrail $g"
  done
}
cleanup
trap cleanup EXIT
read -r AK SK ST < <(aws --profile default sts assume-role --role-arn $ROLE --role-session-name ana-laptop \
  --policy file://lab/session-policy.json --duration-seconds 3600 \
  --query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' --output text)
docker build -q -t $P lab >/dev/null
docker run -d --name $P-laptop --hostname laptop -e AWS_ACCESS_KEY_ID="$AK" -e AWS_SECRET_ACCESS_KEY="$SK" \
  -e AWS_SESSION_TOKEN="$ST" -e AWS_REGION=us-east-1 $P >/dev/null
python3 demo_driver.py
