|
6 | 6 | "source": [ |
7 | 7 | "# LangGraph Agent Deployment to Amazon Bedrock AgentCore\n", |
8 | 8 | "\n", |
9 | | - "This notebook demonstrates how to deploy a FastAPI LangGraph agent to Amazon Bedrock AgentCore Runtime and invoke it." |
| 9 | + "This notebook demonstrates how to deploy a FastAPI LangGraph agent to Amazon Bedrock AgentCore Runtime and invoke it.\n", |
| 10 | + "\n", |
| 11 | + "It's tested to run in the Python virtual environment created in `cx-agent-backend`" |
10 | 12 | ] |
11 | 13 | }, |
12 | 14 | { |
|
46 | 48 | "outputs": [], |
47 | 49 | "source": [ |
48 | 50 | "# Configuration\n", |
49 | | - "AWS_REGION = \"us-east-1\"\n", |
| 51 | + "AWS_REGION = os.environ.get(\"AWS_REGION\", os.environ.get(\"AWS_DEFAULT_REGION\", \"us-east-1\"))\n", |
50 | 52 | "AWS_ACCOUNT_ID = boto3.client('sts').get_caller_identity()['Account']\n", |
51 | 53 | "ECR_REPOSITORY = \"langgraph-cx-agent\"\n", |
52 | 54 | "IMAGE_TAG = \"latest\"\n", |
53 | 55 | "ECR_URI = f\"{AWS_ACCOUNT_ID}.dkr.ecr.{AWS_REGION}.amazonaws.com/{ECR_REPOSITORY}:{IMAGE_TAG}\"\n", |
54 | | - "AGENT_RUNTIME_NAME = \"langgraph_cx_agent_110902\"\n", |
| 56 | + "AGENT_RUNTIME_NAME = \"langgraph_cx_agent\"\n", |
55 | 57 | "AGENT_RUNTIME_ROLE_ARN = f\"arn:aws:iam::{AWS_ACCOUNT_ID}:role/agentic-ai-bedrock-role\" # deployed in the prerequisite section\n", |
56 | | - "USER_POOL_ID = \"<your-user-pool-id>\" # Replace with your Cognito User Pool ID from the terraform deployment output\n", |
57 | | - "CLIENT_ID = \"<your-client-id>\" # Replace with your Cognito App Client ID from the terraform deployment output\n", |
58 | | - "CLIENT_SECRET = \"<your-client-secret>\" # Replace with your Cognito App Client Secret from the AWS console\n", |
| 58 | + "USER_POOL_ID = \"\" # TODO: Enter Cognito User Pool ID from the terraform deployment output\n", |
| 59 | + "CLIENT_ID = \"\" # TODO: Enter Cognito App Client ID from the terraform deployment output\n", |
| 60 | + "CLIENT_SECRET = \"\" # TODO: Enter Cognito App Client Secret from the AWS console\n", |
59 | 61 | "\n", |
60 | 62 | "print(f\"Account ID: {AWS_ACCOUNT_ID}\")\n", |
61 | 63 | "print(f\"Region: {AWS_REGION}\")\n", |
62 | 64 | "print(f\"ECR URI: {ECR_URI}\")\n", |
63 | | - "print(f\"User Pool ID: {USER_POOL_ID}\")" |
| 65 | + "print(f\"User Pool ID: {USER_POOL_ID}\")\n", |
| 66 | + "\n", |
| 67 | + "if not (USER_POOL_ID and CLIENT_ID and CLIENT_SECRET):\n", |
| 68 | + " raise ValueError(\n", |
| 69 | + " \"Please set USER_POOL_ID, CLIENT_ID, and CLIENT_SECRET above\"\n", |
| 70 | + " )\n" |
64 | 71 | ] |
65 | 72 | }, |
66 | 73 | { |
|
90 | 97 | "outputs": [], |
91 | 98 | "source": [ |
92 | 99 | "# Login to ECR\n", |
93 | | - "!aws ecr get-login-password --region {AWS_REGION} | docker login --username AWS --password-stdin {AWS_ACCOUNT_ID}.dkr.ecr.{AWS_REGION}.amazonaws.com" |
| 100 | + "!aws ecr get-login-password --region {AWS_REGION} | docker login --username AWS --password-stdin {AWS_ACCOUNT_ID}.dkr.ecr.{AWS_REGION}.amazonaws.com\n", |
| 101 | + "\n", |
| 102 | + "# Alternatively with Finch:\n", |
| 103 | + "#!aws ecr get-login-password --region {AWS_REGION} | finch login --username AWS --password-stdin {AWS_ACCOUNT_ID}.dkr.ecr.{AWS_REGION}.amazonaws.com" |
94 | 104 | ] |
95 | 105 | }, |
96 | 106 | { |
|
100 | 110 | "outputs": [], |
101 | 111 | "source": [ |
102 | 112 | "# Build and tag Docker image for ARM64\n", |
103 | | - "!cd cx-agent-backend && docker buildx build --platform linux/arm64 -t {ECR_URI} --push ." |
| 113 | + "!cd cx-agent-backend && docker buildx build --platform linux/arm64 -t {ECR_URI} --push .\n", |
| 114 | + "\n", |
| 115 | + "# Alternatively with Finch:\n", |
| 116 | + "#!cd cx-agent-backend && finch build --platform linux/arm64 -t {ECR_URI} . && finch push {ECR_URI}" |
104 | 117 | ] |
105 | 118 | }, |
106 | 119 | { |
|
177 | 190 | " os.environ[var] = getpass.getpass(f\"Please provide your {var}\")\n", |
178 | 191 | "\n", |
179 | 192 | "\n", |
180 | | - "_set_if_undefined(\"USERNAME\")\n", |
| 193 | + "_set_if_undefined(\"EMAIL_USERNAME\")\n", |
181 | 194 | "_set_if_undefined(\"TEMPORARY_PASSWORD\")\n", |
182 | 195 | "_set_if_undefined(\"PERMANENT_PASSWORD\")" |
183 | 196 | ] |
|
189 | 202 | "outputs": [], |
190 | 203 | "source": [ |
191 | 204 | "# Access values later in the notebook\n", |
192 | | - "username = os.environ['USERNAME']\n", |
| 205 | + "username = os.environ['EMAIL_USERNAME']\n", |
193 | 206 | "temp_password = os.environ['TEMPORARY_PASSWORD']\n", |
194 | 207 | "permanent_password = os.environ['PERMANENT_PASSWORD']" |
195 | 208 | ] |
|
280 | 293 | " )\n", |
281 | 294 | " \n", |
282 | 295 | " access_token = response['AuthenticationResult']['AccessToken']\n", |
283 | | - " print(f\"Access token: {access_token}\")\n", |
| 296 | + " os.environ[\"COGNITO_TOKEN\"] = access_token\n", |
| 297 | + " print(\"Access token fetched\")\n", |
284 | 298 | " \n", |
285 | 299 | "except Exception as e:\n", |
286 | 300 | " print(f\"Error: {e}\")" |
|
303 | 317 | "import urllib.parse\n", |
304 | 318 | "import json\n", |
305 | 319 | "\n", |
306 | | - "def invoke_agent(message, agent_arn, auth_token, session_id, region=\"us-east-1\"):\n", |
| 320 | + "def invoke_agent(message, agent_arn, auth_token, session_id, region=AWS_REGION):\n", |
307 | 321 | " \"\"\"Invoke Bedrock AgentCore runtime with a message.\"\"\"\n", |
308 | 322 | " escaped_agent_arn = urllib.parse.quote(agent_arn, safe='')\n", |
309 | 323 | " url = f\"https://bedrock-agentcore.{region}.amazonaws.com/runtimes/{escaped_agent_arn}/invocations?qualifier=DEFAULT\"\n", |
|
338 | 352 | "metadata": {}, |
339 | 353 | "outputs": [], |
340 | 354 | "source": [ |
341 | | - "AUTH_TOKEN = \"\"\n", |
342 | | - "AGENT_ARN = \"\"\n", |
| 355 | + "AUTH_TOKEN = os.environ[\"COGNITO_TOKEN\"] # (See above)\n", |
| 356 | + "AGENT_ARN = agent_runtime_arn\n", |
343 | 357 | "SESSION_ID = str(uuid.uuid4())\n", |
344 | 358 | " \n", |
345 | 359 | "result = invoke_agent(\"Hello, can you help me?\", AGENT_ARN, AUTH_TOKEN, SESSION_ID)\n", |
|
393 | 407 | ], |
394 | 408 | "metadata": { |
395 | 409 | "kernelspec": { |
396 | | - "display_name": "Python 3", |
| 410 | + "display_name": ".venv", |
397 | 411 | "language": "python", |
398 | 412 | "name": "python3" |
399 | 413 | }, |
|
407 | 421 | "name": "python", |
408 | 422 | "nbconvert_exporter": "python", |
409 | 423 | "pygments_lexer": "ipython3", |
410 | | - "version": "3.11.5" |
| 424 | + "version": "3.12.8" |
411 | 425 | } |
412 | 426 | }, |
413 | 427 | "nbformat": 4, |
|
0 commit comments