Makriggs M
Makriggs M

Reputation: 3

Aws Java sdk how to set tags to volume

I am trying to create an ec2 volume using tags

        //Create ec2 client using credentials
    AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(creds).withRegion(region).build();

    List<TagSpecification> ts = new ArrayList<TagSpecification>();
    ts.add(new TagSpecification().withTags(new Tag("Name","HelloWorld")));

    CreateVolumeRequest cvr = new CreateVolumeRequest(10,azone);
    cvr.setVolumeType(VolumeType.Gp2);
    cvr.setTagSpecifications(ts);

    ec2.createVolume(cvr);

But it throws the following error when I try to execute the above code.

Exception in thread "main" com.amazonaws.services.ec2.model.AmazonEC2Exception: 'null' is not a valid taggable resource type for this operation. (Service: AmazonEC2; Status Code: 400; Error Code: InvalidParameterValue)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1588)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1258)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1030)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:742)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:716)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:667)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
    at com.amazonaws.services.ec2.AmazonEC2Client.doInvoke(AmazonEC2Client.java:13611)
    at com.amazonaws.services.ec2.AmazonEC2Client.invoke(AmazonEC2Client.java:13587)
    at com.amazonaws.services.ec2.AmazonEC2Client.executeCreateVolume(AmazonEC2Client.java:3453)
    at com.amazonaws.services.ec2.AmazonEC2Client.createVolume(AmazonEC2Client.java:3430)
    at S3Driver.main(S3Driver.java:49)

Process finished with exit code 1

Upvotes: 0

Views: 1605

Answers (2)

Will Sterling
Will Sterling

Reputation: 21

The answer above requires you to first create the volume, then make a second call to the EC2 API to separately tag the resource. While it does work, the extra call into the API does take a bit more time and creates additional exception/failure scenarios.

The following code should accomplish your goal with only a minor tweak to your original code, and without requiring a separate call to the ec2 api (the tags are applied at creation time). The only change is - .withResourceType(ResourceType.Volume).

        //Create ec2 client using credentials
    AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(creds).withRegion(region).build();

    List<TagSpecification> ts = new ArrayList<TagSpecification>();
    ts.add(new TagSpecification().withTags(new Tag("Name","HelloWorld"))
      .withResourceType(ResourceType.Volume));

    CreateVolumeRequest cvr = new CreateVolumeRequest(10,azone);
    cvr.setVolumeType(VolumeType.Gp2);
    cvr.setTagSpecifications(ts);

    ec2.createVolume(cvr);

A further simplification would be to use the varargs method instead of creating a list (especially if you're only setting one tag) -

        //Create ec2 client using credentials
    AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(creds).withRegion(region).build();

    TagSpecification ts = new TagSpecification()
      .withTags(new Tag("Name","HelloWorld"))
      .withResourceType(ResourceType.Volume);

    CreateVolumeRequest cvr = new CreateVolumeRequest(10,azone)
      .withVolumeType(VolumeType.Gp2);
      .withTagSpecifications(ts);

    ec2.createVolume(cvr);

Upvotes: 2

Chris
Chris

Reputation: 3657

the following code should create a new volume and assign tags to it.

public void createVolumeWithTags() {
    AmazonEC2 amazonEC2 = AmazonEC2ClientBuilder.standard()
        .withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
        .withRegion(Regions.US_WEST_2)
        .build();

    CreateVolumeRequest createVolumeRequest = new CreateVolumeRequest(10, "us-west-2a");
    CreateVolumeResult createVolumeResult = amazonEC2.createVolume(createVolumeRequest);

    ArrayList<Tag> instanceTags = new ArrayList<Tag>();
    instanceTags.add(new Tag("Name", "HelloWorld"));
    CreateTagsRequest createTagsRequest = new CreateTagsRequest().withTags(instanceTags).withResources(createVolumeResult.getVolume().getVolumeId());

    amazonEC2.createTags(createTagsRequest);
}

Upvotes: 0

Related Questions