Starting AWS EC2 Using aws command Line
EC2 instance configuration using aws command line
OS :-- Ubuntu 20.04.2 LTS
Required package : aws CLI v2
Step 1:-- Downloading & Installing package
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
Step 2:-- Configure AWS CLI
- Before Configuring AWS CLI, We have to generate ACCESS ID & SECRET ACCESS CODE for user. Root account is not recommended to generate ACCESS ID & SECRET ACCESS CODE. So login in root AWS console and navigate to "Identity and Access Management (IAM)" add a new user using https://console.aws.amazon.com/iam/
- Follow the on screen steps to generate a new user and get access key and SECRET ACCESS CODE from there like this :----
Secret access key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
- Then run below command to configure AWS CLI on your Laptop
These entries can be found on local machines on below paths. Find below screenshot for reference
STEP 3:-- After configuring AWS cli , We need to generate key pair and then make 400 permission on keypair.
- Create a key pair
- Display your key pair
aws ec2 create-key-pair --key-name SharadKeyPair --query 'KeyMaterial' --output text > SharadKeyPair.pem
Change permission of key pair
chmod 400 SharadKeyPair.pem
Step 4:-- Next create security group in AWS, Commands are as below :--
aws ec2 create-security-group --group-name my-sg --description "My security group" --vpc-id vpc-1a2b3c4d
<vpc-id> This VPC id can be find from aws console using https://ap-south-1.console.aws.amazon.com/vpc/home?region=ap-south-1#dashboard:
You can create a security group for your Amazon Elastic Compute Cloud (Amazon EC2) instances that essentially operates as a firewall, with rules that determine what network traffic can enter and leave.
Security group description can be found using below command
aws ec2 describe-security-groups --group-ids sg-903004f8
Generated security group can be validated using AWS console on browser.
If you want to check generated security group details use below commands to check on console:--
aws ec2 describe-security-groups --group-ids sg-<security group id >
Step 5:-- Add rules to your security group
Adding rule will allow accessing newly created machine from your local computer. First of all check your public IP and if it is changing frequently then allow IP range for that IP.
Checking public IP :-- curl https://checkip.amazonaws.com
Then add the range to your security group by running below command:---
aws ec2 authorize-security-group-ingress --group-id sg-<ID> --protocol tcp --port 22 --cidr 203.0.113.0/24
Below commands can be used to view complete security group:---
aws ec2 describe-security-groups --group-ids sg-<ID>
Step 6:-- Now run EC2 instance running below command
Create a default subnet ID
aws ec2 create-default-subnet --availability-zone ap-south-1a
Now run below command to run EC2 instance
aws ec2 run-instances --image-id ami-0a4a70bd98c6d6441 --count 1 --instance-type t2.micro --key-name SharadKeyPair --security-group-ids --subnet-id subnet-<SubnetID>
Image Id:-- Can be found from https://aws.amazon.com/amazon-linux-ami/ (I have picked free tier Id)
Step 6:-- Validate EC2 instance if it is running or not using AWS console on browser.
👍
ReplyDelete