

# 跨帳戶記憶體存取
<a name="memory-cross-account-access"></a>

Amazon Bedrock AgentCore 記憶體支援跨帳戶存取，可讓您建置多帳戶架構，其中記憶體資源和取用代理程式跨越多個 AWS 帳戶。跨帳戶存取涵蓋兩個案例：
+  **來自另一個帳戶的資料平面操作** — 帳戶 B 中的 主體可以針對帳戶 A 中的記憶體資源呼叫記憶體資料平面 APIs （建立事件、寫入記錄、擷取記錄）。這是透過將資源型政策連接至記憶體資源來設定。

  高階步驟：

  1. 透過連接以資源為基礎的政策，將記憶體資源設定為允許跨帳戶存取。

  1. 參考來自帳戶 B 的資料平面 API 呼叫中的記憶體 ARN。
+  **另一個帳戶中的交付目的地** — 帳戶 A 中的記憶體資源可以將承載和串流事件交付至帳戶 B 中的 Amazon S3 儲存貯體、Amazon SNS 主題和 Amazon Kinesis Data Streams。這在記憶體建立時間透過目標資源上的記憶體執行角色和資源政策進行設定。

  高階步驟：

  1. 在帳戶 A 中建立具有存取目標資源許可的記憶體執行角色。

  1. 將資源型政策新增至帳戶 B 中的目的地資源，以允許執行角色。

  1. 在帳戶 A 中建立記憶體，參考執行角色和跨帳戶資源 ARNs。

**Topics**
+ [先決條件](#memory-cross-account-access-prerequisites)
+ [跨帳戶資料平面存取](#memory-cross-account-access-data-plane)
+ [跨帳戶交付目的地](#memory-cross-account-access-delivery-destinations)
+ [最佳實務](#memory-cross-account-access-best-practices)

## 先決條件
<a name="memory-cross-account-access-prerequisites"></a>

在設定跨帳戶記憶體存取之前，請確認您有：
+ 在資源擁有者帳戶中建立的記憶體資源 （帳戶 A)
+ 記憶體資源的完整 ARN （例如 `arn:aws:bedrock-agentcore:us-east-1:<account-id>:memory/<memory-id>`)
+ 對於資料平面存取：帳戶 B 中的 IAM 角色或使用者，具有允許所需`bedrock-agentcore`動作的身分型許可
+ 對於交付目的地：在帳戶 B 中建立的目標 S3 儲存貯體、SNS 主題或 Kinesis Data Stream

## 跨帳戶資料平面存取
<a name="memory-cross-account-access-data-plane"></a>

您可以允許另一個帳戶中的主體直接針對您的記憶體資源呼叫記憶體資料平面 APIs。這是透過使用 `PutResourcePolicy` API 將資源型政策連接至記憶體來設定。如需資源型政策的詳細資訊，請參閱 [Amazon Bedrock AgentCore 的資源型政策](resource-based-policies.md)。

### 運作方式
<a name="memory-cross-account-access-data-plane-how-it-works"></a>

1. 帳戶 A 會建立記憶體資源。

1. 帳戶 A 使用 `PutResourcePolicy` API 將資源型政策連接至記憶體資源，將特定動作授予帳戶 B 中的委託人。

1. 帳戶 B 中的主體呼叫記憶體資料平面 APIs，將帳戶 A 中記憶體資源的完整 ARN 指定為 `memory-id`。

1.  AWS 會評估記憶體上的資源型政策，以及連接至帳戶 B 主體的身分型政策。如果兩者都允許 動作 （且沒有政策明確拒絕），則請求會成功。

### 支援的動作
<a name="memory-cross-account-access-supported-actions"></a>

您可以授予任何記憶體資料平面動作的跨帳戶存取權。下表列出可用的動作：


| Action | 說明 | 
| --- | --- | 
|  `bedrock-agentcore:CreateEvent`  | 建立短期記憶體事件 | 
|  `bedrock-agentcore:GetEvent`  | 擷取特定事件 | 
|  `bedrock-agentcore:DeleteEvent`  | 刪除特定事件 | 
|  `bedrock-agentcore:ListEvents`  | 列出工作階段中的事件 | 
|  `bedrock-agentcore:ListActors`  | 列出記憶體中的演員 | 
|  `bedrock-agentcore:ListSessions`  | 列出演員的工作階段 | 
|  `bedrock-agentcore:GetMemoryRecord`  | 擷取特定記憶體記錄 | 
|  `bedrock-agentcore:ListMemoryRecords`  | 列出命名空間中的記憶體記錄 | 
|  `bedrock-agentcore:RetrieveMemoryRecords`  | 以語意搜尋記憶體記錄 | 
|  `bedrock-agentcore:DeleteMemoryRecord`  | 刪除特定記憶體記錄 | 
|  `bedrock-agentcore:BatchCreateMemoryRecords`  | 建立多個記憶體記錄 | 
|  `bedrock-agentcore:BatchUpdateMemoryRecords`  | 更新多個記憶體記錄 | 
|  `bedrock-agentcore:BatchDeleteMemoryRecords`  | 刪除多個記憶體記錄 | 
|  `bedrock-agentcore:ListMemoryExtractionJobs`  | 列出記憶體的擷取任務 | 
|  `bedrock-agentcore:StartMemoryExtractionJobs`  | 重新啟動失敗的擷取任務 | 

### 將資源型政策連接至記憶體
<a name="memory-cross-account-access-attach-policy"></a>

#### 將單一動作授予另一個帳戶
<a name="memory-cross-account-access-grant-single-action"></a>

下列範例授予帳戶 B (`<account-B-id>`) 許可，以`BatchCreateMemoryRecords`呼叫帳戶 A () 中的記憶體資源`<account-A-id>`：

```
aws bedrock-agentcore-control put-resource-policy \
    --region us-east-1 \
    --resource-arn "arn:aws:bedrock-agentcore:us-east-1:<account-A-id>:memory/<memory-id>" \
    --policy '{
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "AllowCrossAccountBatchCreate",
                "Effect": "Allow",
                "Principal": {
                    "AWS": "arn:aws:iam::<account-B-id>:root"
                },
                "Action": "bedrock-agentcore:BatchCreateMemoryRecords",
                "Resource": "arn:aws:bedrock-agentcore:us-east-1:<account-A-id>:memory/<memory-id>"
            }
        ]
    }'
```

#### 將多個動作授予另一個帳戶
<a name="memory-cross-account-access-grant-multiple-actions"></a>

下列範例會授予帳戶 B 對記憶體記錄和事件的完整讀取和寫入存取權：

```
aws bedrock-agentcore-control put-resource-policy \
    --region us-east-1 \
    --resource-arn "arn:aws:bedrock-agentcore:us-east-1:<account-A-id>:memory/<memory-id>" \
    --policy '{
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "AllowCrossAccountMemoryReadWrite",
                "Effect": "Allow",
                "Principal": {
                    "AWS": "arn:aws:iam::<account-B-id>:root"
                },
                "Action": [
                    "bedrock-agentcore:CreateEvent",
                    "bedrock-agentcore:GetEvent",
                    "bedrock-agentcore:ListEvents",
                    "bedrock-agentcore:ListActors",
                    "bedrock-agentcore:ListSessions",
                    "bedrock-agentcore:BatchCreateMemoryRecords",
                    "bedrock-agentcore:BatchUpdateMemoryRecords",
                    "bedrock-agentcore:BatchDeleteMemoryRecords",
                    "bedrock-agentcore:ListMemoryRecords",
                    "bedrock-agentcore:RetrieveMemoryRecords",
                    "bedrock-agentcore:GetMemoryRecord",
                    "bedrock-agentcore:DeleteMemoryRecord"
                ],
                "Resource": "arn:aws:bedrock-agentcore:us-east-1:<account-A-id>:memory/<memory-id>"
            }
        ]
    }'
```

#### 授予特定 IAM 角色的存取權
<a name="memory-cross-account-access-grant-specific-role"></a>

若要遵循最低權限原則，請將存取權授予特定角色，而非整個帳戶：

```
aws bedrock-agentcore-control put-resource-policy \
    --region us-east-1 \
    --resource-arn "arn:aws:bedrock-agentcore:us-east-1:<account-A-id>:memory/<memory-id>" \
    --policy '{
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "AllowSpecificRoleReadAccess",
                "Effect": "Allow",
                "Principal": {
                    "AWS": "arn:aws:iam::<account-B-id>:role/AgentMemoryReaderRole"
                },
                "Action": [
                    "bedrock-agentcore:RetrieveMemoryRecords",
                    "bedrock-agentcore:ListMemoryRecords",
                    "bedrock-agentcore:GetMemoryRecord"
                ],
                "Resource": "arn:aws:bedrock-agentcore:us-east-1:<account-A-id>:memory/<memory-id>"
            }
        ]
    }'
```

### 移除以資源為基礎的政策
<a name="memory-cross-account-access-remove-policy"></a>

若要撤銷跨帳戶資料平面存取，請刪除資源型政策：

```
aws bedrock-agentcore-control delete-resource-policy \
    --region us-east-1 \
    --resource-arn "arn:aws:bedrock-agentcore:us-east-1:<account-A-id>:memory/<memory-id>"
```

刪除政策後，任何後續的跨帳戶請求都會傳回 `AccessDeniedException`。

### 從帳戶 B 呼叫資料平面 APIs
<a name="memory-cross-account-access-calling-from-account-b"></a>

連接資源型政策後，帳戶 B 中的主體可以透過將完整記憶體 ARN 指定為 來呼叫資料平面 APIs`memory-id`。

從帳戶 B 建立記憶體記錄：

```
aws bedrock-agentcore batch-create-memory-records \
    --region us-east-1 \
    --memory-id "arn:aws:bedrock-agentcore:us-east-1:<account-A-id>:memory/<memory-id>" \
    --records '[
      {
        "requestIdentifier": "rec-001",
        "content": {"text": "User prefers morning meeting times before 10 AM"},
        "namespaces": ["preferences/user-123"],
        "timestamp": "1729525989"
      }
    ]'
```

從帳戶 B 擷取記憶體記錄：

```
aws bedrock-agentcore retrieve-memory-records \
    --region us-east-1 \
    --memory-id "arn:aws:bedrock-agentcore:us-east-1:<account-A-id>:memory/<memory-id>" \
    --namespace "preferences/user-123" \
    --search-criteria '{"searchQuery": "meeting preferences"}' \
    --max-results 10
```

從帳戶 B 建立事件：

```
aws bedrock-agentcore create-event \
    --region us-east-1 \
    --memory-id "arn:aws:bedrock-agentcore:us-east-1:<account-A-id>:memory/<memory-id>" \
    --actor-id "agent-in-account-b" \
    --session-id "cross-account-session-1" \
    --event-timestamp "$(date -u +"%Y-%m-%dT%H:%M:%S.%3NZ")" \
    --payload '[
      {
        "conversational": {
          "content": {"text": "Schedule my standup for 9 AM tomorrow"},
          "role": "USER"
        }
      }
    ]'
```

## 跨帳戶交付目的地
<a name="memory-cross-account-access-delivery-destinations"></a>

當您使用自訂 （自我管理） 策略或串流交付組態建立記憶體時，Amazon Bedrock AgentCore 會使用記憶體執行角色將承載交付至目的地資源。當這些資源位於不同的帳戶中時，您必須設定兩端：帳戶 A 中的執行角色需要存取資源的許可，而帳戶 B 中的資源需要允許從帳戶 A 存取的政策。

### 運作方式
<a name="memory-cross-account-access-delivery-how-it-works"></a>

1. 您可以使用允許帳戶 A 中記憶體執行角色的資源型政策，在帳戶 B 中建立目的地資源 (S3 儲存貯體、SNS 主題或 Kinesis Data Stream)。

1. 您可以在帳戶 A 中建立記憶體執行角色，其中包含 Amazon Bedrock AgentCore 的信任政策，以及授予帳戶 B 中跨帳戶資源存取權的許可政策。

1. 您可以在帳戶 A 中建立記憶體，參考執行角色和跨帳戶資源 ARNs。

1. Amazon Bedrock AgentCore 會擔任執行角色，並使用它將承載或串流事件交付至帳戶 B 中的資源。

### 設定記憶體執行角色
<a name="memory-cross-account-access-execution-role"></a>

在帳戶 A 中建立記憶體執行角色。該角色需要允許 Amazon Bedrock AgentCore 採用的信任政策，以及授予帳戶 B 中目標資源存取權的許可政策。

信任政策：

```
{
"Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "bedrock-agentcore.amazonaws.com"
      },
      "Action": "sts:AssumeRole",
      "Condition": {
        "ArnLike": {
          "aws:SourceArn": "arn:aws:bedrock-agentcore:<region>:<account-A-id>:memory/*"
        }
      }
    }
  ]
}
```

許可政策 （僅包含與您正在使用的資源相關的陳述式）：

```
{
"Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "AllowS3Access",
      "Effect": "Allow",
      "Action": [
        "s3:PutObject",
        "s3:GetObject"
      ],
      "Resource": "arn:aws:s3:::<bucket-name-in-account-B>/*"
    },
    {
      "Sid": "AllowSNSPublish",
      "Effect": "Allow",
      "Action": "sns:Publish",
      "Resource": "arn:aws:sns:<region>:<account-B-id>:<topic-name>"
    },
    {
      "Sid": "AllowKinesisAccess",
      "Effect": "Allow",
      "Action": [
        "kinesis:PutRecords",
        "kinesis:DescribeStream"
      ],
      "Resource": "arn:aws:kinesis:<region>:<account-B-id>:stream/<stream-name>"
    }
  ]
}
```

### 在帳戶 B 中設定 S3 儲存貯體政策
<a name="memory-cross-account-access-s3-policy"></a>

將下列資源型政策新增至帳戶 B 中的 S3 儲存貯體，以允許帳戶 A 中的記憶體執行角色提供承載：

```
{
"Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "AllowMemoryExecutionRoleAccess",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::<account-A-id>:role/<memory-execution-role-name>"
      },
      "Action": [
        "s3:PutObject",
        "s3:GetObject"
      ],
      "Resource": "arn:aws:s3:::<bucket-name>/*"
    }
  ]
}
```

### 在帳戶 B 中設定 SNS 主題政策
<a name="memory-cross-account-access-sns-policy"></a>

將下列資源型政策新增至帳戶 B 中的 SNS 主題，以允許帳戶 A 中的記憶體執行角色發佈通知：

```
{
"Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "AllowMemoryExecutionRolePublish",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::<account-A-id>:role/<memory-execution-role-name>"
      },
      "Action": "sns:Publish",
      "Resource": "arn:aws:sns:<region>:<account-B-id>:<topic-name>"
    }
  ]
}
```

### 在帳戶 B 中設定 Kinesis Data Stream 政策
<a name="memory-cross-account-access-kinesis-policy"></a>

將下列資源型政策新增至帳戶 B 中的 Kinesis Data Stream，以允許帳戶 A 中的記憶體執行角色串流事件：

```
{
"Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "AllowMemoryExecutionRolePutRecords",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::<account-A-id>:role/<memory-execution-role-name>"
      },
      "Action": [
        "kinesis:PutRecords",
        "kinesis:DescribeStream"
      ],
      "Resource": "arn:aws:kinesis:<region>:<account-B-id>:stream/<stream-name>"
    }
  ]
}
```

### 使用跨帳戶 S3 和 SNS 建立記憶體
<a name="memory-cross-account-access-create-memory-s3-sns"></a>

設定執行角色和資源政策後，在帳戶 A 中建立參考帳戶 B 中跨帳戶資源的記憶體：

```
aws bedrock-agentcore-control create-memory \
    --region us-east-1 \
    --name "cross-account-memory" \
    --description "Memory with cross-account S3 and SNS delivery" \
    --event-expiry-duration 30 \
    --memory-execution-role-arn "arn:aws:iam::<account-A-id>:role/MemoryCrossAccountRole" \
    --memory-strategies '[
      {
        "customMemoryStrategy": {
          "name": "cross_account_strategy",
          "configuration": {
            "selfManagedConfiguration": {
              "triggerConditions": [
                {"messageBasedTrigger": {"messageCount": 5}}
              ],
              "invocationConfiguration": {
                "topicArn": "arn:aws:sns:us-east-1:<account-B-id>:memory-notifications",
                "payloadDeliveryBucketName": "<bucket-name>"
              },
              "historicalContextWindowSize": 10
            }
          }
        }
      }
    ]'
```

### 使用跨帳戶 Kinesis 串流建立記憶體
<a name="memory-cross-account-access-create-memory-kinesis"></a>

```
aws bedrock-agentcore-control create-memory \
    --region us-east-1 \
    --name "cross-account-streaming-memory" \
    --description "Memory with cross-account Kinesis streaming" \
    --event-expiry-duration 30 \
    --memory-execution-role-arn "arn:aws:iam::<account-A-id>:role/MemoryCrossAccountRole" \
    --stream-delivery-resources '{
      "resources": [
        {
          "kinesis": {
            "dataStreamArn": "arn:aws:kinesis:us-east-1:<account-B-id>:stream/memory-record-stream",
            "contentConfigurations": [
              {
                "type": "MEMORY_RECORDS",
                "level": "FULL_CONTENT"
              }
            ]
          }
        }
      ]
    }'
```

## 最佳實務
<a name="memory-cross-account-access-best-practices"></a>
+  **授予最低權限** — 僅授予跨帳戶主體所需的特定動作。
+  **使用特定主體** — 將存取權 授予特定 IAM 角色，而不是整個帳戶根目錄，以限制爆量半徑。
+  **稽核跨帳戶存取** — 使用 AWS CloudTrail 監控記憶體資源的跨帳戶 API 呼叫。
+  **單獨的讀取和寫入存取** — 為唯讀消費者和讀寫生產者 建立單獨的政策陳述式。
+  **在移除政策之前** 驗證 — 在移除以資源為基礎的政策 之前，請確認其他帳戶中沒有作用中的工作負載依賴存取。