2

I have fargate container which has the php prgram.

Then What I want to do is exec command in the container,

it is equivalent to docker command in local.

docker exec -it mycontainer /usr/bin/php mycommand

So, I want to do this from EventBridge (like cron)

Is it possible to do this??

Mark B
  • 183,023
  • 24
  • 297
  • 295
whitebear
  • 11,200
  • 24
  • 114
  • 237

1 Answers1

3

In short:

It is possible to execute command inside container by aws-cli:

aws ecs execute-command  \
    --region $AWS_REGION \
    --cluster ecs-exec-demo-cluster \
    --task ef6260ed8aab49cf926667ab0c52c313 \
    --container nginx \
    --command "/bin/bash" \
    --interactive

Most features that able to run via CLI able to run by using AWS SDK and supported program language.

Any supported language program may be added as AWS Lambda function

Any Lambda may be called by EventBridge

So answer is yes.

Details depends on how you plan to realize code that will be triggered by EventBridge.

rzlvmp
  • 7,512
  • 5
  • 16
  • 45
  • Thank you very much. it works in CLI from my local console but is it possible to trigger this command by EventBridge??? – whitebear May 25 '22 at 07:25
  • here is available [targets](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-targets.html) that can be triggered by EventBridge. I pretty sure that it is possible to implement `execute-command` at least by Lambda or ECS short-running Fargate task. Also try to search about `AWS Systems Manager` → `Run Command` → `AWS-RunDockerAction` feature. – rzlvmp May 25 '22 at 08:17
  • Thank you for your help I will try `RunCommand` – whitebear May 25 '22 at 13:49
  • I use RunCommand to exec the command in container in `ECS` , However I can't select `container` in `ECS` as target. or am I wrong?? – whitebear May 25 '22 at 13:52
  • I think I will make step forward with your help, so I check this as answer. thank you very much. – whitebear May 25 '22 at 14:02
  • It also success `aws ecs execute-command --cluster tvn-prod-cn --container TravelDjangoContainer --interactive --command 'ls -la' --task 0e63831d80654bbb88cbcc4d002b3d4f` so what I should do next is put this command in `RunCommand` – whitebear May 25 '22 at 14:03
  • I didn't use `AWS-RunDockerAction` so can't explain details and don't know how it practically works. Just look over [documentation](https://docs.aws.amazon.com/systems-manager/latest/userguide/ssm-plugins.html#aws-rundockeraction). `However I can't select container in ECS as target` → Do you mean EventBridge target? EventBridge target has to be `AWS Systems Manager`'s `Run command` object. And inside this object you have to set container. That is just my thoughts, saying again, I don't have experience with this service. – rzlvmp May 25 '22 at 14:23
  • 1
    SSM Run Command is specifically for running commands on EC2 servers, not ECS. It isn't appropriate here. – Mark B May 25 '22 at 14:28
  • Thank you very much. I am now make lambda and try to run `aws ecs execute-command --cluster tvn-prod-cn --container TravelDjangoContainer --interactive --command 'ls -la' --task 0e63831d80654bbb88cbcc4d002b3d4f` in it. it's not succeeded yet, but your help gave me the direction. – whitebear May 26 '22 at 16:09