How can I modify the instance name for an ec2 instance

Travis Leleu picture Travis Leleu · Jun 2, 2011 · Viewed 25.1k times · Source

I would like to modify the "name" attribute of an amazon instance. See attached screenshot. I need to do it programmatically, but can't find anywhere in the EC2 API how to set that.

If it matters, I'm launching these via a spot request through their API. I would like to set the field that I tagged, "set this name" in the image below.

screen shot of field to set

Answer

Kevin Mansel picture Kevin Mansel · Jun 9, 2011

This might help...

AmazonEC2 ec2;    
AWSCredentials credentials;
String accKey = "your access key";
String secKey = "your secret key";    

credentials = new BasicAWSCredentials(accKey, secKey);
ec2 = new AmazonEC2Client(credentials);

String instanceId = "Your Instance ID";
List<Tag> tags = new ArrayList<Tag>();

Tag t = new Tag();
t.setKey("Name");
t.setValue("my server!");
tags.add(t);

Tag t = new Tag();
t.setKey("owner");
t.setValue("me");
tags.add(t);

CreateTagsRequest ctr = new CreateTagsRequest();
ctr.setTags(tags);
ctr.withResources(instanceId);
ec2.createTags(ctr);

kind of quick and dirty, but you get the idea.