31

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

Travis Leleu
  • 4,190
  • 2
  • 27
  • 33

4 Answers4

35

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.

Rich
  • 15,048
  • 2
  • 66
  • 119
Kevin Mansel
  • 2,351
  • 1
  • 16
  • 15
  • 11
    FYI you need "Name" (uppercase), instead of "name" to set that name. – Fluffy Oct 31 '13 at 08:44
  • 3
    You can also use the [aws command](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) like so: `aws ec2 create-tags --resources i-??????? --tag Key=Name,Value="MY INSTANCE NAME"` – zekel Jul 27 '17 at 14:58
18

You can do it through AWS console UI:

ec2

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 4
    This UI feature is so annoyingly not-obvious if you don't expect it. Instances come nameless by default, so good luck thinking "Oh, I should mouse over this blank space to add it there" – Weston Wedding Dec 03 '21 at 19:23
  • 3
    Number of developers at AWS: thousands. Number of UX specialists at AWS: 0 – hfogel Dec 22 '21 at 10:33
  • 1
    I had even gotten to the point of seeing that icon, but didn't realize it was supposed to indicate "edit"... – Xiong Chiamiov Apr 27 '22 at 22:16
  • Hilarious comments and same here. Truly hate to say it but for once I find a MS product much more user friendly. Azure is a MUCH more intuitive, friendly and usable UI. – mouselabs Mar 14 '23 at 23:43
13

In 2021 this can be done from the AWS console, by going to EC2 > Instances, clicking the instance ID, then in the bottom panel clicking on the Tags tab, and clicking Manage tags. From there, you can simply change the value of the Name tag then click Save to apply.

idmadj
  • 2,565
  • 2
  • 19
  • 23
4

Further digging into the API and I found what I was looking for.

These are known as tags. You can assign them to nearly any aws entity (some things are excepted, e.g., you can't add a tag to an elastic ip).

You can set keyname/keyvalue pairs through the API. Documentation is here: http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-CreateTags.html

Then you can filter results by these tags, or choose to display them in the web interface.

Travis Leleu
  • 4,190
  • 2
  • 27
  • 33