logo

ECS with Rails

Image without caption
This demo shows how to deploy a ruby application on ECS with the hoop agent.

Requirements

  • ECS cluster
  • Hoop Command Line
1. Login at Hoop and generate a client key
shell
hoop login HOOP_KEY=$(hoop admin create agent ecs --mode embedded)
2. Create the application demo image
shell
# https://github.com/hoophq/ruby-getting-started IMAGE=hoophq/ruby-getting-started
If you want to use your own image for this demo, build and push the image to your own registry. In a production setup use the Elastic Container Registry (ECR) service

Embeeded Agent

This demo requires that the hoop agent command is installed locally within your application. Refer to docker installation to learn how to install it.
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 AWS_REGION=us-east-2 AWSLOG_GROUP=/ecs/webapp ROLE_ARN=arn:aws:iam::200074533906:role/ecsTaskExecutionRole cat <<EOF > /tmp/$HOOP_CONNECTION.json { "family": "$HOOP_CONNECTION", "containerDefinitions": [ { "name": "$HOOP_CONNECTION", "image": "$IMAGE", "portMappings": [{ "containerPort": 3000, "hostPort": 3000, "protocol": "tcp" }], "entryPoint": ["hoopstart", "--", "bundle", "exec", "puma", "-C", "config/puma.rb"], "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 add:
  • The name of your cluster
shell
CLUSTER=dev VPC_CONFIG="awsvpcConfiguration={subnets=[subnet-xxx01,subnet-xxx02],securityGroups=[sg-xxxx],assignPublicIp=ENABLED}"
shell
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
3. Connect to the application via console (bash)
Create the connection before proceeding
shell
hoop admin create connection $HOOP_CONNECTION
Now can to interact with it

Start a bash console

shell
hoop connect $HOOP_CONNECTION
this command is analogous to /bin/bash

Start a rails console

shell
hoop connect $HOOP_CONNECTION -- -c 'rails console'
this command is analogous to /bin/bash -c 'rails console'

Execute one off commands

shell
hoop exec $HOOP_CONNECTION -i 'pp Rails.env' -- -c 'rails runner -'
this command is analogous to echo -n 'pp Rails.env' | /bin/bash -c 'rails runner -'
Creating a connection that exposes /bin/bash as entrypoint could be danger. An alternative is defining directly which command to expose it, like rails console, or rails runner -

Powered by Notaku