In the AWS online UI, when creating a cluster, a user can select these additional options:
-
Infrastructure:
- Amazon EC2 instances
- Auto Scaling group: Create new ASG|select
- Provisioning model: On-demand|Spot
- Container instance Amazon Machine Image: Amazon Linux image etc.
- EC2 instance type: t2.micro
- EC2 instance role: Create New Role|Select
- Desired capacity: min: 0, max 5 etc.
- SSH Key pair:
- Amazon EC2 instances
-
Networking settings:
- VPC:
- Subnets:
- Security Group:
When using the terminal – aws cli, the options are limited for creating a cluster.
See example:
https://awscli.amazonaws.com/v2/documentation/api/2.3.2/reference/ecs/create-cluster.html
It is not possible to select most of these other options via the aws create-cluster command. At the very least I want to be able to select EC2 and ASG.
How is this typically done via aws cli?
What other commands would I need to run in aws-cli to replicate what the UI "Create Cluster" does?
My end goal is to push docker images to aws ec2 using aws-cli commands only, before moving onto other orchestration tools.
I don’t need specific implementation details. Just the core steps.
ie
- create-cluster
- create task definition
- what’s next and in what order?
I have so far followed the instructions:
https://docs.aws.amazon.com/AmazonECR/latest/userguide/getting-started-cli.html
Step 1 – step 7.
I cannot find anymore tutorials in AWS documentation to describe my intentions above.
2
Answers
ok after a lot of debugging and tutorials (none of which are complete) I've established a working model of steps.
Pre-requisite: Run and test local Dockerfile for single nginx hello world example
Folder directory
Disclaimer This tutorial also is not complete because you are going to have to supply the variables as below.
Also you will need to configure your current aws user login to have the permission for the following commands. This won't be too hard to discover because every command you follow below will give an error message to say you need that permission. You can simply go to your configured login permission and add them one by one. Eventually your permission will look something like this (not complete).
cd deployScripts
Create-Repository.sh
user-data.template
Run-instance-associate-with-cluster.sh
Create both roles which both have permission "AmazonEC2ContainerServiceforEC2Role":
task-definition.template
Create-task-definition.sh
Visit the Public IPv4 DNS of the in AWS > EC2 > 'nginx-instance'
done!
Conclusion. The original answer as not to use aws cli commands because it isn't typically done this way. Alternatives are to use the AWS console or terraform or potentially ecs-cli, but how do you really learn the low level details if you do that? A lot of the terminology and steps are hidden. My steps above maybe able to refine a little more but I had lots of hurdles to get that to work.
Notes: The main problems I had were following tutorials that weren't complete. For example including the image tag in the taskdefinition.json I added the shortcut name and that continously failed for me. I had to add the full image tag as it is pushed onto aws.
The UI is doing many AWS API calls for you when you use that interface. Using the CLI you would have to run separate CLI commands for each of those AWS API calls. For example just to create the load balancer, you would have to execute quite a few
aws elbv2
calls to create the load balancer itself, its target group, configure listeners on the load balancer, assign an SSL certificate to the load balancer, etc.The AWS CLI is typically not used to create your entire infrastructure like this. While it is certainly possible to use the CLI to do this, there are just much better tools available. I usually only see the AWS CLI used for automating the creation of a single S3 bucket or something, not an entire server cluster with load balancers and everything else that goes with it.
Typically, when people are trying to automate their infrastructure creation, they will use an Infrastructure as Code (IaC) tool, such as CloudFormation, or Terraform. There are also Cloud Development Kits (CDKs) available that allow you to write code in the programming language you prefer, that generate the CloudFormation or Terraform templates for you.
IaC tools also give you the benefits of managing the changing state of your infrastructure over time, and providing drift detection.