Skip to main content

AWS ECS | exec one-off

The Elastic Container Service allows executing one off tasks of any type directly into ECS tasks/containers.


It's important to configure the ECS tasks before trying this feature, please refer to the AWS documentation first

Connection Configuration

CLUSTER_NAMEenv-varThe name or arn of the ECS Cluster
SERVICE_NAMEenv-varThe name of the service on ECS
AWS_ACCESS_KEY_IDenv-varThe access key credential
AWS_SECRET_ACCESS_KEYenv-varThe secret key credential

Connection Command --cluster=$CLUSTER_NAME --service-name=$SERVICE_NAME

How to Use

Now it's possible to execute ruby script straight from Hoop

hoop exec ecs-exec -- --pipe 'rails runner -' <<EOF
myvar='Hello from Rails'
puts myvar
hoop exec ecs-exec -i 'puts Rails.env' -- --pipe 'rails runner -'

The --pipe option requires that the base64 command is available in the image. It will be used to decode the content of the input to prevent content leaking from the shell, like single or double quotes. It helps to address a limitation of the aws ecs execute-command.

It's possible to pipe any command

hoop exec ecs-exec -i '(println "Clojure REPL")' -- --pipe 'clojure'
hoop exec ecs-exec -- --pipe 'python3' <<EOF
import os
# defaults to /bin/bash
hoop exec ecs-exec -i 'echo "hello world from bash"'

The --pipe command works as a pipeline on Linux. It's important that the command could accept input from stdin, e.g.: echo 'ls -l' | /bin/sh

Calling scripts are easy too

hoop exec ecs-exec -i '/path/to/my/'
# override the ecs task-id
hoop exec ecs-exec -i '/path/to/my/' -- --task mytaskid
# execute a rails script
hoop exec ecs-exec -i 'rails runner /path/to/script.rb'