sdbondi
6/1/2017 - 3:51 PM

Very cheap Mongodb backup using: s3, lambda function, docker and ECS

Very cheap Mongodb backup using: s3, lambda function, docker and ECS

const AWS = require('aws-sdk');

console.log('Loading function');

 const { 
     TASK_FAMILY,
     TASK_CLUSTER,
     TASK_S3DEST,
     TASK_AWS_ACCESS_KEY_ID,
     TASK_AWS_SECRET_ACCESS_KEY,
 } = process.env;

const environment = [{
  name: 'AWS_ACCESS_KEY_ID',
  value: TASK_AWS_ACCESS_KEY_ID,
}, {
  name: 'AWS_DEFAULT_REGION',
  value: 'eu-west-1',
}, {
  name: 'AWS_SECRET_ACCESS_KEY',
  value: TASK_AWS_SECRET_ACCESS_KEY,
}, {
  name: 'DBHOST',
  value: '10.0.0.29',
}, {
  name: 'DBNAME',
  value: 'iscreen',
}, {
  name: 'DBPORT',
  value: '27017',
}, {
  name: 'S3DEST',
  value: TASK_S3DEST,
}];

exports.handler = (event, context, callback) => {
    // Log the received event
    console.log('Received event: ', event);

    const ecs = new AWS.ECS();
   
    ecs.describeTaskDefinition({ taskDefinition: TASK_FAMILY }, (err, result) => {
       if (err) { return callback(err); }
       const { taskDefinitionArn } = result.taskDefinition;
       console.log(`Starting task definition ${taskDefinitionArn}`);
       
       ecs.runTask({ 
           cluster: TASK_CLUSTER, 
           taskDefinition: taskDefinitionArn, 
           count: 1,
           overrides: {
               containerOverrides: [{
                   name: 'mongodb-backup',
                   environment,
               }],
           },
        }, callback);
    });
};