An EC2 instance is a virtual server in Amazon’s Elastic Compute Cloud (EC2) for running applications on web services (AWS) infrastructure. Instances are created from Amazon Machine Images (AMI).
To create an instance using AWS CLI first we need to configure AWS CLI on the terminal, open your terminal and type the following command:
$ aws configure
AWS CLI prompts you for access key, secret access key, AWS Region, and output format and stores them in a profile (a collection of settings) named
After configuration we need to go through a checklist:
1 . Regions– let’s check the regions available, by using the command
$ aws ec2 describe-regions --output table
Change the region using command:
$ aws configure set default.region eu-west-1
2 . Availability Zone – Check the availability zones for the region you selected.
$ aws ec2 describe-availability-zones --region eu-west-1
You can see the availability zones and their status as available, it is in the default Json format
3 . Amazon Machine Image – It serves as template for your instance. To select AMI, copy the AMI id, and then use following command to view the details of the AMI
$ aws ec2 describe-images --image-id ami-030dbca661d40241
4 . Key_pairs -You’ll need to provide the name of your key pair when you launch an instance and the corresponding private key each time you connect to the instance. To view key pairs available use the following command:
$ aws ec2 describe-key-pairs
5 . Security Groups – It provides security at the protocol and port access level. To view available security groups , use the following command it will list all the security groups available .
$ aws ec2 describe-security-groups --output table
6 . Instance type : Instance type consists combination of memory, storage, network performance, CPU and instance sizes. Choosing Instance type allows you to scale your resources to the requirements of your target workload.
Now you know all the requirements for creating Instance, lets create instance using command:
$ aws ec2 run-instances --image-id ami-030dbca661d402413 --count 1 --instance-type t2.micro --key-name test_pair --subnet subnet-89f16bef --security-group-ids sg-81bd97ff
- Image-id : copy your AMI image Id, here we are using Amazon Linux 2AMI.
- Count : count describes number of instances to be launched.
- Instance type: The type of instance you want to launch, here we are using t2.micro
- Key-name : here we write the name of the key pair file.
- Subnet : Copy the subnet id from your console
- Security group: copy the security group id from the existing group or you can create new one.
Your instance has been created now use your instance id to check the state of your instance , using command:
$ aws ec2 describe-instance-id i-082efad5784896962 --output table
Important : copy your public Ip address or Public Dns name to ssh into your instance.
Connecting to Amazon EC2 Instance
Use the chmod command to make sure your private key file isn’t publicly viewable.
$ chmod 400 test_pair.pem
You have copied Public Ip address which will be used to connect instance , use command :
$ ssh -i my-key-pair.pem ec2-user@public_dns_name/public_ip_address
To terminate the instance use following command :
$ aws ec2 terminate-instances --instance-ids i-082efad5784896962