Ste
Ste

Reputation: 685

Run CLI command on ECS Fargate container

How is it possible to run a CLI command within a container that's using ECS/Fargate?

Upvotes: 9

Views: 7028

Answers (5)

tekneee
tekneee

Reputation: 691

DEPRECATED: As mentioned on this answer (How can I run commands in a running container in AWS ECS using Fargate) you cannot do it due to the fact AWS doesn't give you access to the underlying infrastructure.

UPDATE: Pierre below mentions an announcement from AWS allowing to do just that.

Upvotes: 3

Tim Mortimer
Tim Mortimer

Reputation: 31

AWS have now launched Amazon ECS Exec, which allows you to directly interact with containers: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-exec.html.

Upvotes: 3

tanvi
tanvi

Reputation: 628

You may be able to script your container to execute a cli command, but you cannot ssh into the container though. If you can invoke a .sh file from the CMD command in the Dockerfile, the cli command will get executed as long as you have aws-cli installed on the docker image.
In the Dockerfile make sure to run pip3 install awscli --upgrade --user before you invoke your script that contains cli commands.

As an alternative, you can use boto3 for Python or the AWS SDK for JavaScript, which both have comprehensive documentation and enable you to run all the commands you could have run via cli

Upvotes: 0

Ing. Luca Stucchi
Ing. Luca Stucchi

Reputation: 3277

I don't know if this is what you are trying to achieve, but if you want you can run a command on a new container that you instantiate for the occasion through a CloudWatch Rule

It will be enough to create a new task definition and indicate the command to execute (in the example executing a Laravel command)

ECSReputationSchedulerTask:
  Type: AWS::ECS::TaskDefinition
  Properties:
    Cpu: 256
    ExecutionRoleArn: !ImportValue ECSTaskExecutionRole
    Family: TaskDefinitionFamily
    Memory: 512
    NetworkMode: awsvpc
    RequiresCompatibilities:
      - FARGATE
    ContainerDefinitions:
      -
        Command:
          - "php"
          - "/home/application/artisan"
          - "execute:operation"
        Name: 'MySchedulerContainer'
        ...

and then reference it into a CloudWatch rule (unfortunately this can't be done via CloudFormation yet)

Upvotes: 0

Harsh Manvar
Harsh Manvar

Reputation: 30090

As i know and having experience on ECS you are not allowed to do it. aws does not give you access to the underlying resources.

if you are using fargate + EC2 Configuration then also it is not to access EC2.

Upvotes: 1

Related Questions