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')