bedrock-agentcore-starter-toolkit 0.0.1__py3-none-any.whl → 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of bedrock-agentcore-starter-toolkit might be problematic. Click here for more details.

Files changed (50) hide show
  1. bedrock_agentcore_starter_toolkit/__init__.py +5 -0
  2. bedrock_agentcore_starter_toolkit/cli/cli.py +32 -0
  3. bedrock_agentcore_starter_toolkit/cli/common.py +44 -0
  4. bedrock_agentcore_starter_toolkit/cli/gateway/__init__.py +1 -0
  5. bedrock_agentcore_starter_toolkit/cli/gateway/commands.py +88 -0
  6. bedrock_agentcore_starter_toolkit/cli/runtime/__init__.py +1 -0
  7. bedrock_agentcore_starter_toolkit/cli/runtime/commands.py +651 -0
  8. bedrock_agentcore_starter_toolkit/cli/runtime/configuration_manager.py +133 -0
  9. bedrock_agentcore_starter_toolkit/notebook/__init__.py +5 -0
  10. bedrock_agentcore_starter_toolkit/notebook/runtime/__init__.py +1 -0
  11. bedrock_agentcore_starter_toolkit/notebook/runtime/bedrock_agentcore.py +239 -0
  12. bedrock_agentcore_starter_toolkit/operations/__init__.py +1 -0
  13. bedrock_agentcore_starter_toolkit/operations/gateway/README.md +277 -0
  14. bedrock_agentcore_starter_toolkit/operations/gateway/__init__.py +6 -0
  15. bedrock_agentcore_starter_toolkit/operations/gateway/client.py +456 -0
  16. bedrock_agentcore_starter_toolkit/operations/gateway/constants.py +152 -0
  17. bedrock_agentcore_starter_toolkit/operations/gateway/create_lambda.py +85 -0
  18. bedrock_agentcore_starter_toolkit/operations/gateway/create_role.py +90 -0
  19. bedrock_agentcore_starter_toolkit/operations/gateway/exceptions.py +13 -0
  20. bedrock_agentcore_starter_toolkit/operations/runtime/__init__.py +26 -0
  21. bedrock_agentcore_starter_toolkit/operations/runtime/configure.py +241 -0
  22. bedrock_agentcore_starter_toolkit/operations/runtime/create_role.py +404 -0
  23. bedrock_agentcore_starter_toolkit/operations/runtime/invoke.py +129 -0
  24. bedrock_agentcore_starter_toolkit/operations/runtime/launch.py +439 -0
  25. bedrock_agentcore_starter_toolkit/operations/runtime/models.py +79 -0
  26. bedrock_agentcore_starter_toolkit/operations/runtime/status.py +66 -0
  27. bedrock_agentcore_starter_toolkit/services/codebuild.py +332 -0
  28. bedrock_agentcore_starter_toolkit/services/ecr.py +84 -0
  29. bedrock_agentcore_starter_toolkit/services/runtime.py +473 -0
  30. bedrock_agentcore_starter_toolkit/utils/endpoints.py +32 -0
  31. bedrock_agentcore_starter_toolkit/utils/logging_config.py +72 -0
  32. bedrock_agentcore_starter_toolkit/utils/runtime/config.py +129 -0
  33. bedrock_agentcore_starter_toolkit/utils/runtime/container.py +310 -0
  34. bedrock_agentcore_starter_toolkit/utils/runtime/entrypoint.py +197 -0
  35. bedrock_agentcore_starter_toolkit/utils/runtime/logs.py +33 -0
  36. bedrock_agentcore_starter_toolkit/utils/runtime/policy_template.py +74 -0
  37. bedrock_agentcore_starter_toolkit/utils/runtime/schema.py +151 -0
  38. bedrock_agentcore_starter_toolkit/utils/runtime/templates/Dockerfile.j2 +44 -0
  39. bedrock_agentcore_starter_toolkit/utils/runtime/templates/dockerignore.template +68 -0
  40. bedrock_agentcore_starter_toolkit/utils/runtime/templates/execution_role_policy.json.j2 +98 -0
  41. bedrock_agentcore_starter_toolkit/utils/runtime/templates/execution_role_trust_policy.json.j2 +21 -0
  42. bedrock_agentcore_starter_toolkit-0.1.1.dist-info/METADATA +137 -0
  43. bedrock_agentcore_starter_toolkit-0.1.1.dist-info/RECORD +47 -0
  44. bedrock_agentcore_starter_toolkit-0.1.1.dist-info/entry_points.txt +2 -0
  45. bedrock_agentcore_starter_toolkit-0.1.1.dist-info/licenses/NOTICE.txt +190 -0
  46. bedrock_agentcore_starter_toolkit/init.py +0 -3
  47. bedrock_agentcore_starter_toolkit-0.0.1.dist-info/METADATA +0 -26
  48. bedrock_agentcore_starter_toolkit-0.0.1.dist-info/RECORD +0 -5
  49. {bedrock_agentcore_starter_toolkit-0.0.1.dist-info → bedrock_agentcore_starter_toolkit-0.1.1.dist-info}/WHEEL +0 -0
  50. /bedrock_agentcore_starter_toolkit-0.0.1.dist-info/licenses/LICENSE → /bedrock_agentcore_starter_toolkit-0.1.1.dist-info/licenses/LICENSE.txt +0 -0
@@ -0,0 +1,90 @@
1
+ """Creates an execution role to use in the Bedrock AgentCore Gateway module."""
2
+
3
+ import json
4
+ import logging
5
+ from typing import Optional
6
+
7
+ from boto3 import Session
8
+ from botocore.client import BaseClient
9
+ from botocore.exceptions import ClientError
10
+
11
+ from ...operations.gateway.constants import (
12
+ BEDROCK_AGENTCORE_TRUST_POLICY,
13
+ POLICIES,
14
+ POLICIES_TO_CREATE,
15
+ )
16
+
17
+
18
+ def create_gateway_execution_role(
19
+ session: Session, logger: logging.Logger, role_name: str = "AgentCoreGatewayExecutionRole"
20
+ ) -> str:
21
+ """Create the Gateway execution role.
22
+
23
+ :param logger: the logger to use.
24
+ :return: the role ARN.
25
+ """
26
+ iam = session.client("iam")
27
+ # Create the role
28
+ try:
29
+ role = iam.create_role(
30
+ RoleName=role_name,
31
+ AssumeRolePolicyDocument=json.dumps(BEDROCK_AGENTCORE_TRUST_POLICY),
32
+ Description="Execution role for AgentCore Gateway",
33
+ )
34
+ for policy_name, policy in POLICIES_TO_CREATE:
35
+ _attach_policy(
36
+ iam_client=iam,
37
+ role_name=role_name,
38
+ policy_name=policy_name,
39
+ policy_document=json.dumps(policy),
40
+ )
41
+ for policy_arn in POLICIES:
42
+ _attach_policy(iam_client=iam, role_name=role_name, policy_arn=policy_arn)
43
+
44
+ return role["Role"]["Arn"]
45
+
46
+ except ClientError as e:
47
+ if e.response["Error"]["Code"] == "EntityAlreadyExists":
48
+ try:
49
+ role = iam.get_role(RoleName=role_name)
50
+ logger.info("✓ Role already exists: %s", role["Role"]["Arn"])
51
+ return role["Role"]["Arn"]
52
+ except ClientError as get_error:
53
+ logger.error("Error getting existing role: %s", get_error)
54
+ raise
55
+ else:
56
+ logger.error("Error creating role: %s", e)
57
+ raise
58
+
59
+
60
+ def _attach_policy(
61
+ iam_client: BaseClient,
62
+ role_name: str,
63
+ policy_arn: Optional[str] = None,
64
+ policy_document: Optional[str] = None,
65
+ policy_name: Optional[str] = None,
66
+ ) -> None:
67
+ """Attach a policy to an IAM role.
68
+
69
+ :param iam_client: the IAM client to use.
70
+ :param role_name: name of the role.
71
+ :param policy_arn: the arn of the policy to attach.
72
+ :param policy_document: the policy document (if not using a policy_arn).
73
+ :param policy_name: the policy name (if not using a policy_arn).
74
+ :return:
75
+ """
76
+ if policy_arn and policy_document:
77
+ raise Exception("Cannot specify both policy arn and policy document.")
78
+ try:
79
+ if policy_arn:
80
+ iam_client.attach_role_policy(RoleName=role_name, PolicyArn=policy_arn)
81
+ elif policy_document and policy_name:
82
+ policy = iam_client.create_policy(
83
+ PolicyName=policy_name,
84
+ PolicyDocument=policy_document,
85
+ )
86
+ iam_client.attach_role_policy(RoleName=role_name, PolicyArn=policy["Policy"]["Arn"])
87
+ else:
88
+ raise Exception("Must specify both policy document and policy name or just a policy arn")
89
+ except ClientError as e:
90
+ raise RuntimeError(f"Failed to attach AgentCore policy: {e}") from e
@@ -0,0 +1,13 @@
1
+ """Exceptions for the Bedrock AgentCore Gateway module."""
2
+
3
+
4
+ class GatewayException(Exception):
5
+ """Base exception for all Gateway SDK errors."""
6
+
7
+ pass
8
+
9
+
10
+ class GatewaySetupException(GatewayException):
11
+ """Raised when gateway or Cognito setup fails."""
12
+
13
+ pass
@@ -0,0 +1,26 @@
1
+ """Bedrock AgentCore operations - shared business logic for CLI and notebook interfaces."""
2
+
3
+ from .configure import configure_bedrock_agentcore, validate_agent_name
4
+ from .invoke import invoke_bedrock_agentcore
5
+ from .launch import launch_bedrock_agentcore
6
+ from .models import (
7
+ ConfigureResult,
8
+ InvokeResult,
9
+ LaunchResult,
10
+ StatusConfigInfo,
11
+ StatusResult,
12
+ )
13
+ from .status import get_status
14
+
15
+ __all__ = [
16
+ "configure_bedrock_agentcore",
17
+ "validate_agent_name",
18
+ "launch_bedrock_agentcore",
19
+ "invoke_bedrock_agentcore",
20
+ "get_status",
21
+ "ConfigureResult",
22
+ "InvokeResult",
23
+ "LaunchResult",
24
+ "StatusResult",
25
+ "StatusConfigInfo",
26
+ ]
@@ -0,0 +1,241 @@
1
+ """Configure operation - creates BedrockAgentCore configuration and Dockerfile."""
2
+
3
+ import logging
4
+ import re
5
+ from pathlib import Path
6
+ from typing import Any, Dict, Optional, Tuple
7
+
8
+ from ...services.ecr import get_account_id, get_region
9
+ from ...utils.runtime.config import merge_agent_config, save_config
10
+ from ...utils.runtime.container import ContainerRuntime
11
+ from ...utils.runtime.schema import (
12
+ AWSConfig,
13
+ BedrockAgentCoreAgentSchema,
14
+ BedrockAgentCoreDeploymentInfo,
15
+ NetworkConfiguration,
16
+ ObservabilityConfig,
17
+ ProtocolConfiguration,
18
+ )
19
+ from .models import ConfigureResult
20
+
21
+ log = logging.getLogger(__name__)
22
+
23
+
24
+ def configure_bedrock_agentcore(
25
+ agent_name: str,
26
+ entrypoint_path: Path,
27
+ execution_role: Optional[str] = None,
28
+ ecr_repository: Optional[str] = None,
29
+ container_runtime: Optional[str] = None,
30
+ auto_create_ecr: bool = True,
31
+ auto_create_execution_role: bool = True,
32
+ enable_observability: bool = True,
33
+ requirements_file: Optional[str] = None,
34
+ authorizer_configuration: Optional[Dict[str, Any]] = None,
35
+ verbose: bool = False,
36
+ region: Optional[str] = None,
37
+ protocol: Optional[str] = None,
38
+ ) -> ConfigureResult:
39
+ """Configure Bedrock AgentCore application with deployment settings.
40
+
41
+ Args:
42
+ agent_name: name of the agent,
43
+ entrypoint_path: Path to the entrypoint file
44
+ execution_role: AWS execution role ARN or name (auto-created if not provided)
45
+ ecr_repository: ECR repository URI
46
+ container_runtime: Container runtime to use
47
+ auto_create_ecr: Whether to auto-create ECR repository
48
+ auto_create_execution_role: Whether to auto-create execution role if not provided
49
+ enable_observability: Whether to enable observability
50
+ requirements_file: Path to requirements file
51
+ authorizer_configuration: JWT authorizer configuration dictionary
52
+ verbose: Whether to provide verbose output during configuration
53
+ region: AWS region for deployment
54
+ protocol: agent server protocol, must be either HTTP or MCP
55
+
56
+ Returns:
57
+ ConfigureResult model with configuration details
58
+ """
59
+ # Set logging level based on verbose flag
60
+ if verbose:
61
+ log.setLevel(logging.DEBUG)
62
+ log.debug("Verbose mode enabled")
63
+ else:
64
+ log.setLevel(logging.INFO)
65
+ # Log agent name at the start of configuration
66
+ log.info("Configuring BedrockAgentCore agent: %s", agent_name)
67
+ build_dir = Path.cwd()
68
+
69
+ if verbose:
70
+ log.debug("Build directory: %s", build_dir)
71
+ log.debug("Bedrock AgentCore name: %s", agent_name)
72
+ log.debug("Entrypoint path: %s", entrypoint_path)
73
+
74
+ # Get AWS info
75
+ if verbose:
76
+ log.debug("Retrieving AWS account information...")
77
+ account_id = get_account_id()
78
+ region = region or get_region()
79
+
80
+ if verbose:
81
+ log.debug("AWS account ID: %s", account_id)
82
+ log.debug("AWS region: %s", region)
83
+
84
+ # Initialize container runtime
85
+ if verbose:
86
+ log.debug("Initializing container runtime with: %s", container_runtime or "default")
87
+ runtime = ContainerRuntime(container_runtime)
88
+
89
+ # Handle execution role - convert to ARN if provided, otherwise use auto-create setting
90
+ execution_role_arn = None
91
+ execution_role_auto_create = auto_create_execution_role
92
+
93
+ if execution_role:
94
+ # User provided a role - convert to ARN format if needed
95
+ if execution_role.startswith("arn:aws:iam::"):
96
+ execution_role_arn = execution_role
97
+ else:
98
+ execution_role_arn = f"arn:aws:iam::{account_id}:role/{execution_role}"
99
+
100
+ if verbose:
101
+ log.debug("Using execution role: %s", execution_role_arn)
102
+ else:
103
+ # No role provided - use auto_create_execution_role parameter
104
+ if verbose:
105
+ if execution_role_auto_create:
106
+ log.debug("Execution role will be auto-created during launch")
107
+ else:
108
+ log.debug("No execution role provided and auto-create disabled")
109
+
110
+ # Generate Dockerfile and .dockerignore
111
+ bedrock_agentcore_name = None
112
+ # Try to find the variable name for the Bedrock AgentCore instance in the file
113
+ if verbose:
114
+ log.debug("Attempting to find Bedrock AgentCore instance name in %s", entrypoint_path)
115
+
116
+ if verbose:
117
+ log.debug("Generating Dockerfile with parameters:")
118
+ log.debug(" Entrypoint: %s", entrypoint_path)
119
+ log.debug(" Build directory: %s", build_dir)
120
+ log.debug(" Bedrock AgentCore name: %s", bedrock_agentcore_name or "bedrock_agentcore")
121
+ log.debug(" Region: %s", region)
122
+ log.debug(" Enable observability: %s", enable_observability)
123
+ log.debug(" Requirements file: %s", requirements_file)
124
+
125
+ dockerfile_path = runtime.generate_dockerfile(
126
+ entrypoint_path,
127
+ build_dir,
128
+ bedrock_agentcore_name or "bedrock_agentcore",
129
+ region,
130
+ enable_observability,
131
+ requirements_file,
132
+ )
133
+
134
+ # Check if .dockerignore was created
135
+ dockerignore_path = build_dir / ".dockerignore"
136
+
137
+ log.info("Generated Dockerfile: %s", dockerfile_path)
138
+ if dockerignore_path.exists():
139
+ log.info("Generated .dockerignore: %s", dockerignore_path)
140
+
141
+ # Handle project configuration (named agents)
142
+ config_path = build_dir / ".bedrock_agentcore.yaml"
143
+
144
+ if verbose:
145
+ log.debug("Agent name from BedrockAgentCoreApp: %s", agent_name)
146
+ log.debug("Config path: %s", config_path)
147
+
148
+ # Determine entrypoint format
149
+ if bedrock_agentcore_name:
150
+ entrypoint = f"{str(entrypoint_path)}:{bedrock_agentcore_name}"
151
+ else:
152
+ entrypoint = str(entrypoint_path)
153
+
154
+ if verbose:
155
+ log.debug("Using entrypoint format: %s", entrypoint)
156
+
157
+ # Create new configuration
158
+ ecr_auto_create_value = bool(auto_create_ecr and not ecr_repository)
159
+
160
+ if verbose:
161
+ log.debug("ECR auto-create: %s", ecr_auto_create_value)
162
+
163
+ if verbose:
164
+ log.debug("Creating BedrockAgentCoreConfigSchema with following parameters:")
165
+ log.debug(" Name: %s", agent_name)
166
+ log.debug(" Entrypoint: %s", entrypoint)
167
+ log.debug(" Platform: %s", ContainerRuntime.DEFAULT_PLATFORM)
168
+ log.debug(" Container runtime: %s", runtime.runtime)
169
+ log.debug(" Execution role: %s", execution_role_arn)
170
+ ecr_repo_display = ecr_repository if ecr_repository else "Auto-create" if ecr_auto_create_value else "N/A"
171
+ log.debug(" ECR repository: %s", ecr_repo_display)
172
+ log.debug(" Enable observability: %s", enable_observability)
173
+
174
+ # Create new agent configuration
175
+ config = BedrockAgentCoreAgentSchema(
176
+ name=agent_name,
177
+ entrypoint=entrypoint,
178
+ platform=ContainerRuntime.DEFAULT_PLATFORM,
179
+ container_runtime=runtime.runtime,
180
+ aws=AWSConfig(
181
+ execution_role=execution_role_arn,
182
+ execution_role_auto_create=execution_role_auto_create,
183
+ account=account_id,
184
+ region=region,
185
+ ecr_repository=ecr_repository,
186
+ ecr_auto_create=ecr_auto_create_value,
187
+ network_configuration=NetworkConfiguration(network_mode="PUBLIC"),
188
+ protocol_configuration=ProtocolConfiguration(server_protocol=protocol or "HTTP"),
189
+ observability=ObservabilityConfig(enabled=enable_observability),
190
+ ),
191
+ bedrock_agentcore=BedrockAgentCoreDeploymentInfo(),
192
+ authorizer_configuration=authorizer_configuration,
193
+ )
194
+
195
+ # Use simplified config merging
196
+ project_config = merge_agent_config(config_path, agent_name, config)
197
+ save_config(project_config, config_path)
198
+
199
+ if verbose:
200
+ log.debug("Configuration saved with agent: %s", agent_name)
201
+
202
+ return ConfigureResult(
203
+ config_path=config_path,
204
+ dockerfile_path=dockerfile_path,
205
+ dockerignore_path=dockerignore_path if dockerignore_path.exists() else None,
206
+ runtime=runtime.get_name(),
207
+ region=region,
208
+ account_id=account_id,
209
+ execution_role=execution_role_arn,
210
+ ecr_repository=ecr_repository,
211
+ auto_create_ecr=auto_create_ecr and not ecr_repository,
212
+ )
213
+
214
+
215
+ AGENT_NAME_REGEX = r"^[a-zA-Z][a-zA-Z0-9_]{0,47}$"
216
+ AGENT_NAME_ERROR = (
217
+ "Invalid agent name. Must start with a letter, contain only letters/numbers/underscores, "
218
+ "and be 1-48 characters long."
219
+ )
220
+
221
+
222
+ def validate_agent_name(name: str) -> Tuple[bool, str]:
223
+ """Check if name matches the pattern [a-zA-Z][a-zA-Z0-9_]{0,47}.
224
+
225
+ This pattern requires:
226
+ - First character: letter (a-z or A-Z)
227
+ - Remaining 0-47 characters: letters, digits, or underscores
228
+ - Total maximum length: 48 characters
229
+
230
+ Args:
231
+ name: The string to validate
232
+
233
+ Returns:
234
+ bool: True if the string matches the pattern, False otherwise
235
+ """
236
+ match = bool(re.match(AGENT_NAME_REGEX, name))
237
+
238
+ if match:
239
+ return match, ""
240
+ else:
241
+ return match, AGENT_NAME_ERROR