• Status: Solved
  • Priority: High
  • Security: Private
  • Views: 168
  • Last Modified:

Help preserve AWS tags in node.js

Hi everyone,
Does anyone know how to preserve snapshot tags in AWS. This node.js function moves the snapshots to another region but does not preserve tags so it becomes difficult to identify which server the snapshot belonged to.
If I cannot preserve tags, I would like to know how to tag a snapshot using the javascript code below. Example Key:DeleteOn Value [currentdate]
I found this code snippet that may be of help:
http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/ec2-example-creating-an-instance.html

 // Add tags to the instance
   params = {Resources: [instanceId], Tags: [
      {
         Key: 'DeleteOn',
         Value: 'current date as 2017-12-05'
      }
   ]};
   ec2.createTags(params, function(err) {
      console.log("Tagging instance", err ? "failure" : "success");
   });
Thanks!
Peter
//define variables
var sourceRegion = 'us-east-1';
var destinationRegion = 'us-east-2';
var AWS = require('aws-sdk');
console.log ('Loading function');

//main function
exports.handler = (event, context, callback) => {

    //Get the EBS snapshot ID from Cloudwatch event details
    var snapshotArn = event.detail.snapshot_id.split('/');
    const snapshotId = snapshotArn['1'];
    const description = 'Snapshot copy from ${snapshotId} in ${sourceRegion}';
    console.log ("snapshotId:", snapshotId);

    //Load EC2 class and update the configuration to use destination
    AWS.config.update({region: destinationRegion});
    var ec2 = new AWS.EC2();

    //Prepare variables for ec2.modifySnapshotAttribute call
    const copySnapshotParams = {
        Description: description,
        DestinationRegion: destinationRegion,
        SourceRegion: sourceRegion,
        SourceSnapshotId: snapshotId
    };

    ec2.copySnapshot(copySnapshotParams, (err, data) => {
        if (err) {
            const errorMessage = `Error copying snapshot ${snapshotId} to region ${destinationRegion}.`;
            console.log(errorMessage);
            console.log(err);
            callback(errorMessage);
        } else {
            const successMessage = `Successfully started copy of snapshot ${snapshotId} to region ${destinationRegion}.`;
            console.log(successMessage);
            console.log(data);
            callback(null, successMessage);
        }
    });
};

Open in new window

0
peter
Asked:
peter
  • 2
  • 2
1 Solution
 
Phil PhillipsDirector of DevOps & Quality AssuranceCommented:
You'll likely have to describe the tags on the source snapshot, then create them on the destination.  Something like this (untested, so I don't know if it works or not! But hopefully gives you the right idea):

//define variables
var sourceRegion = 'us-east-1';
var destinationRegion = 'us-east-2';
var AWS = require('aws-sdk');
console.log ('Loading function');

//main function
exports.handler = (event, context, callback) => {

    //Get the EBS snapshot ID from Cloudwatch event details
    var snapshotArn = event.detail.snapshot_id.split('/');
    const snapshotId = snapshotArn['1'];
    const description = 'Snapshot copy from ${snapshotId} in ${sourceRegion}';
    console.log ("snapshotId:", snapshotId);

    //Load EC2 class and update the configuration to use destination
    AWS.config.update({region: destinationRegion});
    var ec2 = new AWS.EC2();

    //Prepare variables for ec2.modifySnapshotAttribute call
    const copySnapshotParams = {
        Description: description,
        DestinationRegion: destinationRegion,
        SourceRegion: sourceRegion,
        SourceSnapshotId: snapshotId
    };

    ec2.copySnapshot(copySnapshotParams, (err, data) => {
        if (err) {
            const errorMessage = 'Error copying snapshot ${snapshotId} to region ${destinationRegion}.';
            console.log(errorMessage);
            console.log(err);
            callback(errorMessage);
        } else {
            const successMessage = 'Successfully started copy of snapshot ${snapshotId} to region ${destinationRegion}.';
            console.log(successMessage);
            console.log(data);
            copyTags(snapshotId, data.SnapshotId, (err, data) => {
                if (err) {
                    callback(err);
                }
                else {
                    callback(null, successMessage);
                }
            });
        }
    });
};

function copyTags(sourceId, destinationId, callback) {
    // Get the tags to copy
    ec2.describeTags({
        Filters: [
            {
                Name: "resource-id",
                Values: [ sourceId ]
            }
        ]
    }, (err, data) => {
        if (err) {
            const errorMessage = 'Error describing ${sourceId}';
            console.log(errorMessage);
            console.log(err);
            callback(errorMessage);
        } else {
            ec2.createTags({
                Resources: [ destinationId ],
                Tags: data.Tags
            }, (err, data) => {
                if (err) {
                    const errorMessage = 'Error tagging ${destinationId}';
                    console.log(errorMessage);
                    console.log(err);
                    callback(errorMessage);
                }
                else {
                    callback(null);
                }
            });
        }
    });
}

Open in new window

0
 
petersystems engineerAuthor Commented:
Ok, that does make sense. I don't know about the formatting however, looks pretty broken. But , I can work with it. I'm wondering however if maybe its best to have a separate Python or node.js script at the destination [in us-east-2] to iterate over any new snapshots that have arrived, and tag them, with the current time. Then another script to delete snapshots with tags older then 7 days.
0
 
Phil PhillipsDirector of DevOps & Quality AssuranceCommented:
You can have the copy script also tag them at the same time.  In before calling ec2.createTags, you can add additional values to the data.tags variable.

I actually use this tool to do something similar: https://github.com/evannuil/aws-snapshot-tool
Though been meaning to replace it with a Node.js variant at some point (mainly so I can run with via Lambda).
0
 
petersystems engineerAuthor Commented:
I'll close this question, as helpful to solve my problem, thank you!
Peter
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now