This CDK stack deploys a complete Amazon Bedrock AgentCore Runtime with a sophisticated weather-based activity planning agent. This demonstrates the full power of AgentCore by integrating Browser tool, Code Interpreter, Memory, and S3 storage in a single deployment.
- Overview
- Architecture
- Prerequisites
- Deployment
- Testing
- Sample Queries
- How It Works
- Cleanup
- Troubleshooting
This CDK stack creates a comprehensive AgentCore deployment that showcases:
- AgentCore Runtime: Hosts a Strands agent with multiple tools
- Browser Tool: Web automation for scraping weather data from weather.gov
- Code Interpreter: Python code execution for weather analysis
- Memory: Stores user activity preferences
- S3 Bucket: Stores generated activity recommendations
- ECR Repository: Container image storage
- IAM Roles: Comprehensive permissions for all components
The Weather Activity Planner agent can:
- Scrape Weather Data: Uses browser automation to fetch 8-day forecasts from weather.gov
- Analyze Weather: Generates and executes Python code to classify days as GOOD/OK/POOR
- Retrieve Preferences: Accesses user activity preferences from memory
- Generate Recommendations: Creates personalized activity suggestions based on weather and preferences
- Store Results: Saves recommendations as Markdown files in S3
- Weather-based activity planning
- Automated web scraping and data analysis
- Multi-tool agent orchestration
- Memory-driven personalization
- Asynchronous task processing
The architecture demonstrates a complete AgentCore deployment with multiple integrated tools:
Core Components:
- User: Sends weather-based activity planning queries
- AWS CodeBuild: Builds the ARM64 Docker container image with the agent code
- Amazon ECR Repository: Stores the container image
- AgentCore Runtime: Hosts the Weather Activity Planner Agent
- Weather Agent: Strands agent that orchestrates multiple tools
- Invokes Amazon Bedrock LLMs for reasoning and code generation
- Browser Tool: Web automation for scraping weather data from weather.gov
- Code Interpreter Tool: Executes Python code for weather analysis
- Memory: Stores user activity preferences (30-day retention)
- S3 Bucket: Stores generated activity recommendations
- IAM Roles: Comprehensive permissions for all components
Workflow:
- User sends query: "What should I do this weekend in Richmond VA?"
- Agent extracts city and uses Browser Tool to scrape 8-day forecast
- Agent generates Python code and uses Code Interpreter to classify weather
- Agent retrieves user preferences from Memory
- Agent generates personalized recommendations
- Agent stores results in S3 bucket using use_aws tool
-
AWS Account: You need an active AWS account with appropriate permissions
-
AWS CLI: Install and configure AWS CLI with your credentials
aws configure
-
Python 3.10+ and AWS CDK v2 installed
# Install CDK npm install -g aws-cdk # Verify installation cdk --version
-
CDK version 2.218.0 or later (for BedrockAgentCore support)
-
Bedrock Model Access: Enable access to Amazon Bedrock models in your AWS region
-
Required Permissions: Your AWS user/role needs permissions for:
- CloudFormation stack operations
- ECR repository management
- IAM role creation
- Lambda function creation
- CodeBuild project creation
- BedrockAgentCore resource creation (Runtime, Browser, CodeInterpreter, Memory)
- S3 bucket operations (for CDK assets and results storage)
This is the CDK version of the end-to-end weather agent. If you prefer CloudFormation, see the CloudFormation version.
# Install dependencies
pip install -r requirements.txt
# Bootstrap CDK (first time only)
cdk bootstrap
# Deploy
cdk deploy# 1. Create and activate Python virtual environment
python3 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# 2. Install Python dependencies
pip install -r requirements.txt
# 3. Bootstrap CDK in your account/region (first time only)
cdk bootstrap
# 4. Synthesize the CloudFormation template (optional)
cdk synth
# 5. Deploy the stack
cdk deploy --require-approval never
# 6. Get outputs
cdk list- Expected Duration: 15-20 minutes
- Main Steps:
- Stack creation: ~2 minutes
- Docker image build (CodeBuild): ~10-12 minutes
- Runtime and tools provisioning: ~3-5 minutes
- Memory initialization: ~1 minute
# Get the Runtime ARN from CDK outputs
RUNTIME_ARN=$(aws cloudformation describe-stacks \
--stack-name WeatherAgentDemo \
--region us-east-1 \
--query 'Stacks[0].Outputs[?OutputKey==`AgentRuntimeArn`].OutputValue' \
--output text)
# Get the S3 bucket name
BUCKET_NAME=$(aws cloudformation describe-stacks \
--stack-name WeatherAgentDemo \
--region us-east-1 \
--query 'Stacks[0].Outputs[?OutputKey==`ResultsBucketName`].OutputValue' \
--output text)
# Invoke the agent
aws bedrock-agentcore invoke-agent-runtime \
--agent-runtime-arn $RUNTIME_ARN \
--qualifier DEFAULT \
--payload $(echo '{"prompt": "What should I do this weekend in Richmond VA?"}' | base64) \
response.json
# View the immediate response
cat response.json
# Wait a few minutes for processing, then check S3 for results
aws s3 ls s3://$BUCKET_NAME/
# Download the results
aws s3 cp s3://$BUCKET_NAME/results.md ./results.md
cat results.md- Navigate to Bedrock AgentCore Console
- Go to "Runtimes" in the left navigation
- Find your runtime (name starts with
WeatherAgentDemo_) - Click on the runtime name
- Click "Test" button
- Enter test payload:
{ "prompt": "What should I do this weekend in Richmond VA?" } - Click "Invoke"
- View the immediate response
- Wait 2-3 minutes for background processing
- Navigate to S3 Console to download results.md from the results bucket
Try these queries to test the weather agent:
-
Weekend Planning:
{"prompt": "What should I do this weekend in Richmond VA?"} -
Specific City:
{"prompt": "Plan activities for next week in San Francisco"} -
Different Location:
{"prompt": "What outdoor activities can I do in Seattle this week?"} -
Vacation Planning:
{"prompt": "I'm visiting Austin next week. What should I plan based on the weather?"}
-
User Query: "What should I do this weekend in Richmond VA?"
-
City Extraction: Agent extracts "Richmond VA" from the query
-
Weather Scraping (Browser Tool):
- Navigates to weather.gov
- Searches for Richmond VA
- Clicks "Printable Forecast"
- Extracts 8-day forecast data (date, high, low, conditions, wind, precipitation)
- Returns JSON array of weather data
-
Code Generation (LLM):
- Agent generates Python code to classify weather days
- Classification rules:
- GOOD: 65-80°F, clear, no rain
- OK: 55-85°F, partly cloudy, slight rain
- POOR: <55°F or >85°F, cloudy/rainy
-
Code Execution (Code Interpreter):
- Executes the generated Python code
- Returns list of tuples:
[('2025-09-16', 'GOOD'), ('2025-09-17', 'OK'), ...]
-
Preference Retrieval (Memory):
- Fetches user activity preferences from memory
- Preferences stored by weather type:
{ "good_weather": ["hiking", "beach volleyball", "outdoor picnic"], "ok_weather": ["walking tours", "outdoor dining", "park visits"], "poor_weather": ["indoor museums", "shopping", "restaurants"] }
-
Recommendation Generation (LLM):
- Combines weather analysis with user preferences
- Creates day-by-day activity recommendations
- Formats as Markdown document
-
Storage (S3 via use_aws tool):
- Saves recommendations to S3 bucket as
results.md - User can download and review recommendations
- Saves recommendations to S3 bucket as
The agent runs asynchronously to handle long-running tasks:
- Immediate response: "Processing started..."
- Background processing: Completes all steps
- Results available in S3 after ~2-3 minutes
cdk destroy# Step 1: Empty the S3 bucket (required before deletion)
BUCKET_NAME=$(aws cloudformation describe-stacks \
--stack-name WeatherAgentDemo \
--region us-east-1 \
--query 'Stacks[0].Outputs[?OutputKey==`ResultsBucketName`].OutputValue' \
--output text)
aws s3 rm s3://$BUCKET_NAME --recursive
# Step 2: Terminate any active browser sessions
# Get the Browser ID
BROWSER_ID=$(aws cloudformation describe-stacks \
--stack-name WeatherAgentDemo \
--region us-east-1 \
--query 'Stacks[0].Outputs[?OutputKey==`BrowserId`].OutputValue' \
--output text)
# List active sessions
aws bedrock-agentcore list-browser-sessions \
--browser-id $BROWSER_ID \
--region us-east-1
# Terminate each active session (replace SESSION_ID with actual session ID from list command)
# Repeat this command for each active session
aws bedrock-agentcore terminate-browser-session \
--browser-id $BROWSER_ID \
--session-id SESSION_ID \
--region us-east-1
# Step 3: Delete the stack
aws cloudformation delete-stack \
--stack-name WeatherAgentDemo \
--region us-east-1
# Wait for deletion to complete
aws cloudformation wait stack-delete-complete \
--stack-name WeatherAgentDemo \
--region us-east-1Important: Browser sessions are automatically created when the agent uses the browser tool. Always terminate active sessions before deleting the stack to avoid deletion failures.
- Navigate to S3 Console
- Find the bucket (name format:
<stack-name>-results-<account-id>) - Empty the bucket
- Navigate to Bedrock AgentCore Console
- Go to "Browsers" in the left navigation
- Find your browser (name starts with
WeatherAgentDemo_browser) - Click on the browser name
- In the "Sessions" tab, terminate any active sessions
- Navigate to CloudFormation Console
- Select the
WeatherAgentDemostack - Click "Delete"
- Confirm deletion
If you see bootstrap errors:
cdk bootstrap aws://ACCOUNT-NUMBER/REGIONEnsure your IAM user/role has:
CDKToolkitpermissions or equivalent- Permissions to create all resources in the stack
iam:PassRolefor service roles
Install dependencies in the project directory:
pip install -r requirements.txtCheck CodeBuild logs in the AWS Console:
- Go to CodeBuild console
- Find the build project (name contains "weather-agent-build")
- Check build history and logs
If deployment fails due to browser sessions:
- List active sessions using AWS CLI
- Terminate all active sessions
- Retry deployment or cleanup
If memory initialization fails:
- Check Lambda function logs in CloudWatch
- Verify IAM permissions for memory access
- Retry deployment