Search code examples
amazon-web-servicesamazon-ec2aws-lambdaamazon-ami

access all ec2 cross region via lambda


I have lambda function for auto Ami backup is possible to execute lambda across the region for take automatic backup of all my EC2 working on account.

One lambda function execution for all ec2 across region

    var aws = require('aws-sdk');  
aws.config.region = 'us-east-1','ap-south-1','eu-central-1';  
var ec2 = new aws.EC2();  
var now = new Date();  
date = now.toISOString().substring(0, 10)  
hours = now.getHours()  
minutes = now.getMinutes()  
exports.handler = function(event, context) {  
    var instanceparams = {
        Filters: [{
            Name: 'tag:Backup',
            Values: [
                'yes'
            ]
        }]
    }
    ec2.describeInstances(instanceparams, function(err, data) {
        if (err) console.log(err, err.stack);
        else {
            for (var i in data.Reservations) {
                for (var j in data.Reservations[i].Instances) {
                    instanceid = data.Reservations[i].Instances[j].InstanceId;
                    nametag = data.Reservations[i].Instances[j].Tags
                    for (var k in data.Reservations[i].Instances[j].Tags) {
                        if (data.Reservations[i].Instances[j].Tags[k].Key == 'Name') {
                            name = data.Reservations[i].Instances[j].Tags[k].Value;
                        }
                    }
                    console.log("Creating AMIs of the Instance: ", name);
                    var imageparams = {
                        InstanceId: instanceid,
                        Name: name + "_" + date + "_" + hours + "-" + minutes,
                        NoReboot: true
                    }
                    ec2.createImage(imageparams, function(err, data) {
                        if (err) console.log(err, err.stack);
                        else {
                            image = data.ImageId;
                            console.log(image);
                            var tagparams = {
                                Resources: [image],
                                Tags: [{
                                    Key: 'DeleteOn',
                                    Value: 'yes'
                                }]
                            };
                            ec2.createTags(tagparams, function(err, data) {
                                if (err) console.log(err, err.stack);
                                else console.log("Tags added to the created AMIs");
                            });
                        }
                    });
                }
            }
        }
    });
}

where aws.config.region is for region config..it's working for current(in which lambda deploy) region


Solution

  • This line:

    var ec2 = new aws.EC2(); 
    

    connects to the Amazon EC2 service in the region where the Lambda function is running.

    You can modify it to connect to another region:

    var ec2 = new AWS.EC2({apiVersion: '2006-03-01', region: 'us-west-2'});
    

    Thus, your program could loop through a list of regions (from ec2.describeRegions), creating a new EC2 client for the given region, then running the code you already have.

    See: Setting the AWS Region - AWS SDK for JavaScript