首页 > 解决方案 > 从账户 B 中的 lambda 函数访问账户 A 中的代码提交?

问题描述

用例

我在帐户 A 中有一个 codecommit REPO。我在帐户 B 中有我的 lambda。我需要从帐户 B 中的我的 lambda 访问 codecommit repo。

我做了什么

  1. CrossAccountRole在账户 A 中创建了一个名为 managed 的​​角色,codedcommitReadOnlypolicy并为其分配了账户 B 的信任关系,以便账户 B 可以承担该角色。
  2. 在帐户 B 中,我创建了ASSUMEROLE这样的策略

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "VisualEditor0",
                "Effect": "Allow",
                "Action": [
                    "sts:AssumeRole",
                    "sts:GetFederationToken"
                ],
                "Resource": "arn:aws:iam::134952096518:role/CrossAccountRole"
            },
            {
                "Sid": "VisualEditor1",
                "Effect": "Allow",
                "Action": "sts:GetCallerIdentity",
                "Resource": "*"
            }
        ]
    }
    

    并将其附加到我的用户权限。

  3. 我还为 lambda 函数创建了一个基本执行角色并将ASSUMEROLE策略附加到它。

我确信一切正常。因为当我尝试从 AccountB 中的控制台切换角色时,我可以看到codecommit ReposAccountA 中的角色。

但我仍然无法从 accountB 中的 lambda 函数连接到 AccountA 代码提交存储库。

我的 lambda 函数在 accountB 中看起来像这样

const AWS = require('aws-sdk');
AWS.config.region = 'ap-south-1';
var codecommit = new AWS.CodeCommit({apiVersion: '2015-04-13'});
var params = {
  commitId: 'xxx', 
  repositoryName: 'new' 
};
  var sts = new AWS.STS();
  var sts_params = {
    RoleArn: "arn:aws:iam::AccountA:role/CrossAccountRole",
    RoleSessionName: "ThisCanBeAnyName"
  };
exports.handler = async (event) => {
    // TODO implement
try {
       const creds = await sts.assumeRole(sts_params).promise();
       console.log(creds);
    AWS.config.update({
      accessKeyId: creds.Credentials.AccessKeyId,
      secretAccessKey: creds.Credentials.SecretAccessKey,
      sessionToken: creds.Credentials.SessionToken
    });
   const response = await codecommit.getCommit(params).promise();
   const email = response.commit.committer.email;
    console.log(response);
  } catch (err) {
    console.log(err.message);
    throw err;
  }
};

我收到类似的错误new Repository not exist。由于它是在帐户 B 中搜索,因此是通过此错误。

感谢任何帮助谢谢

标签: amazon-web-servicesaws-lambdaaws-codecommit

解决方案


对于 IAM 角色来说,一切都很好。需要在我的 lambda 代码中进行一些更改。在我的 lambda 代码中,我需要将 传递temporary credentials给服务,这是codecommit我在上面的代码中没有做的。所以,这是工作样本

const AWS = require('aws-sdk');
AWS.config.region = 'ap-south-1';
var params = {
   commitId: 'abc',
  repositoryName: 'new' 
};
  var sts = new AWS.STS();
  var sts_params = {
    RoleArn: "arn:aws:iam::xx:role/CrossAccountRole",
    RoleSessionName: "crossaccountRole"
  };
exports.handler = async (event) => {
    // TODO implement
try {
      const creds = await sts.assumeRole(sts_params).promise();
      const accessparams2 = {
    accessKeyId: creds.Credentials.AccessKeyId,
    secretAccessKey: creds.Credentials.SecretAccessKey,
    sessionToken: creds.Credentials.SessionToken,
  };
   const codecommit = await new AWS.CodeCommit(accessparams2);
   const response = await codecommit.getCommit(params).promise();
    return response;
  } catch (err) {
    console.log(err.message);
    throw err;
  }
};

希望对其他人有帮助


推荐阅读