简体   繁体   中英

How to detach an IAM role into AWS running instance and attach a new IAM role into that particular instance using java

I am creating an Amazon EC2 instance with a specific IAM Role.

Now I want to detach that IAM role and attach a new IAM Role or replace with a new IAM Role. I am finding the options to do with CLI but I am not able to find any option when the instance is running through java code.

Here is my code:

RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId(imageID)
        .withBlockDeviceMappings(blockDeviceMappings).withInstanceType(type).withMinCount(1).withMaxCount(1)
                .withIamInstanceProfile(new IamInstanceProfileSpecification().withName(iamRole))
                .withUserData(getUserDataScript())
                .withNetworkInterfaces(new InstanceNetworkInterfaceSpecification().withAssociatePublicIpAddress(true)
                        .withDeviceIndex(0).withSubnetId(subnetID).withGroups(groups));
        RunInstancesResult runInstancesResult = ec2Client.runInstances(runInstancesRequest);

To change the IAM Role associate with an Amazon EC2 instance, first disassociate the existing one:

disassociateIamInstanceProfile()

The, attach a new one:

associateIamInstanceProfile()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM