簡體   English   中英

如何在單個程序中使用兩個 AWS IAM 賬戶?

[英]How do I use two AWS IAM accounts in a single program?

我一直在嘗試在兩個不同帳戶的 DynamoDB 表之間批量傳輸一些數據,但我無法這樣做,因為我不能在同一程序中使用另一個帳戶,因為它只是默認為我在其中使用的主帳戶AWS CLI。

這是我訪問兩個不同 IAM 帳戶的代碼。

目的地_acc.js

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
const CONFIG = {
  region: "us-east-1",
  accessKeyId: "x",
  secretAccessKey: "y",
};
const dest = new DynamoDBClient(CONFIG);
export { dest };

source_acc.js

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
const CONFIG = {
  region: "us-east-1",
  accessKeyId: "x",
  secretAccessKey: "y",
};
const source = new DynamoDBClient(CONFIG);
export { source };

測試.js

export const scanTable = async () => {
  const params = {
    TableName: "table",
  };

  var scanResults = [];
  var items = [];
  do {
    items = await dest.send(new ScanCommand(params));
    items.Items.forEach((item) => {
      console.log(item);
      scanResults.push(item);
    });
    params.ExclusiveStartKey = items.LastEvaluatedKey;
  } while (typeof items.LastEvaluatedKey !== "undefined");

  return scanResults;
};

scanTable(); //Returns the data in the table of `source` account instead of the data in `dest` account.

DynamoDB 根據 IAM 角色選擇賬戶和區域。 您首先需要在允許您從第一個帳戶承擔的第二個帳戶中設置一個角色: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_common-scenarios_aws-accounts.html

之后,在您的代碼中,您需要調用 sts assumeRole 並根據該角色創建第二個客戶端: https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html

現在,當您承擔該角色並創建具有該角色的客戶端時,您可以訪問第二個帳戶中的 DynamoDB 表/其他資源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM