logo

AWS ECS

1. Create the Task Definition
Before creating the task definition, make sure that you have:
  • The log configuration group configured properly
shell
HOOP_CONNECTION=demo-rails-sidecar AWS_REGION=us-east-2 AWSLOG_GROUP=/ecs/webapp ROLE_ARN=arn:aws:iam::200074533906:role/ecsTaskExecutionRole HOOP_KEY= cat <<EOF > /tmp/$HOOP_CONNECTION.json { "family": "$HOOP_CONNECTION", "containerDefinitions": [ { "name": "$HOOP_CONNECTION", "image": "$IMAGE", "portMappings": [{ "containerPort": 3000, "hostPort": 3000, "protocol": "tcp" }], "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "$AWSLOG_GROUP", "awslogs-region": "$AWS_REGION", "awslogs-stream-prefix": "ecs" } } }, { "name": "hoopagent", "image": "$IMAGE", "command": ["hoop", "start", "agent"], "environment": [ { "name": "HOOP_KEY", "value": "$HOOP_KEY" }, { "name": "HOOP_CONNECTION", "value": "$HOOP_CONNECTION" } ], "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "$AWSLOG_GROUP", "awslogs-region": "$AWS_REGION", "awslogs-stream-prefix": "ecs" } } } ], "taskRoleArn": "$ROLE_ARN", "executionRoleArn": "$ROLE_ARN", "networkMode": "awsvpc", "requiresCompatibilities": [ "FARGATE" ], "cpu": "512", "memory": "1024" } EOF
shell
aws ecs register-task-definition \ --region=$AWS_REGION \ --cli-input-json file:///tmp/$HOOP_CONNECTION.json
2. Create the Service
Before you proceed, make sure to known:
  • The name of your cluster
plain text
CLUSTER=dev VPC_CONFIG="awsvpcConfiguration={subnets=[subnet-xxx01,subnet-xxx02],securityGroups=[sg-xxxx],assignPublicIp=ENABLED}"
plain text
aws ecs create-service --cluster $CLUSTER \ --service-name=$HOOP_CONNECTION \ --task-definition=$HOOP_CONNECTION \ --region=$AWS_REGION \ --desired-count=2 \ --launch-type FARGATE \ --platform-version LATEST \ --network-configuration=$VPC_CONFIG
Wait for the service to start the web application

Start a bash console

shell
# this is analougous to # /bin/bash hoop connect ecs:$HOOP_CONNECTION

Start a rails console

shell
# this is analougous to # /bin/bash -c 'rails console' hoop connect ecs:$HOOP_CONNECTION -- -c 'rails console'

Execute one off commands

shell
# this is analougous to # echo -n 'pp Rails.env' | /bin/bash -c 'rails runner -' hoop exec ecs:$HOOP_CONNECTION -i 'pp Rails.env' -- -c 'rails runner -'

Powered by Notaku