aws与IAM角色交叉帐户dynamodb访问

问题描述 投票:0回答:1

我在一个帐户中有一个aws ecs ec2实例,它试图访问另一个aws帐户上的dynamob数据库表。我没有使用任何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表?

amazon-dynamodb aws-sdk amazon-iam amazon-ecs aws-sdk-net
1个回答
0
投票

https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2.html

两个帐户都需要ec2的角色策略,以及允许EC2服务承担这些角色的信任策略。目标帐户中的角色策略将赋予Dynamodb表IAM权限。

然后,源EC2实例必须假定该角色才能访问该表。

授予EC2 Server访问权以承担该角色

{
    "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"
        }
      ]
    }

角色授予对名为ACCESS_DYNAMODB的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"}
        }
    ]
}
© www.soinside.com 2019 - 2024. All rights reserved.