Amazon EC2 Node JS API Code Snippets

In this blog post, will see few examples of using NodeJS EC2 API.

First you need to install the nodejs library, this can be done using

npm install aws-sdk

Next you need to do to configuration the api using your access key and secret key

var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: 'your-key', secretAccessKey: 'your-secret'});
AWS.config.update({region: 'us-west-2'});
var ec2 = new AWS.EC2();

Check Running EC2 Instances

Below code returns all running ec2 instances and its details. More details can be found here

ec2.describeInstances(function(err, result) {
                    if (err)
                        console.log(err);
                    var inst_id = '-';
                    for (var i = 0; i < result.Reservations.length; i++) {
                        var res = result.Reservations[i];
                        var instances = res.Instances;
                        for (var j = 0; j < instances.length; j++) {
                            var instanceID = instances[j].InstanceId;
                            var state = instances[j].State.Code;
                            var public_ip = instances[j].PublicIpAddress;
                            var imageID = instances[j].ImageId;
                            console.log('instance ' + instanceID + " state " + state + " public ip " + public_ip + 'image id ' + imageID);
                        }
                    }
                });

Start New EC2 Instance

Below code is to start a new ec2 instance, more details here

ec2.runInstances({
                            ImageId: 'ami-97XXXXX',
                            MaxCount: 2,
                            MinCount: 1,
                            BlockDeviceMappings: [
                                {
                                    DeviceName: '/dev/sda1',
                                    Ebs: {
                                        DeleteOnTermination: true,
                                        VolumeSize: 10
                                    }
                                }
                            ],
                            InstanceType: 't1.micro',
                            SecurityGroupIds: ['sg-074d9862'],
                            Monitoring: {Enabled: false},
                        }, function(err, data) {
                            if (err) {
                               console.log("Could not create instance", err);
                                return;
                            }else{
console.log('instances started');
}
                            
                        });

Terminate EC2 Instance

Code to terminate instances below, more details can be seen here

var terminate = new Array();
 terminate[terminate.length] = 'instanceID1';
 terminate[terminate.length] = 'instanceID2';
 ec2.terminateInstances({
                    InstanceIds: terminate
                }, function(err) {
                    if (err)
                        console.log("terminate" + err);
                });