-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_create_gateway.py
More file actions
146 lines (124 loc) · 4.44 KB
/
Copy path11_create_gateway.py
File metadata and controls
146 lines (124 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
"""
Script to create AgentCore Gateway.
Prerequisites:
- cognito_config.json (from Cognito setup)
- gateway_role_config.json (from IAM role setup)
"""
import json
import boto3
import sys
print("=" * 80)
print("AGENTCORE GATEWAY CREATION")
print("=" * 80)
print()
try:
# Step 1: Load configuration files
print("Step 1: Loading configuration files...")
print("-" * 80)
try:
with open('cognito_config.json') as f:
cognito_config = json.load(f)
print("✓ Loaded cognito_config.json")
except FileNotFoundError:
print("❌ Error: cognito_config.json not found")
print(" Run 08_create_cognito.py first")
sys.exit(1)
try:
with open('gateway_role_config.json') as f:
role_config = json.load(f)
print("✓ Loaded gateway_role_config.json")
except FileNotFoundError:
print("❌ Error: gateway_role_config.json not found")
print(" Run 09_create_gateway_role.py first")
sys.exit(1)
print()
# Step 2: Initialize AgentCore control plane client
print("Step 2: Initializing AgentCore client...")
print("-" * 80)
gateway_client = boto3.client("bedrock-agentcore-control", region_name='us-west-2')
print("✓ AgentCore control plane client initialized")
print()
# Step 3: Build authentication configuration
print("Step 3: Building authentication configuration...")
print("-" * 80)
auth_config = {
"customJWTAuthorizer": {
"allowedClients": [cognito_config["client_id"]],
"discoveryUrl": cognito_config["discovery_url"]
}
}
print(f"✓ Auth config created:")
print(f" Client ID: {cognito_config['client_id']}")
print(f" Discovery URL: {cognito_config['discovery_url']}")
print()
# Step 4: Create gateway
print("Step 4: Creating AgentCore Gateway...")
print("-" * 80)
print("⏳ This may take 30-60 seconds...")
create_response = gateway_client.create_gateway(
name="ReturnsRefundsGateway",
roleArn=role_config["role_arn"],
protocolType="MCP",
authorizerType="CUSTOM_JWT",
authorizerConfiguration=auth_config,
description="Gateway for returns and refunds agent with order lookup capabilities"
)
# Extract gateway details
gateway_id = create_response["gatewayId"]
gateway_url = create_response["gatewayUrl"]
gateway_arn = create_response["gatewayArn"]
print(f"✓ Gateway created successfully!")
print(f" Gateway ID: {gateway_id}")
print(f" Gateway URL: {gateway_url}")
print(f" Gateway ARN: {gateway_arn}")
print()
# Step 5: Save gateway configuration
print("Step 5: Saving configuration to gateway_config.json...")
print("-" * 80)
config = {
"id": gateway_id,
"gateway_id": gateway_id,
"gateway_url": gateway_url,
"gateway_arn": gateway_arn,
"name": "ReturnsRefundsGateway",
"region": "us-west-2",
"role_arn": role_config["role_arn"],
"cognito_client_id": cognito_config["client_id"],
"cognito_discovery_url": cognito_config["discovery_url"]
}
with open('gateway_config.json', 'w') as f:
json.dump(config, f, indent=2)
print("✓ Configuration saved to gateway_config.json")
print()
# Summary
print("=" * 80)
print("✅ GATEWAY CREATION COMPLETE!")
print("=" * 80)
print("\nGateway Details:")
print(f" Name: ReturnsRefundsGateway")
print(f" ID: {gateway_id}")
print(f" URL: {gateway_url}")
print(f" ARN: {gateway_arn}")
print()
print("Configuration:")
print(f" Protocol: MCP")
print(f" Authentication: OAuth 2.0 (Cognito)")
print(f" Execution Role: {role_config['role_arn']}")
print()
print("What This Gateway Does:")
print(" ✓ Provides secure MCP endpoint for agents")
print(" ✓ Validates OAuth tokens from Cognito")
print(" ✓ Translates MCP requests to Lambda calls")
print(" ✓ Manages authentication and authorization")
print()
print("Next Steps:")
print(" 1. Add Lambda function as a Gateway target")
print(" 2. Gateway will expose Lambda as an MCP tool")
print(" 3. Agents can discover and use the tool")
print("=" * 80)
except Exception as e:
print(f"\n❌ Error creating gateway: {e}")
import traceback
traceback.print_exc()
sys.exit(1)