我有两个AWS账户:
DEV: 111111111111
PROD: 999999999999
我在名为prodRepo
的prod帐户中创建了一个代码提交仓库。
我想要做的是允许DEV
和PROD
帐户上的ec2实例拥有对此repo的只读权限。所以git clone
,git pull
等等......
我可以使用名为PROD
的以下IAM实例配置文件在我的codecommit-tester
帐户上轻松完成此操作
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"codecommit:BatchGetRepositories",
"codecommit:Get*",
"codecommit:GitPull",
"codecommit:List*"
],
"Resource": "arn:aws:codecommit:us-east-1:999999999999:prodRepo"
}
]
}
信托关系政策是:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
}
然后我使用git config中的aws凭证帮助程序来执行只读git操作,而不必在我的机器上存储凭据(它从实例元数据中获取代码提交的凭据)。
$ cat ~/.gitconfig
[credential]
helper = !aws codecommit credential-helper $@
UseHttpPath = true
我遇到的问题是在DEV
帐户上创建IAM政策/角色,以执行与PROD
帐户相同的操作。这是我试过的。
我在PROD
帐户上编辑了信任关系以信任DEV帐户:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
},
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::111111111111:root"
},
"Action": "sts:AssumeRole"
}
}
现在我认为这意味着DEV
帐户可以承担此角色。在DEV
帐户上,我创建了附加到角色的这些IAM策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"codecommit:BatchGetRepositories",
"codecommit:Get*",
"codecommit:GitPull",
"codecommit:List*"
],
"Resource": "arn:aws:codecommit:us-east-1:999999999999:prodRepo"
}
]
}
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::999999999999:role/codecommit-tester"
}
}
在使用此IAM实例配置文件启动ec2实例后,我在DEV帐户上使用凭证助手,执行git clone
时出现此错误:
$ git clone https://git-codecommit.us-east-1.amazonaws.com/v1/repos/prodRepo
Cloning into 'prodRepo'...
fatal: unable to access 'https://git-codecommit.us-east-1.amazonaws.com/v1/repos/prodRepo/': The requested URL returned error: 403
那么我在DEV
的IAM角色/政策中错过了什么才能使其发挥作用?
答案 0 :(得分:1)
我认为你不需要你提到的 dev 中的iam角色(在DEV帐户上我创建了附加到角色的这些IAM策略)....没有尝试使用实例交叉帐户承担角色.. 但是如果您可以使用
在prod帐户中创建新的IAM角色{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::999999999999:role/codecommit-tester"
}
]
}
和信任关系就像是
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::111111111111:root"
},
"Action": "sts:AssumeRole"
}
并假设在dev ec2角色中使用新的IAM ARN。