1
votes

I just started to read about AWS and am trying to create an ec2 instance and attach a role to it. As far as i have read i need to create an instance profile, attach a role to it and then attach it to EC2 instance but i am unable to do so because of the errors described below. I dont have enough time left, so any help will be highly appreciated.

Here is my code:

Creating a role:

role = iam.create_role(
                Path='/',
                RoleName=self.roleName,
                AssumeRolePolicyDocument= str1,
                Description="Allow EC2 instances to call AWS services"
            )
        #roleArn=role["Role"]["Arn"]
        response = iam.attach_role_policy(
            RoleName=self.roleName, PolicyArn='arn:aws:iam::aws:policy/AdministratorAccess')

        response = iam.attach_role_policy(
            RoleName=self.roleName,
            PolicyArn='arn:aws:iam::aws:policy/AmazonS3FullAccess'
        )

Creating an instance profile and attaching role:

instance_profile = iam.create_instance_profile(InstanceProfileName=self.instanceProfile,Path='/')
iam.add_role_to_instance_profile(InstanceProfileName=self.instanceProfile, RoleName=self.roleName)

Creating EC2 instance:

instance = ec2.create_instances(
                ImageId=imageId,
                MinCount=1,
                MaxCount=1,
                KeyName=keyName,
                InstanceType=instanceType,
                IamInstanceProfile={
                        'Name': instanceProfile
                 }) code here
            )

I get the following error while running the above code:

An error occurred (InvalidParameterValue) when calling the RunInstances operation: Value (mnbvinst) for parameter iamInstanceProfile.name is invalid. Invalid IAM Instance Profile name

And as soon as I re run the application and try to provide the same name for the instance, I get the following error.

An error occurred (LimitExceeded) when calling the AddRoleToInstanceProfile operation: Cannot exceed quota for InstanceSessionsPerInstanceProfile: 1

I have tried using the run_instances method for creating EC2 instance but the same error persists

Variables:

iam = boto3.client('iam')
ec2 = boto3.resource('ec2')
client = boto.client('ec2')
2
you are not passing the correct "instanceProfile" name. i guess it should be InstanceProfileNameSudharsan Sivasankaran
@SudharsanSivasankaran instanceProfile variable actually contains the name of the instance profile.Suman Sourav Singh
maybe it not a vaid name. can you check in UI if the instance profie got created?Sudharsan Sivasankaran

2 Answers

0
votes

Your code has:

IamInstanceProfile={
    'Name': instanceProfile
}

However, there is no variable called instanceProfile.

You also seem to be mixing instanceProfile and instance_profile:

instance_profile = iam.create_instance_profile(InstanceProfileName=self.instanceProfile,Path='/')
iam.add_role_to_instance_profile(InstanceProfileName=self.instanceProfile, RoleName=self.roleName)

The first line creates instance_profile, but the second line refers to self.instanceProfile.

You've got some cleaning-up to do.

0
votes

Is this instance profile being used in multiple resources? IAM instance profiles restricts the number of simultaneous credential sessions.

You may have too many EC2 resources coming up with the same instance profile.

You can either create a instance profile and use that or request AWS for a limit increase here: https://console.aws.amazon.com/support/home#/case/create?issueType=service-limit-increase&limitType=service-code-iam-groups-and-users