Open In App

Securing EC2 Instances With Security Groups And Key Pairs

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Amazon provides a service for website hosting and maintenance that is EC2, Elastic Cloud instance. They are the backbone of cloud-based applications, providing scalability and flexibility in computing resources. However, there are many challenges in protecting these instances from threats and attacks. Whenever we create an EC2 instance we need to take care of its security at the top level for which we have Security groups and key pairs to discard any unauthorized access to our instance.

Understanding Of Primary Terminologies

  • EC2 Instances: These are the cloud platform services given by was to host data and run applications on the cloud. It's a virtual server in the cloud that gives its users an expandable computing capacity. These instances can easily be configured and scaled based on user requirements and specifications.
  • Security Group: A Security Group is a virtual firewall for an EC2 instance. It controls inbound and outbound traffic and sets the rules for the same. Each instance has its own Security group and works for its instance on the instance level.
  • Key Pairs: A key pair is a security credential used for secure access to your EC2 instance. They have both public and private keys which are generated together. These pairs are primarily used for SSH access to Linux-based instances or RPD access to Windows-based instances.

Securing Instance With Security Groups And Key Pair: A Step-By-Step Guide

Go through the following steps to create a key pair launch your instance and create a security group as specified in the step below. You can also create the security group before launching the instance or you can do so during the launch of the instance.

Step 1: Login To the AWS Console

  • Navigate to AWS Console and log in with your credentials from the root user.
Sign in AWS with root user

Step 2: Navigate To EC2 Dashboard

  • From the AWS console, Navigate to your EC2 instance service.
Click on your EC2 instance.

Step 3: Configuring Instance With Details

  • configure your instance with following details and launch it using following steps:
  • Click on launch instance button to start with configuring your instance.
  • Name the instance as per your choice and requirement.

launching an instance

  • Select a machine image for your instance. If you want to have free tier service go with available t2 micro free tier service in Amazon Machine image.
Selecting AMI
  • From the launch instance window go to create key pair option and create a new key pair using following steps given in next step.

Step 4: Creating A AWS Key Pair

  • Click on the create key pair option and yo will get a pop up window to create your Key pair.
  • Select the name of your key pair as per your choice and select .pem if you will perform SSH login. After filling in the detail click on the create key pair button.

Creating AWS Key Pair

  • After creating your key pair click on the launch instance and wait for some tile till your instance gets into running state.

Step 5: Configuring Network Security Groups

  • After your instance gets launched go for security group creation and set inbound and outbound rule for it.
  • Click on the create security group option and fill in the required details about the security group.

Creating Security Groups

  • Setup the inbound and outbound rules for your security group.
Choosing inbound and outbound rules
  • After setting up rules for your security group click on the create Security group option.

Step 6: Connect To EC2 Instance With SSH Connection

  • After creating our security group we need to associate our security group with your launched instance.
  • To select your Security group select your instance, go to action button.In action button click on the security option and select change security group.

Connecting to EC2 Instance With SSH

  • Select the Security group that we created in previous step and associate it with your instance.

Step 7: Login Remotely From Local System

  • Use SSH login method to connect to your EC2 instance. Use the private key of the key-pair that we get while we created our KEY-PAIR.
  • Specify the public address in your console during SSH connnection. Use the following code formate to do so:
 ssh -i path/to/private-key.pem ec2-user@public-ip

Note: Here change the key file name and ip address with your values.

Connecting Instance Remotely

Conclusion

Securing your EC2 instance using key-pair and Security Groups is the fundamental step in securing your instance from unauthorused access and maintaining its integrity and confidentiality. Secure your cloud-based infrastructure in EC2 and secure your data and webpage hosted.By following above steps you can understand the concept of Security group and key pair and create one for your instance. Remeber not to share your private or public key of key-pair and if so happens delete the key pair and security group to avoid extra billing on your account.


Similar Reads