I'm trying to mock the creation of a compute environment, which requires some other resources, namely an IAM instance profile and service role. However, when I create those IAM resources and then attempt to use them in the compute environment creation, things fail with:
<Message>Role arn:aws:iam::123456789012:instance-profile/InstanceProfile not found</Message>
The code is below:
@mock_batch
@mock_iam
def test_create_compute_environment(lims_objs):
client = boto3.client("batch")
iam = boto3.resource("iam")
service_role = iam.create_role(
RoleName="BatchServiceRole", AssumeRolePolicyDocument="AWSBatchServiceRole"
)
instance_profile = iam.create_instance_profile(
InstanceProfileName="InstanceProfile"
)
instance_profile.add_role(RoleName=service_role.name)
for elem in iam.instance_profiles.all():
print(elem, elem.arn)
for elem in iam.roles.all():
print(elem)
response = client.create_compute_environment(
computeEnvironmentName="compute_environment",
type="MANAGED",
state="ENABLED",
computeResources={
"type": "EC2",
"minvCpus": 0,
"maxvCpus": 256,
"desiredvCpus": 2,
"instanceTypes": ["optimal"],
"imageId": "test",
"subnets": [],
"securityGroupIds": [],
"ec2KeyPair": "",
"instanceRole": instance_profile.arn,
"tags": {},
},
serviceRole=service_role.arn,
)
In the test, I can see the prints for the IAM objects, so I know they are being created. Are these just not shared across moto mocks?
iam.InstanceProfile(name='InstanceProfile') arn:aws:iam::123456789012:instance-profile/InstanceProfile
iam.Role(name='BatchServiceRole')
I know this may not be the complete working example if we can get past the instance profile, but this is where it's stuck now.
Any insight is much appreciated. Thanks so much!