AWS

How to Use EC2 in AWS?

In this blog, I will explain How to Use EC2 in AWS.

Amazon Elastic Compute Cloud (EC2) is a web service offered by Amazon Web Services (AWS) that provides resizable compute capacity in the cloud. EC2 allows you to create and manage virtual machines (known as instances) on AWS infrastructure. Here’s a step-by-step guide on how to use EC2 in AWS:

Sign Up for AWS

If you’re not already an AWS customer, sign up for an AWS account at https://aws.amazon.com/. You will need to provide payment information, but you can use the AWS Free Tier to get started with EC2 at no cost for the first 12 months.

Access the AWS Management Console

Once you have an AWS account, log in to the AWS Management Console using your credentials.

Select the Region

In fact, AWS has multiple data centers in different geographic regions. So, choose a region where you want to create your EC2 instances. The region you select will affect factors such as latency and data residency. When you log in to AWS Management Console, you will get the option for selecting the region in the top right corner as shown below.

Select a Region
Select a Region

Launch an EC2 Instance

In order to create an EC2 instance, follow these steps.

To begin with, in the AWS Management Console, go to the EC2 service by clicking on “Services” and then selecting “EC2” under “Compute.”

Select EC2 Service
Select EC2 Service

Then, click the “Launch Instance” button.

Click the Launch Instance Button to Launch an EC2 Instance
Click the Launch Instance Button to Launch an EC2 Instance

After that, choose an Amazon Machine Image (AMI). Basically, an AMI is a pre-configured template for your virtual machine. You can choose from various Linux and Windows AMIs. So, let us choose Amazon Linux AMI. Also, don’t forget to give a name to your EC2 instance.

EC2 Instance Name and AMI
EC2 Instance Name and AMI

Further, choose an Instance Type: Select the hardware configuration of your instance, including CPU, memory, storage, and network performance. The t2.micro instance is often used for free tier purposes.

Select EC2 Instance Type
Select EC2 Instance Type

Then, configure Instance Details. For this purpose, set instance details such as the number of instances, network settings, and user data (optional). After that, add Storage. Here you can configure the storage for your instance. Basically, you can add or modify the size and type of the root and additional volumes.

Storage Configuration for EC2
Storage Configuration for EC2

Next, assign key-value pairs to your instances for better organization.

Create a Key Pair to Access the EC2 Instance
Create a Key Pair to Access the EC2 Instance

So, provide the key pair name, and keep the default options selected.

Create a Key Pair for the Instance
Create a Key Pair for the Instance

When you click on Create Key Pair button, the corresponding pem file is downloaded.

Key Pair File
Key Pair File

Next, configure Security Groups. Define the rules that control inbound and outbound traffic to your instance. So, let us keep the default options.

Security Groups in AWS
Security Groups in AWS

Finally, our configuration for the EC2 instance is complete. Once again review the configuration settings, and then click “Launch.”

Launch the EC2 Instance
Launch the EC2 Instance

Our instance is running as shown in the instances tab.

A Running EC2 Instance
A Running EC2 Instance

When you select the instance, its details are visible.

Details of the Running EC2 Instance
Details of the Running EC2 Instance

Connect EC2 Instance Using SSH

In order to connect the EC2 instance through SSH, first download and install the PuTTY Key Generator. For the purpose of accessing your machine, you need a private key ppk file. The file which is downloaded earlier is a .pem file. In order to convert a pem file to a ppk file, we use the PuTTY Key Generator. Since we need a ppk file rather than a pem file, we need this tool. Here, we need to create or select an existing key pair to connect securely to your instance.

PuTTY Key Generator
PuTTY Key Generator

Open the PuTTY Key Generator, click on Load, and browse the pem file. Then, click on the Save private key and save the ppk file for connecting with your Linux instance.

Retrieve the private key
Retrieve the private key

Also, you need to install PuTTY to create a connection with the EC2 instance using SSH. So, after installing, open your putty, copy the host name as public IPv4 DNS address, and click on SSH -> Auth -> Credentials, and under auth browse the ppk file. You will be able to connect with your Linux instance.

Public IPv4 Address of the Instance
Public IPv4 Address of the Instance
Browse the ppk Key
Browse the ppk Key

Now you can log in as an ec2-user by providing the passphrase that you have given in the PuTTY Key Generator.

Connect with the EC2 Instance
Connect with the EC2 Instance

Manage Your EC2 Instances

You can start, stop, terminate, and perform other management actions on your EC2 instances from the AWS Management Console. Let us create a directory before stopping the instance.

Access EC2 Instance Using SSH
Access EC2 Instance Using SSH

Then, stop the instance.

Stopping the Instance
Stopping the Instance
Disconnecting EC2 Instance
Disconnecting EC2 Instance

Now start the instance again. These options are available in the Instance State tab.

Starting the Instance Again
Starting the Instance Again

You can see that a new public IPv4 address is assigned this time.

New Public IPv4 Address Assigned
New Public IPv4 Address Assigned

Once again connect using PuTTY.

Navigate to the folder that you have created earlier.

Access the EC2 Instance Again
Access the EC2 Instance Again

Lastly, terminate the instance.

Terminate Instance
Terminate Instance

Conclusion

In conclusion, using Amazon EC2 in AWS provides flexibility, scalability, and the ability to run virtual machines in the cloud to meet your computing needs. Be mindful of costs and security while working with EC2 instances, and always follow AWS best practices and recommendations


Further Reading

JUnit Tutorial

What are Spot Instances in AWS?

How to Create Permission Policies in AWS?

Boto3 and its Features

How to Create an AWS Spot Instance?

GetObject and PutObject Permissions in Amazon S3

Features of AWS Lambda

Which Front End Technology is Better: Angular or React?

20+ Interview Questions on Go Programming Language

100+ MCQs On Java Architecture

How to Deploy a Machine Learning Model?

Java Practice Exercise

How to Read Data from Amazon DynamoDB using AWS Lambda?

An Example of Auto Scaling Using AWS Lambda

How to Create a Web Application Using a Trained Machine Learning Model?

programmingempire

Princites

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *