Home > Net >  Using aws ecs execute-command in aws not local for cron like usage
Using aws ecs execute-command in aws not local for cron like usage

Time:05-25

I have fargate container.

Now I can access the container and exec the command such as , (in this case I did ls -la for test)

aws ecs execute-command --cluster tvn-prod-cn --container TravelDjangoContainer --interactive --command 'ls -la' --task 0e63831d80654bbb88cbcc4d002b3d4f

So, what I would like to do is set this command as cron like.

My ideas are below

  • set this command in SSM RunCommand

  • trigger RunCommand by EventBridge

Is it possible ??

How can I set this command in RunCommand?


I made lambda with serverless

I googled around and make this script, but it's not correct,,, Could anyone helps or samples??

'use strict';
const AWS = require('aws-sdk')
const SSM = new AWS.SSM({region: 'ap-northeast-1'})
const REMOTE_WORKING_DIR = '/home/ec2-user'

module.exports.hello = async (event) => {
  try {
    let command = 'ls -la'

    let params = {
      DocumentName: 'AWS-RunExecScript',
      Parameters: {
        commands: [command],
        workingDirectory: [REMOTE_WORKING_DIR]
      },
      CloudWatchOutputConfig: {
        CloudWatchLogGroupName: 'SSMLogs',
        CloudWatchOutputEnabled: true
      },
      TimeoutSeconds: 3600 // 1 hour
    }

    SSM.sendCommand(params, function(err, data){
      if(err){
        console.log(err, err.stack)
      } else {
        console.log(data)
      }
    });
  } catch(e){
    console.log(e);
  };

  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: 'Go Serverless v1.0! Your function executed successfully!',
        input: event,
      },
      null,
      2
    ),
  };

};

CodePudding user response:

Instead of using SSM RunCommand, which isn't really designed to run tasks in ECS, I would create a Lambda function that calls ECS execute_command() and schedule that using the EventBridge cron schedule.

If you want to use SSM RunCommand, you would need an EC2 server that it could run the aws ecs execute-command CLI command from, which is surely not what you want.

  • Related