我在一个帐户中有一个AWS ECS EC2实例,它正在尝试访问另一个AWS帐户中的dynamob db表。我没有使用任何aws访问密钥和id,而是使用了附加到ec2实例的AWS iam角色。
这是一个.net项目,我的appsettings.Staging.json是这个。
{
"aws": {
"region": "ap-southeast-1"
},
"DynamoDbTables": {
"BenefitCategory": "stag_table1",
"Benefit": "stag_table2"
},
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
这是我附加到“ ecsInstanceRole”的内联策略
“ xxxxxxxxxxxxx” >>这是dynamodb表所在的aws帐户。
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"dynamodb:PutItem",
"dynamodb:DescribeTable",
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:Scan",
"dynamodb:Query",
"dynamodb:UpdateItem",
"dynamodb:DeleteTable",
"dynamodb:UpdateTable",
"dynamodb:GetRecords"
],
"Resource": [
"arn:aws:dynamodb:ap-southeast-1:xxxxxxxxxxx:table/stag_table1",
"arn:aws:dynamodb:ap-southeast-1:xxxxxxxxxxx:table/stag_table2",
]
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"dynamodb:ListGlobalTables",
"dynamodb:ListTables"
],
"Resource": "*"
}
]
}
在此设置中,api尝试连接到同一帐户中的表。我已在ecsInstanceRole角色仍然无法工作的情况下在受信任实体中添加了其他aws帐户。
aws sdk或aws ecs / ec2实例是否可以通过其他aws帐户自动找到dynamodb表?
答案 0 :(得分:0)
https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2.html
两个帐户都将需要ec2的角色策略,而信任策略则允许EC2服务承担这些角色。目标帐户中的角色策略将为Dynamodb表提供IAM权限。
然后,源EC2实例将必须承担该角色才能访问表。
授予EC2服务器访问权限以承担角色
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "abcdTrustPolicy",
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Principal": {"AWS": "arn:aws:iam::SOURCE_ACCOUNT_ID:role/NAME_A"}
}
]
}
允许NAME_A实例配置文件角色切换到另一个帐户中的角色
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowToAssumeCrossAccountRole",
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::DESTINATION_ACCOUNT_ID:role/ACCESS_DYNAMODB"
}
]
}
角色授予对Dynamodb名为ACCESS_DYNAMODB的访问权限
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowDDBActions",
"Effect": "Allow",
"Action": [
"dynamodb:*"
],
"Resource": "*"
}
]
}
目标中的信任策略
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DestinationTrustPolicy",
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Principal": {"Service": "ec2.amazonaws.com"}
}
]
}