创建新角色时如何指定策略名称?

How do you specify a policy name when creating a new role?

我正在将一些 aws-iot java 代码转换为使用 boto3,但在创建新角色时卡住了。旧代码在为角色指定策略时指定策略名称,但我不知道如何在boto3中指定。这是 java 代码块(注意:withId(assumePolicyName)):

iamClient.createRole(new CreateRoleRequest()
                .withRoleName(role)
                .withAssumeRolePolicyDocument(new com.amazonaws.auth.policy.Policy()
                    .withId(assumePolicyName)
                    .withStatements(new Statement(Statement.Effect.Allow)
                        .withActions(() -> "sts:AssumeRole")
                        .withPrincipals(new Principal("Service", "iot.amazonaws.com")))
                    .toJson()
                )
            );

我不知道在哪里用 boto3 指定 assumePolicyName,这是我在 boto3 中的内容:

self.iamClient.create_role(RoleName=role_name, AssumeRolePolicyDocument={
            'Statement': [
                {
                    'Principal': {
                        'Service': ['iot.amazonaws.com']
                    },
                    'Effect': 'Allow',
                    'Action': ['sts:AssumeRole']
                },
            ]
        }

如何指定策略名称?

据我所知,IAM 无法将策略名称与 AssumedRolePolicyDocument 相关联。我不确定名称的用途是什么,因为每个角色只能有一个这样的策略,并且这些信任关系不能在角色之间共享。

我非常接近。我应该尝试几次 trial/errors。正确的解决方案是这个文档:

self.iamClient.create_role(RoleName=role_name, AssumeRolePolicyDocument={
                'Id': 'assume_role_id',
                'Statement': [
                {
                    'Principal': {
                        'Service': ['iot.amazonaws.com']
                    },
                    'Effect': 'Allow',
                    'Action': ['sts:AssumeRole']
                }
            ]
        }