I am trying to create an IAM role and instance profile, and then associate the instance profile with an EC2 instance, using the AWS Java SDK. I am running into an issue where the IAM role and instance profile are successfully created (I have checked this by waiting on the instance profile using AmazonIdentityManagementWaiters.instanceProfileExists()
), but when I try to associate the instance profile with the instance using Ec2Client.replaceIamInstanceProfileAssociation
, I consistently get an EC2 exception:
software.amazon.awssdk.services.ec2.model.Ec2Exception: Value (ROLE-NAME-BLAH) for parameter iamInstanceProfile.name is invalid. Invalid IAM Instance Profile name (Service: Ec2, Status Code: 400, Request ID: BLAH)
If I hold my debugger for ~30 seconds before the call to Ec2Client.replaceIamInstanceProfileAssociation
, the call goes through successfully. This seems to be a race condition but I'm not sure how to resolve it, since there are no waiters available for an instance profile from the Ec2Client
side. Any thoughts on how to resolve this / alternative approaches? Thanks!