amazon-web-services - 从账户 B 中的 lambda 函数访问账户 A 中的代码提交?
问题描述
用例
我在帐户 A 中有一个 codecommit REPO。我在帐户 B 中有我的 lambda。我需要从帐户 B 中的我的 lambda 访问 codecommit repo。
我做了什么
- 我
CrossAccountRole
在账户 A 中创建了一个名为 managed 的角色,codedcommitReadOnlypolicy
并为其分配了账户 B 的信任关系,以便账户 B 可以承担该角色。 在帐户 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": "*" } ] }
并将其附加到我的用户权限。
- 我还为 lambda 函数创建了一个基本执行角色并将
ASSUMEROLE
策略附加到它。
我确信一切正常。因为当我尝试从 AccountB 中的控制台切换角色时,我可以看到codecommit Repos
AccountA 中的角色。
但我仍然无法从 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 中搜索,因此是通过此错误。
感谢任何帮助谢谢
解决方案
对于 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;
}
};
希望对其他人有帮助
推荐阅读
- xml - xsd:扩展不起作用:src-resolve:无法将名称“BaseObj”解析为(n)“类型定义”组件
- actions-on-google - 获取注册设备 Google Homegraph
- view - 无法使用不同于创建视图的库名读取 SAS 视图
- python - Python script with executable permission says "Command Not Found"
- java - JavaFX creates Alert dialog which is too small
- python - 当我导入模块时,它运行整个 python 文件
- mysql - Clean multiple numbered mysql tables
- c# - 由于混合 AcroForm-XFA 问题,在 C# 中使用 itext7 进行的 AcroField 更改未提交/不可见
- html - Can not place 2 tables side-to-side
- python-3.x - 如何将聊天机器人问题及其答案添加到 pandas 数据框中,以便以后在提出相同问题时可以检索到它