Search code examples
javaaws-lambdaaws-java-sdk

create a snapshot to a EBS volume with tags


I want to create a snapshot for a EBS volume with tags as "backup. "I've written a method to create a snapshot to a EBS volume. The snapshot is being created correctly. However the snapshot is wihout having any tag. The method is as follows.

public Snapshot createEbsSnapshot(String volumeId)
        throws IOException {

    logger.log("============Creating snapshot for a volume: " + volumeId +" ==============");

    String description = "This is a snapshot created by using AWS-Java SDK";

    CreateSnapshotRequest snapshotRequest = new CreateSnapshotRequest(
            volumeId, description);

    CreateSnapshotResult snapshotResult = ec2Client
            .createSnapshot(snapshotRequest);

    Snapshot snapshot = snapshotResult.getSnapshot();       

    logger.log("============Snapshot creation for a volume: " + volumeId +" is done.==============");

    return snapshot;
}

I'm not sure whether we can do this by using aws-java sdk. Please correct me if I'm wrong.

Thank you


Solution

  • The AmazonEC2Client class has a createTags method that can assign tags to a resource. If you take the SnapshotId of the CreateSnapshotResult and use that to call the createTags method, I think you should be able to assign a tag to the snapshot.