Day 16: How to connect to EC2 Instance from UI & Terminal
AWS CLI guide | Connecting EC2 Instance from UI & Terminal | AWS CFT WalkThrough
We have discussed how to create Virtual Machines or AWS EC2 instances manually, and we have used the AWS Console to create the instances. We need to connect the instance with the local machine.
Logging with the EC2 Instance
The manual way of Logging in :
One of the ways to create an EC2 Instance is by using the AWS Console.
Go to the list of EC2 instances running, click the instance you need to login/connect with
Click 'connect' to see a terminal or CLI to put your commands and tinker with the instances.
Create a txt file and try to add some text to it.
The problem is that this step is manual and AWS CLI UI-specific, which every user may not use. The session for AWS CLI UI won't last long, losing your instructions, so it is not an efficient way to connect the instance.
Connecting the instance with the local terminal / local machine :
To connect an EC2 instance with your local machine/ terminal, we need first to get the public IP for the instance. You get it by clicking on the instance in the EC2 Dashboard.
Get to the terminal to connect the EC2 instance, use the following command
ssh ubuntu@<IP address copied>
we have used 'ubuntu' in the command as I have created an ubuntu instance
At first, it would decline the connection request
In the previous blog, we discussed the creation of the key-value pair at the time of the creation of the EC2 instance. We understand that this pair acts as login credentials for the instance.
We need to use them now. (make sure that the key-value pair is downloaded as a .pem file on your local machine)
ssh -i <absolute path of the .pem file> ubuntu@<IP address copied>
This step will give you an error, saying that the permissions are too open; what does it mean?
It says that the permissions related to the pem file are too open, saying that it is vulnerable to logins by other users too. We need to change the permissions related to the pem file.
Use the following command to change the permissions related to the file, then try logging in with the previous command. It will be a successful login.
chmod 600 <absolute path for the pem file>
Now, to check if this is the same instance we have created. Try checking the text file you have created previously.
Stopping and terminating the instance
It is a good practice to stop the instance when you are done working with the instance, as AWS may charge considering the running of the instance.
Stopping doesn't mean you are deleting the instance; stopping is just a temporary switch-off for the instance, which you can reactivate and use again.
To permanently delete it, we terminate the instance.
Creating the AWS Resources with an automation process
There are different ways to create AWS resources with an automation process. A few important ones are the following
AWS CLI (Command Line Interface)
AWS CLI will help you interact with the AWS APIs and create the resources provided by AWS
To use AWS CLI, you first need to install it, Click here to find the instructions
Check if the AWS CLI is installed by the following command; if you find a list of resources, it means the AWS CLI is installed elsewhere; you have to see through the previous steps.
aws version
Installation won't do anything for us; this is just a binary file. We need to authenticate with the AWS CLI to use it.
For this, we first need to get the user's security credentials. Go to AWS Console, click on your profile, and click on security credentials. Scroll down and create access keys; make sure that you copy and store them somewhere safe or download a CSV file to store the security credentials.
Note that the secret access key can be viewed or downloaded. You cannot recover it later. However, you can create a new access key at any time.
Use the following command to authenticate with AWS CLI, then enter both the security credentials and press enter for other fields.
aws configure
Now you are authenticated with the AWS, the commands you enter through your terminal will be reflected in the AWS console.
Task: Let us create an EC2 instance through AWS CLI and list them
Create the task and make a PR to the linked GitHub Repository at the end of the blog, for the marking of the task completion.
AWS CFT (Cloud Formation Template)
AWS CFT is another way to interact with the AWS APIs.
Go to the AWS CFT in the AWS Console
You can create the instances by the template, or yourself can customize it
Task : Create EC2 Instance with CFT Template.
Script Automation using AWS API
Script can be used to write the code for the creation of resources.
For example, using Python BOTO3 Module to create the EC2 Instances
Task : Write the code for the creation of S3 Buckets and execute it.
In the last section, we haven't deep-dived into the creation of resources with automation, as this will be discussed in the further blogs under Infrastructure as Code.
Please find the codes and assignments for the DevOps learning in the GitHub Repository. Please add your feedback here in the comments.
Stay tuned for the upcoming blogs and exciting algorithms. Stay learning.