How to launch an EC2 instance in CLI?
In industries we don’t have two or three instance for work but they can be hundreds,thousands and even millions if we talk about companies like Netflix.
In WebUI it will be quite handy for us to launch hundred instances. For example if you want to launch10 instances of t2.micro ,10 instances of t2.large and so on, then you will have to go to EC2 dashboard again and again and configure them. On the other hand if you use CLI then you just need to run a command.
Let’s do some basics in EC2 instance:
Firstly we will create the key-pair ,volume and security groups and with these key-pair and security-groups we will launch our instance. At final we will attach volume to our instance.
- Creating a Key pair:
The highlighted portion shows the command to create key-pair in CLI.
The output text save the key in your current working directory which you can use to connect to your instance through ssh protocol
2.Creating security groups:
Here we can provide the IP’s also to control inbound traffic.For more you can use aws ec2 create-security-group help command in CLI.
3.Launching an instance:
Currently i am having three instances with no name.I am terminating the topmost instance or instance having id i-010dd289aa986do8.Let’s launch an instance with name myInstance.
Here we launches an instance with name myInstance using key myclikey and security groups MySecurityGroup.Here you can also see we specify number of instances with count in my case it’s value is 1. If you want to launch hundred instances then you should change it as --count 100.If we talk about storage we have not specified the storage so aws by default choose the 8Gib of type gp2 for us.
Now let’s see EC2 dashboard:
For above discussion of 10 instances of t2.micro ,10 instances of t2.large and so on.You can do following
aws ec2 run-instances — image-id ami-0e306788ff2473ccb — instance-type t2.micro — count 10 — security-group-ids sg-02fb764381c3dd016 — key-name myclikey
aws ec2 run-instances — image-id ami-0e306788ff2473ccb — instance-type t2.large — count 10 — security-group-ids sg-02fb764381c3dd016 — key-name myclikey
4.Attaching a volume to an instance:
Above i have only one volume attached to one of my instance. Now attaching another volume to it.
Now let’s see the WebUI:
Finally we have covered what i have told you earlier.There is lots of things we can do with the above commands especially in the ec2 run-instance command.
Hope it helps the needy.