# EC2 (Elastic Compute Cloud) Instance Using AWS CLI through Terminal using Linux

In order to create EC2 instance Login to the AWS Console and get the following information;

* Install AWS CLI for Linux on the computer.
    

`curl "`[`https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip`](https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip)`" -o "`[`awscliv2.zip`](http://awscliv2.zip)`"`

* Install zip/ Unzip to unzip the aws cli package
    

`sudo apt-get install zip (This should also install unzip option too)`

* Unzip the file, currently should be in the same directory.
    

`Unzip` [`awsclive2.zip`](http://awsclive2.zip)

* Navigate to the file and run the command
    

`sudo ./aws/install`

* Create an Admin user through IAM (Identity Access Management).
    

* To get the Access Key ID
    
* Secret Key
    

(Note: <mark>Google is your best friend if you don’t know this search for it.</mark>)

* Need to know the specific region (for this I will be using us-east-1)
    

* Create keypair through AWS CLI
    

`aws ec2 create-key-pair --key-name awsclikey --query 'keyMaterial' --output text > awsclikey.pem`

![](https://lh6.googleusercontent.com/nvGyYxU7-Em28aqNSUst0IVEtlDS028CGrOFkS3sL1YtfeNwzKMAfGPIzsVUW4N6oD_NlVcdMEaLnwGaV5GGM_V2FrB8B-BElxWK1gBfcHaoVccEH-S_15v09g56jpDyKM-nTPo5E-MGb24UGtPTgdvdeEYa1_eI3et_9e14MnY4r1m79QcCrCxqUJQ8cw align="left")

* Change the permission of the key to it has read-only access
    

`chmod 400 awsclikey.pem`

* A filter name and value pair that is used to return a more specific list of results from a described operation.
    

`aws ec2 describe-key-pairs --key-names awsclikey`

* We will need a security group to manage the traffic that will be using the EC2 instances.
    

`aws ec2 create-security-group --group-name awscligroup --description "group created with awscli" --vpc-id vpc-0e4167f02a110a4ed`

***Note: <mark>Get the Default VPC Id from the console</mark>***

![](https://lh5.googleusercontent.com/Rf8FDmDeUcKmrBWUJqxjkYEl6wz38Cl5x-XTZxMwzQQYeW3YfeW2X_jM5EmmBsv-1xjct0VG_ZjHjj3IDY1DbR1hKtkaCRozoBfY9YOFE1PoFTqJB26U1_mtTovbc-dXvlTZx7K9maHx9_O89epjPTv1G3yJ1JRSWEbujccQQ352M058BqQP17ELTNrafg align="left")

* Define the inbound rules now
    

`aws ec2 authorize-security-group-ingress --group-id sg-0b2ba2fe2deb772b6 --protocol tcp  --port 0-65535 --cidr 0.0.0.0/0`

![](https://lh3.googleusercontent.com/FKDNq6U9_Qh54gW9IAIFOiC2seZQCSkO_C3wHNPGEm23byjD_9B7CSZuK2eHPlOXQJSYou0xFUDaVdiAlYds6M-v6OuDcmr2ckKeZfMTR7pHsLY0IIdQEnGFxbVDSdf25CxJLGtrUg-DQr_SWE0UPmO_W-bU7WpBow54zpFrBsE_1d_LHO9P4OSaj7ld7A align="left")

* Ingress means input bounds rule
    

* Group id is what we created just now.
    
* Protocol tcp which we will be using
    
* Port has to define the range 0-65535
    
* Define the IP address 0.0.0.0/0
    
* To perform the next step we will need the Image Id (*a specific image we will be using for our instance. For this demo we will be using Ubuntu Linux LTS 20*).
    

`aws ec2 run-instances --image-id ami-0b93ce03dcbcb10f6 --count 2 --instance-type t2.micro --key-name awsclikey --security-group-ids sg-0b2ba2fe2deb772b6 --subnet-id subnet-013a2aa48b12f80b2`

![](https://lh4.googleusercontent.com/HIyzzjhliRiFFisR1Omt52dtRkbDNNPd-D-i8oyEzBlOPmd5sHsGkDt6XYaVdRXcusdNxOGdw1BV6yf6BFbu00NfyMMv84hG3tLkW-W3Qv09pP1-LwWXa1LaY_0VxBcDMeHKyjZyiccQwTr6mT7uJNBgO3bBP9lEoFgd1I2eb94F9Dcp5VogwrGLVL73Bw align="left")

* The EC2 instance will be ready shortly.
