Skip to content

Commit 7f6630e

Browse files
committed
Replace AgentCore toolkit with CDK, fix vulnerabilities, cleanup improvements
1 parent dea835f commit 7f6630e

File tree

23 files changed

+9058
-52566
lines changed

23 files changed

+9058
-52566
lines changed

README.md

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The numbers in the following flow correspond to an autonomous agent executing st
7474

7575
6. **Payment Authorization:** The agent calls `make_payment`. The tool verifies sufficient balance exists and sets `auth:true` in session storage. This marks the user's intent to proceed with payment but does not transfer funds
7676

77-
7. **Initial x402 Request:** The agent calls `generate_image` again. The tool finds auth: true and creates an x402 HTTP client using the CDP AgentKit wallet's dynamically exported private key. The client sends a POST request to Amazon API Gateway without an `PAYMENT-SIGNATURE` header.
77+
7. **Initial x402 Request:** The agent calls `generate_image` again. The tool finds `auth:true` and creates an x402 HTTP client using the CDP AgentKit wallet's dynamically exported private key. The client sends a POST request to Amazon API Gateway without an `PAYMENT-SIGNATURE` header.
7878

7979
8. **402 Payment Required:** AWS Lambda receives the request and returns `HTTP 402` with payment requirements. The response includes the USDC amount in wei, seller wallet address, USDC contract address, and `EIP-712` domain parameters (name: 'USDC', version: '2', chainId: 84532).
8080

@@ -150,9 +150,8 @@ Agentic maintains warm WebSocket connections, eliminating connection establishme
150150
4. AWS CLI configured with credentials
151151
5. USDC on Base Sepolia for testing (obtain from [Circle Faucet](https://faucet.circle.com/))
152152
6. WalletConnect Project ID from [Reown Cloud](https://cloud.reown.com)
153-
7. Python 3.11 or later (required for AgentCore CLI, installed in Step 5.3)
154-
8. CDP API credentials from [Coinbase Developer Platform](https://portal.cdp.coinbase.com)
155-
9. Bash shell (Windows users: use WSL or Git Bash)
153+
7. CDP API credentials from [Coinbase Developer Platform](https://portal.cdp.coinbase.com)
154+
8. Bash shell (Windows users: use WSL or Git Bash)
156155

157156
#### Clone Repository
158157

@@ -462,36 +461,30 @@ APP_ID=$(aws amplify list-apps --query "apps[?name=='ai-content-monetization'].a
462461
aws amplify delete-app --app-id $APP_ID
463462
```
464463

465-
**2. Delete AgentCore Runtime and Memory (if deployed):**
464+
**2. Delete Agentic CDK stack:**
466465

467466
```bash
468-
cd agentic
469-
source venv/bin/activate
470-
agentcore destroy
467+
cd agentic/cdk && cdk destroy --force && cd ../..
471468
```
472469

473-
**3. Delete ECR repository (if created):**
470+
**3. Delete Serverless CDK stack:**
474471

475472
```bash
476-
aws ecr delete-repository --repository-name bedrock-agentcore-agent --force
477-
```
478-
479-
**4. Delete Agentic CDK stack:**
480-
481-
```bash
482-
cd cdk && cdk destroy --force && cd ../..
473+
cd serverless && cdk destroy --force && cd ..
483474
```
484475

485-
**5. Delete Serverless CDK stack:**
476+
**4. Delete CloudWatch log groups:**
486477

487478
```bash
488-
cd serverless && cdk destroy --force && cd ..
479+
for prefix in "/aws/lambda/AiContent" "/aws/lambda/X402" "/aws/codebuild/X402" "/aws/bedrock-agentcore/runtimes/x402_payment_agent"; do
480+
aws logs describe-log-groups --log-group-name-prefix "$prefix" --query 'logGroups[*].logGroupName' --output text | xargs -n1 aws logs delete-log-group --log-group-name 2>/dev/null
481+
done
489482
```
490483

491-
**6. Clean up local files:**
484+
**5. Clean up local files:**
492485

493486
```bash
494-
rm -rf node_modules/ dist/ serverless/node_modules/ serverless/cdk.out/ serverless/outputs.json serverless/lib/*.js serverless/lib/*.d.ts serverless/bin/*.js serverless/bin/*.d.ts agentic/cdk/node_modules/ agentic/cdk/cdk.out/ agentic/venv/ agentic/.bedrock_agentcore/ agentic/.bedrock_agentcore.yaml
487+
rm -rf node_modules/ dist/ serverless/node_modules/ serverless/cdk.out/ serverless/outputs.json serverless/lib/*.js serverless/lib/*.d.ts serverless/bin/*.js serverless/bin/*.d.ts agentic/cdk/node_modules/ agentic/cdk/cdk.out/ agentic/lambda/node_modules/
495488
```
496489

497490
## Troubleshooting

agentic/.env-sample

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ USDC_CONTRACT=0x036CbD53842c5426634e7929541eC2318f3dCF7e
1313
# Seller Wallet (receives payments)
1414
SELLER_WALLET=your_seller_wallet_address
1515

16-
# Gateway URL (set after CDK deployment without trailing slash)
17-
GATEWAY_URL=https://your-api-gateway-url.execute-api.us-east-1.amazonaws.com
18-
1916
# AWS Region
2017
AWS_REGION=us-east-1
2118

22-
# AgentCore Memory (BEDROCK_AGENTCORE_MEMORY_ID automatically set after AgentCore Runtime deployment)
19+
# ============================================================================
20+
# AUTO-GENERATED BY CDK (do not set manually)
21+
# These values are automatically set after running 'cdk deploy' via agentic-export.sh
22+
# ============================================================================
23+
24+
# Gateway URL (set after CDK deployment without trailing slash)
25+
GATEWAY_URL=
2326

27+
# AgentCore Memory ID (automatically created by CDK)
28+
BEDROCK_AGENTCORE_MEMORY_ID=

agentic/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ The numbers in the following flow correspond to an autonomous agent executing st
3434

3535
6. **Payment Authorization:** The agent calls `make_payment`. The tool verifies sufficient balance exists and sets `auth:true` in session storage. This marks the user's intent to proceed with payment but does not transfer funds
3636

37-
7. **Initial x402 Request:** The agent calls `generate_image` again. The tool finds auth: true and creates an x402 HTTP client using the CDP AgentKit wallet's dynamically exported private key. The client sends a POST request to Amazon API Gateway without an `PAYMENT-SIGNATURE` header.
37+
7. **Initial x402 Request:** The agent calls `generate_image` again. The tool finds `auth:true` and creates an x402 HTTP client using the CDP AgentKit wallet's dynamically exported private key. The client sends a POST request to Amazon API Gateway without an `PAYMENT-SIGNATURE` header.
3838

3939
8. **402 Payment Required:** AWS Lambda receives the request and returns `HTTP 402` with payment requirements. The response includes the USDC amount in wei, seller wallet address, USDC contract address, and `EIP-712` domain parameters (name: 'USDC', version: '2', chainId: 84532).
4040

agentic/agentic-export.sh

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,75 @@
11
#!/bin/bash
22

3-
# Export CDK stack outputs to agentic .env file
3+
# Export CDK stack outputs to agentic .env file and root .env file
44
STACK_NAME="X402GatewayStack"
55
ENV_FILE=".env"
6+
ROOT_ENV_FILE="../.env"
67

78
echo "Fetching CDK outputs from $STACK_NAME..."
89

910
# Get stack outputs
1011
GATEWAY_URL=$(aws cloudformation describe-stacks --stack-name "$STACK_NAME" \
1112
--query "Stacks[0].Outputs[?OutputKey=='ApiUrl'].OutputValue" --output text)
1213

14+
MEMORY_ID=$(aws cloudformation describe-stacks --stack-name "$STACK_NAME" \
15+
--query "Stacks[0].Outputs[?OutputKey=='MemoryId'].OutputValue" --output text)
16+
17+
AGENT_RUNTIME_ARN=$(aws cloudformation describe-stacks --stack-name "$STACK_NAME" \
18+
--query "Stacks[0].Outputs[?OutputKey=='AgentRuntimeArn'].OutputValue" --output text)
19+
1320
if [ -z "$GATEWAY_URL" ] || [ "$GATEWAY_URL" == "None" ]; then
14-
echo "Error: Could not retrieve stack outputs. Ensure the stack is deployed."
21+
echo "Error: Could not retrieve GATEWAY_URL. Ensure the stack is deployed."
1522
exit 1
1623
fi
1724

18-
# Remove trailing slash
25+
# Remove trailing slash from GATEWAY_URL
1926
GATEWAY_URL="${GATEWAY_URL%/}"
2027

2128
echo "Retrieved outputs:"
2229
echo " GATEWAY_URL: $GATEWAY_URL"
30+
echo " BEDROCK_AGENTCORE_MEMORY_ID: $MEMORY_ID"
31+
echo " AGENT_RUNTIME_ARN: $AGENT_RUNTIME_ARN"
2332

2433
# Check if .env exists
2534
if [ ! -f "$ENV_FILE" ]; then
26-
echo "Error: $ENV_FILE not found. Run 'cp .env.sample .env' first."
35+
echo "Error: $ENV_FILE not found. Run 'cp .env-sample .env' first."
2736
exit 1
2837
fi
2938

30-
# Update .env file
31-
if grep -q "^GATEWAY_URL=" "$ENV_FILE"; then
32-
sed -i.bak "s|^GATEWAY_URL=.*|GATEWAY_URL=${GATEWAY_URL}|" "$ENV_FILE"
33-
else
34-
echo "GATEWAY_URL=${GATEWAY_URL}" >> "$ENV_FILE"
39+
# Update agentic/.env file
40+
update_env_var() {
41+
local file=$1
42+
local key=$2
43+
local value=$3
44+
45+
if grep -q "^${key}=" "$file" 2>/dev/null; then
46+
sed -i.bak "s|^${key}=.*|${key}=${value}|" "$file"
47+
else
48+
echo "${key}=${value}" >> "$file"
49+
fi
50+
}
51+
52+
# Update GATEWAY_URL in agentic/.env
53+
update_env_var "$ENV_FILE" "GATEWAY_URL" "$GATEWAY_URL"
54+
55+
# Update BEDROCK_AGENTCORE_MEMORY_ID in agentic/.env
56+
if [ -n "$MEMORY_ID" ] && [ "$MEMORY_ID" != "None" ]; then
57+
update_env_var "$ENV_FILE" "BEDROCK_AGENTCORE_MEMORY_ID" "$MEMORY_ID"
58+
fi
59+
60+
# Update AGENT_RUNTIME_ARN in root .env (for serverless stack)
61+
if [ -n "$AGENT_RUNTIME_ARN" ] && [ "$AGENT_RUNTIME_ARN" != "None" ]; then
62+
if [ -f "$ROOT_ENV_FILE" ]; then
63+
update_env_var "$ROOT_ENV_FILE" "AGENT_RUNTIME_ARN" "$AGENT_RUNTIME_ARN"
64+
echo ""
65+
echo "AGENT_RUNTIME_ARN exported to $ROOT_ENV_FILE"
66+
else
67+
echo "Warning: $ROOT_ENV_FILE not found. AGENT_RUNTIME_ARN not exported to root."
68+
fi
3569
fi
3670

37-
rm -f "${ENV_FILE}.bak"
71+
# Clean up backup files
72+
rm -f "${ENV_FILE}.bak" "${ROOT_ENV_FILE}.bak" 2>/dev/null
3873

3974
echo ""
40-
echo "GATEWAY_URL exported to $ENV_FILE"
75+
echo "GATEWAY_URL and BEDROCK_AGENTCORE_MEMORY_ID exported to $ENV_FILE"

agentic/cdk/cdk.json

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
{
2-
"app": "node stack.js"
2+
"app": "node stack.js",
3+
"watch": {
4+
"include": ["**"],
5+
"exclude": [
6+
"README.md",
7+
"cdk*.json",
8+
"node_modules",
9+
"**/*.d.ts",
10+
"**/*.js.map"
11+
]
12+
},
13+
"context": {
14+
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
15+
"@aws-cdk/core:checkSecretUsage": true,
16+
"@aws-cdk/core:target-partitions": ["aws", "aws-cn"],
17+
"@aws-cdk/aws-iam:minimizePolicies": true,
18+
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
19+
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
20+
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
21+
"@aws-cdk/core:enablePartitionLiterals": true,
22+
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
23+
"@aws-cdk/customresources:installLatestAwsSdkDefault": false
24+
}
325
}

agentic/cdk/package-lock.json

Lines changed: 17 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agentic/cdk/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
"version": "1.0.0",
44
"scripts": {
55
"deploy": "cdk deploy --require-approval never",
6-
"destroy": "cdk destroy"
6+
"destroy": "cdk destroy",
7+
"synth": "cdk synth"
78
},
89
"devDependencies": {
910
"esbuild": "^0.25.0"
1011
},
1112
"dependencies": {
12-
"aws-cdk-lib": "^2.100.0",
13+
"aws-cdk-lib": "^2.220.0",
1314
"constructs": "^10.0.0",
1415
"cdk-nag": "^2.28.0",
15-
"dotenv": "^16.4.5",
16-
"hono": "^4.0.0"
16+
"dotenv": "^16.4.5"
1717
}
1818
}

0 commit comments

Comments
 (0)