DEV Community

Cover image for How to connect aws EC2 to CLI?
Subathra
Subathra

Posted on

How to connect aws EC2 to CLI?

Hi everyone! In this blog i am going to share the details that how to connect your EC2 instance to your CLI ...So bing reading.....
Step 1:
In first step you have to create the aws EC2 instance..It can be done by following 4 ways
a)using aws CDK
b)using Direct communication with API(consuming programming languages ,you can connect to EC2 API)
c)aws CLI
e)aws Cloud Formation template
d)using terraform
This are the ways to connect to your EC2 ibstance..But here we are going to discuss about the aws CLI (option c)

Step 2:
-- Login to your aws account and create the EC2 instance ,you can name your instance ,choose the OS, and have to choose SSH key-value pairs to generate (it is used to login into your VM) and choose the extension as .pem (you have to keep your .pem file safe) and fill all necessary details that are applicable to free tier and create instance ,after some seconds,you can see that your instance is running!

Step 3:
-- Here you have to click your account located on the top right corner,there you have to choose an option SECURITY CREDENTIALS,then you have to scroll down on the page and have to give give generate access keys.You will be generated with access keys and secret access keys.Again here you have to keep this keys safe .

Step 4: #Connecting using CLI
-- Here, goto your CLI(ubuntu or CentOS (any distribution of linux systems) ).Follow the commands below ..
>> sudo -
>> Enter your password for root account
>> mkdir "Directory_name" (creating a new directory to store p.pem file)
>> cd "Directory_name"
>> vi "file_name.pem"
>> press the i(to insert the contents) paste the downloaded .pem contents into this file ,after that press ESC ,press Shift + : ,w,q (your contents will be saved )

If you don't know the exact location of a current file ,give
find / -name "your_file_name"
This command is to login into your VM
ssh -i filepath hostname@publicipV4_address
aws configure
you will be prompted for inputs (-> Give the access key in first prompt -> Give secret access key in second prompt -> Give the region name and press Enter)
To check whether you are connected or not you can give commands like (aws s3 ls)
you get some list of names of your aws s3 bucket ,you are connected to aws instance

[Access keys are used for programmatic access into your account(used for managing resources)]

Step 5:
Then ,what's next ,you're successfully connected !!
<< Happy Journey>>

NOTE: if you get some error like .pem file is too open(permissions are too open) ,you can change permisiion by giving command
chmod 600 -your_path_to_the _.pem_file )

Top comments (0)