摩托车不嘲笑iam create_role

我正在尝试测试一些使用boto3的python代码,并使用moto对AWS进行测试。 因此,据我所知有两个问题,

  1. 第1期

找不到moto帐户ID。根据他们的github,accountid直接由

from moto.core import ACCOUNT_ID

but for me it gives this error: cannot import name 'ACCOUNT_ID' from 'moto.core'

  1. 第2期

因此,我对帐号进行了硬编码(googled),希望它能起到作用。现在,我有不同的错误,

 instance_role = iam.create_role(
AttributeError: 'function' object has no attribute 'create_role'

can anyone help to assist me fixing this issue. so far: so no idea why boto3.client for iam throws no create_role ( I know i'm doing print, but that's not the reason for the error, this is just to see what is happening)

import os
import pytest
from moto import (mock_cloudformation,
                  mock_sts,
                  mock_iam)


iam_role_name = "local_new_user"

trust_policy_document = {
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Principal": {
            "AWS": "arn:aws:iam::{account_id}:root".format(account_id=123456789012)
        },
        "Action": "sts:AssumeRole",
    },
}


@pytest.fixture
def aws_credentials():
    """Mocked AWS Credentials for moto."""
    os.environ['AWS_ACCESS_KEY_ID'] = 'testing'
    os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing'
    os.environ['AWS_SECURITY_TOKEN'] = 'testing'
    os.environ['AWS_SESSION_TOKEN'] = 'testing'
    os.environ['RELEASE_ENVIRONMENTNAME'] = 'local'



@pytest.fixture
def iam_test(aws_credentials):
    with mock_iam():
        yield boto3.client('iam', region_name='eu-west-1')





def test_iam_assume_role(iam_test):
    resp = iam_test.create_role(
        RoleName=iam_role_name,
        AssumeRolePolicyDocument=json.dumps(trust_policy_document),
        Description="test"
    )
    print(resp)



if __name__ == '__main__':
    test_iam_assume_role(iam_test)
评论