简体   繁体   English

通过角色连接到 2 个不同 AWS 账户的资源

[英]Connect to resources of 2 different AWS accounts via roles

Consider 2 AWS accounts A ( Other team ) and B ( Mine ) For my use case, I have to poll to queues in A's AWS account for payload and perform database operations in B. To do this, I have a role created in B having access to my databases + A's account ID as trusted relationship.考虑 2 个 AWS 账户 A(其他团队)和 B(我的)对于我的用例,我必须轮询 A 的 AWS 账户中的队列以获取有效负载并在 B 中执行数据库操作。为此,我在 B 中创建了一个角色,具有访问我的数据库 + A 的帐户 ID 作为信任关系。 Likewise A has created a role for granting access to queue in A + added B as trusted relationship.同样,A 创建了一个角色,用于授予对 A 中队列的访问权限 + 添加 B 作为受信任关系。

Code for doing all the polling + database action will reside on EC2 in my account ( B ).执行所有轮询 + 数据库操作的代码将驻留在我帐户 ( B ) 中的 EC2 上。 Now how do I consume payloads and perform operations from a role's perspective.现在我如何从角色的角度使用有效负载并执行操作。 Is my understanding correct?我的理解正确吗?

  1. Assume role A承担角色A
  2. Poll to provisioning queue, get the JSON payload from SQS轮询到配置队列,从 SQS 获取 JSON 负载
  3. Assume role B承担角色 B
  4. Perform database operations in Oracle RDS在 Oracle RDS 中执行数据库操作
  5. Assume role A承担角色A
  6. Return back the response to response SQS返回对响应 SQS 的响应
  7. Start polling again on provisioning queue SQS在配置队列 SQS 上再次开始轮询

FYI : I am performing the above operations using Python + Boto3仅供参考:我正在使用 Python + Boto3 执行上述操作

You don't say which programming language or SDK you are using, but essentially you can create two client/service objects, one leveraging credentials from role A and the other from role B. Then simply make API calls using the appropriate client/service object.您没有说明您使用的是哪种编程语言或 SDK,但基本上您可以创建两个客户端/服务对象,一个利用来自角色 A 的凭据,另一个利用来自角色 B 的凭据。然后只需使用适当的客户端/服务对象进行 API 调用.

Using boto3, for example:使用 boto3,例如:

sqs_accounta = boto3.client(
    'sqs',
    region_name='us-east-1',
    aws_access_key_id=xxx,
    aws_secret_access_key=yyy,
    aws_session_token=zzz
)

rds_accountb = boto3.client(
    'rds',
    region_name='us-west-2',
    aws_access_key_id=aaa,
    aws_secret_access_key=bbb,
    aws_session_token=ccc
)

Instead of assuming roles, you can grant Cross-Account access to the queues in Account A to the user/role in Account B.您可以向账户 B 中的用户/角色授予对账户 A 中队列的跨账户访问权限,而不是承担角色。

In your case, grant it to EC2 instance role and then you'll be able to do all the required operations from within the instance without any "Assume role" tricks.在您的情况下,将其授予 EC2 实例角色,然后您将能够从实例内执行所有必需的操作,而无需任何“承担角色”技巧。

Pretend for a moment that everything was happening in your own account (Account-B).假装一切都发生在您自己的帐户(帐户-B)中。

You would give a set of credentials to your code (either an IAM User or, if the code is running on an Amazon EC2 instance, you would assign an IAM Role to the instance) that it can use to access the necessary resources in Account B. So, no problem there.您将为您的代码提供一组凭证(IAM 用户,或者,如果代码在 Amazon EC2 实例上运行,您将为该实例分配一个 IAM 角色),它可用于访问账户 B 中的必要资源. 所以,没有问题。

The only additional requirement is that you wish to access Amazon SQS in Account-A.唯一的附加要求是您希望在账户 A 中访问 Amazon SQS。 It so happens that you can add permissions directly to an Amazon SQS queue that grants cross-account access.碰巧的是,您可以直接向授予跨账户访问权限的 Amazon SQS 队列添加权限

See: Basic Examples of IAM Policies for Amazon SQS - Amazon Simple Queue Service请参阅: Amazon SQS 的 IAM 策略的基本示例 - Amazon Simple Queue Service

So, you do not actually need to assume any roles .因此,您实际上不需要承担任何角色 Just use the normal credentials that are assigned to your code, and add permissions to the SQS queue to allow that particular IAM User or IAM Role to use the queue.只需使用分配给您的代码的普通凭证,并向 SQS 队列添加权限以允许该特定 IAM 用户或 IAM 角色使用该队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS CDK:如何将资源部署到不同的账户? - AWS CDK: how to deploy resources to different accounts? 是否可以使用AWS假设角色从帐户中的不同区域访问AWS资源 - Is it possible to access aws resources from different region across accounts using aws assume-role 设计一项 AWS 服务,用于监控和控制多个不同 AWS 账户中的资源 - Design of one AWS service that monitors & controls resources in several different AWS accounts 通过 AWS PrivateLink 从不同的 AWS 账户访问 AWS ElastiCache(Redis CLUSTER 模式) - Accessing AWS ElastiCache (Redis CLUSTER mode) from different AWS accounts via AWS PrivateLink 允许多个AWS账户查看资源 - Allow multiple AWS accounts to view resources 连接不同 AWS 账户中的 AWS Lambda 和 RDS - Connecting AWS Lambda and RDS in different AWS Accounts 如何以编程方式在 AWS 组织成员账户中创建角色 - How to programmatically create roles in AWS organisation member accounts 是否可以为多个 AWS 账户中的资源创建 AWS 私有链接? - Is it possible to create an AWS Private Link for resources in multiple AWS accounts? 连接AWS ACL,但“找不到资源”吗? - Connect AWS ACL but “no resources found”? 如何使用顶级账户中的 AWS Single Sign On 配置来自不同账户的多个 AWS Connect 实例? - How do I configure multiple AWS Connect instances from different accounts with AWS Single Sign On in a top level account?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM