jonb
jonb

Reputation: 875

Set a tag to EC2 instance using JAVA

This is the last code I tried:

System.out.println("Manager not exists. Going to create.");
            // Basic 32-bit Amazon Linux AMI 1.0 (AMI Id: ami-08728661)
            RunInstancesRequest request = new RunInstancesRequest("ami-acd005d5", 1, 1);
            request.setInstanceType(InstanceType.T2Micro.toString());
            List<Instance> instances = ec2.runInstances(request).getReservation().getInstances();
            System.out.println("Launch instances: " + instances);
            Instance instance = instances.get(0);

            Collection<String> resources = new ArrayList();
            resources.add(instance.getInstanceId());
            Collection<Tag> tags = new ArrayList();
            tags.add(new Tag("Name", "Manager"));
            CreateTagsRequest createTagsRequest = new CreateTagsRequest();  
            createTagsRequest.setResourceId(instance.getInstanceId());
            createTagsRequest.setTags(tags);
            CreateTagsResult tagsRsults = createTags(createTagsRequest);

It doesn't compile on line createTagsRequest.setTags(tags):

The method setTags(java.util.Collection<com.amazonaws.services.workspaces.model.Tag>) in the type CreateTagsRequest is not applicable for the arguments (java.util.Collection<com.amazonaws.services.ec2.model.Tag>)

It offers me to change 'setTags' to 'withTags' and the opposite (Circular situation) . Tried several methods, looked at the AWS documentation and Javadoc.

This is my imports:

    import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.InstanceStateName;
import com.amazonaws.services.ec2.model.InstanceType;
import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.ec2.model.Tag;

Edit: Current code not working:

Collection<Tag> tags = new ArrayList<Tag>();
Tag t = new Tag();
            t.setKey("Name");
            t.setValue("Manager");
            tags.add(t);
            CreateTagsRequest createTagsRequest = new CreateTagsRequest();  
            createTagsRequest.withTags(tags);
            createTagsRequest.withResourceId(instance.getInstanceId());
            ec2.createTags(createTagsRequest);

Upvotes: 2

Views: 1584

Answers (3)

Brad Rippe
Brad Rippe

Reputation: 3515

An update for AWS SDK version:

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>bom</artifactId>
    <version>2.17.230</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

I used the following code:

Region region = Region.US_WEST_2;
Ec2Client ec2 = Ec2Client.builder()
               .region(region)
               .credentialsProvider(ProfileCredentialsProvider.create())
               .build();

try {
    ArrayList<Tag> tags = new ArrayList<Tag>();
    tags.add(Tag.builder().key("tag").value("value").build());
    CreateTagsRequest request = CreateTagsRequest.builder()
            .resources("instanceId")
            .tags(tags).build();
    ec2.createTags(request);
} catch (Ec2Exception e) {
    System.out.println("Error setting instance id " + instanceId + " with tag " + tag +  " to value " + value);
    System.err.println(e.awsErrorDetails().errorCode());

}
System.out.println("SET instance id " + instanceId + " with tag " + tag + " to value " + value);

Upvotes: 0

Randhir
Randhir

Reputation: 812

Used withResources(instanceId) method from CreateTagsRequest class instead of withResourceId(instanceId) and it worked.

Below code for reference:

     Collection<Tag> tags = new ArrayList<Tag>();
     Tag t = new Tag();
     t.setKey("keyname");
     t.setValue("keyvalue");
     tags.add(t);
     
     CreateTagsRequest createTagsRequest = new CreateTagsRequest();  
     createTagsRequest.withTags(tags);
     createTagsRequest.withResources(instanceId);
     
     ec2Client.createTags(createTagsRequest);

Upvotes: 0

kichik
kichik

Reputation: 34744

It seems like you imported CreateTagsRequest from com.amazonaws.services.workspaces.model instead of from com.amazonaws.services.ec2.model. Notice the difference there between wokspaces and ec2 in the middle.

Upvotes: 2

Related Questions