Quick Start: EC2
What is ECS EC2?
ECS EC2 is a way to run Docker containers on your own EC2 instances. This difference between ECS EC2 and ECS Fargate is who manages the servers. With ECS Fargate, AWS manages the EC2 instances for you and provides an interesting “serverless” option. With ECS EC2, you manage the EC2 instances and are responsible for maintenance. The pro with ECS EC2 is more control and cost savings, since you’re not paying for the overhead for AWS Fargate maintenance. Refer to Heroku vs ECS Fargate vs EC2 On-Demand vs EC2 Spot Pricing Comparison for a pricing comparison.
In a hurry? No sweat! Here’s a quick start to using ufo that takes only a few minutes. For this example, we will use a Sinatra app from tongueroo/demo-ufo. The
ufo init command sets up the ufo directory structure in your project. The
ufo ship command deploys your code to an AWS ECS service. The
ufo ps and
ufo scale command shows you how to verify and scale additional containers.
gem install ufo git clone https://github.com/tongueroo/demo-ufo.git demo cd demo aws ecr create-repository --repository-name demo/sinatra ECR_REPO=$(aws ecr describe-repositories --repository-name demo/sinatra | jq -r '.repositories.repositoryUri') ufo init --image $ECR_REPO ufo current --service demo-web ufo ship ufo ps ufo scale 2
This quickstart assumes:
- You have push access to the repo. Refer to the Notes “Repo Push Access” section below for more info.
- You are using ECS EC2 and have an ECS cluster with EC2 Container instances running. Refer to the Notes “ECS EC2 vs ECS Fargate” section below for more info.
- The ECS Cluster is in the default VPC. If it is not you need to use the
--elb-subnetsoptions in the ufo init command.
ufo ship demo-web command does the following:
- Builds the Docker image and pushes it to a registry
- Builds the ECS task definitions and registry them to ECS
- Updates the ECS Service
- Creates an ELB and connects it to the ECS Service
You should see something similar to this:
$ ufo init --app=demo --image=tongueroo/demo-ufo Setting up ufo project... create .env create .ufo/settings.yml create .ufo/task_definitions.rb create .ufo/templates/main.json.erb create .ufo/variables/base.rb create .ufo/variables/development.rb create .ufo/variables/production.rb create Dockerfile create bin/deploy append .gitignore Starter ufo files created. $ ufo ship demo-web Building docker image with: docker build -t 112233445566.dkr.ecr.us-west-2.amazonaws.com/demo/sinatra:ufo-2017-09-10T15-00-19-c781aaf -f Dockerfile . .... Software shipped! $ ufo ps +----------+------+-------------+---------------+---------+-------+ | Id | Name | Release | Started | Status | Notes | +----------+------+-------------+---------------+---------+-------+ | f590ee5e | web | demo-web:85 | 1 minutes ago | RUNNING | | +----------+------+-------------+---------------+---------+-------+ $ ufo scale 2 Scale demo-web service in development cluster to 2 $
Congratulations! You have successfully deployed code to AWS ECS with ufo. It was really that simple 😁
Repo Push Access
The quick start example pushes the Docker image to a Docker repo that you have access to. This repo could be your own Dockerhub account or ECR repo. You can control the setting with the
--image option. Example:
ufo init --image=yourusername/yourrepo # use your own Dockerhub account
Also, if you are using ECR instead, you can specify an ECR repo with the
--image option. Example:
ufo init --image 123456789012.dkr.ecr.us-west-2.amazonaws.com/myimage
For more info, refer to the ufo init reference docs.
ECS EC2 vs ECS Fargate
Ufo does not create the EC2 servers themselves to run the ECS tasks. If you use
ufo ship to deploy an application to ECS EC2 and have not set up the EC2 servers, then the CloudFormation update will not be able to provision the ECS tasks and eventually roll back. Essentially it cannot create the ECS tasks because there are no EC2 servers to run them.
If you would like not to manage the EC2 server fleet, you are looking for ECS Fargate instead of ECS EC2. ECS Fargate allows you to run ECS Tasks and AWS will manage the EC2 server fleet for you. Refer to the Quick Start: Fargate docs and use those quick start like commands instead. The pricing for Fargate is more because AWS manages the server fleet for you. Refer to Heroku vs ECS Fargate vs EC2 On-Demand vs EC2 Spot Pricing Comparison for a pricing comparison.
Learn more in the next sections.
Pro tip: Use the <- and -> arrow keys to move back and forward.