

# 创建 AgentCore 代码解释器
<a name="code-interpreter-create"></a>

您可以使用 Amazon Bedrock AgentCore 控制台、 AWS CLI 或 AWS SDK 创建代码解释器。

**Example**  

1. ====== 使用控制台创建代码解释器

1. 在 [https://console.aws.amazon.com/bedrock-agentcore/home\#](https://console.aws.amazon.com/bedrock-agentcore/home#) 处打开 AgentCore 控制台。

1. 在导航窗格中，选择**Built-in 工具**。

1. 选择 “**创建代码解释器” 工具**。

1. 提供唯一的**工具名称**和可选的**描述**。

1. 在 **“网络设置”** 下，选择以下选项之一：
   +  **沙盒**-外部网络访问受限的环境
   +  **公共网络**-允许访问公共互联网资源

1. 在 “**权限”** 下，指定一个 IAM 运行时角色，该角色定义了代码解释器可以访问的 AWS 资源。

1. 选择**创建**。

   创建代码解释器工具后，控制台会显示有关该工具的重要细节：
<Region>唯一标识代码解释器工具资源的亚马逊资源名称 (ARN)（例如，arn: aws: bedrock-agentcore:: 123456789012: code-inperter-custom）。interpreter/code
代码解释器工具的唯一标识符，用于 API 调用（例如，code-interpreter-custom-abc123）。
代码解释器在执行代码时扮演的 IAM 角色，决定它可以访问哪些 AWS 资源。
代码解释器（沙盒或公共）的网络配置。
创建代码解释器工具的日期和时间。

1. 要使用 AWS CLI 创建代码解释器，请使用以下`create-code-interpreter`命令：

   ```
   aws bedrock-agentcore create-code-interpreter \
     --region <Region> \
     --name "my-code-interpreter" \
     --description "My Code Interpreter for data analysis" \
     --network-configuration '{
       "networkMode": "PUBLIC"
     }' \
     --execution-role-arn "arn:aws:iam::123456789012:role/my-execution-role"
   ```

1. 要使用适用于 Python 的 AWS 软件开发工具包创建代码解释器，请使用以下`create_code_interpreter`方法：

   ```
   import boto3
   
   # Initialize the boto3 client
   cp_client = boto3.client(
       'bedrock-agentcore-control',
       region_name="<Region>",
       endpoint_url="https://bedrock-agentcore-control.<Region>.amazonaws.com"
   )
   
   # Create a Code Interpreter
   response = cp_client.create_code_interpreter(
       name="myTestSandbox1",
       description="Test code sandbox for development",
       executionRoleArn="arn:aws:iam::123456789012:role/my-execution-role",
       networkConfiguration={
           "networkMode": "PUBLIC"
       }
   )
   
   # Print the Code Interpreter ID
   code_interpreter_id = response["codeInterpreterId"]
   print(f"Code Interpreter ID: {code_interpreter_id}")
   ```

1. 要使用 API 创建新的代码解释器实例，请使用以下调用：

   ```
   # Using awscurl
   awscurl -X PUT "https://bedrock-agentcore-control.<Region>.amazonaws.com/code-interpreters" \
   -H "Content-Type: application/json" \
   --region <Region> \
   --service bedrock-agentcore \
   -d '{
       "name": "codeinterpreter'$(date +%m%d%H%M%S)'",
       "description": "Test code sandbox for development",
       "executionRoleArn": "'${ROLE_ARN}'",
       "networkConfiguration": {
           "networkMode": "PUBLIC"
       }
   }'
   ```