简体   繁体   English

Boto3 在本地承担角色,因为 ECS 任务将用于测试目的

[英]Boto3 Assume Role locally as ECS task will Do for testing purposes

I am currently working on the development of a flask application that will be deployed in ECS using boto3 and python.我目前正在开发将使用 boto3 和 python 部署在 ECS 中的 Flask 应用程序。

Currently, for the development of it, I have a python env where I program the different functionalities and interact with the boto3 API assuming roles using the profiles in the .aws / credentials file:目前,为了开发它,我有一个 python 环境,我在其中编写不同的功能并与 boto3 API 交互,使用 .aws / 凭证文件中的配置文件承担角色:

iam = boto3.Session (profile_name = "account_alias", region_name = 'eu-west-1'). client ('iam')

However, to deploy it in the ECS I have to change all these sessions to something like this so that it is assumed by the role of the task:但是,要在 ECS 中部署它,我必须将所有这些会话更改为这样的内容,以便由任务的角色承担:

try:
    sts_client = boto3.client ('sts')
    assumed_role_object = sts_client.assume_role (
        RoleArn = "arn: aws: iam ::" + str (Account) + ": role /" + str (Role)
except Exception as e:
    logging.exception ("Could NOT assume role in account:% s", Account)

credentials = assumed_role_object ['Credentials']

boto3_session = boto3.session.Session (
    aws_access_key_id = credentials ['AccessKeyId'],
    aws_secret_access_key = credentials ['SecretAccessKey'],
    aws_session_token = credentials ['SessionToken'],
)

This makes it unable to test it locally (impossible to assume) and leads to numerous errors when deploying it (creating the ECR without testing it).这使得它无法在本地测试它(不可能假设)并在部署它时导致许多错误(创建 ECR 而不测试它)。

Is there a way to assume roles without profiles locally in the same way that the task will in the ECS?有没有办法以与 ECS 中的任务相同的方式在本地承担没有配置文件的角色?

Thank you.谢谢你。

I recommend you make a consistently-named role that will exist in all the target accounts.我建议您创建一个名称一致的角色,该角色将存在于所有目标帐户中。 Make the trust policy of that role such that your own credentials can assume it (access key and secret that you are using for your default profile).制定该角色的信任策略,以便您自己的凭据可以承担它(您用于默认配置文件的访问密钥和机密)。 Then you can use the same logic to assume a role during your local development and not have to change anything.然后,您可以使用相同的逻辑在本地开发期间承担角色,而无需更改任何内容。 Essentially, instead of using local profiles with different credentials, you would use a single credential that has permission to assume a role in every target account.本质上,不是使用具有不同凭据的本地配置文件,而是使用一个有权在每个目标帐户中承担角色的凭据。 I've worked in an environment with the same problem, and doing this allows me to have the same local code that I deploy.我曾在一个有同样问题的环境中工作过,这样做可以让我拥有与部署相同的本地代码。

There's no reason your local code can't also assume a role, and if there are concerns about making a role that you can assume in all those target accounts just consider that by using different profiles (with, I assume, different credentials) you must already have access to do the things you would be doing by assuming the role.您的本地代码没有理由也不能担任角色,如果担心在所有这些目标帐户中扮演可以担任的角色,请考虑使用不同的配置文件(我假设,具有不同的凭据),您必须已经可以通过担任该角色来执行您将要做的事情。 If the role has more permissions than you should have, then make a new role so you assume different roles locally and in AWS.如果该角色的权限超出您应有的权限,则创建一个新角色,以便您在本地和 AWS 中担任不同的角色。 But at least that will allow you to use the same code--just with a different role name variable passed in.但至少这将允许您使用相同的代码——只是传入不同的角色名称变量。

You'll either need to make ECR usage use profiles or make localhost usage assume roles if you want the code to be the same, and I believe it is easier to make localhost usage assume roles than to get your ECR tasks set up to use profiles.如果您希望代码相同,您要么需要让 ECR 使用使用配置文件,要么让 localhost 使用承担角色,我相信让 localhost 使用承担角色比让 ECR 任务设置为使用配置更容易.

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM