AWS Builder Center

How I Launched and Configured an EC2 Instance Using AWS CLI

In this hands-on guide, I share my experience of launching and configuring an Amazon EC2 instance using the AWS CLI. I’ll walk through the process from configuring AWS CLI and selecting an AMI to creating a Security Group, launching the EC2 instance, connecting through SSH, and running a web server.

Introuction
While learning AWS, I wanted to understand how EC2 instances can be created without using the AWS Management Console. So I decided to launch and configure an EC2 instance completely using the AWS CLI.
In this article, I’ll share what I did step by step, including configuring AWS CLI, creating a security group, finding an AMI, launching the instance, connecting to it through SSH, and finally running a simple web server.
This was a hands-on practice that helped me understand how different AWS components work together.
What is Amazon EC2?
Amazon EC2 (Elastic Compute Cloud) provides virtual servers in the AWS Cloud.
Instead of buying and maintaining a physical server, we can launch an EC2 instance whenever we need computing resources.
For this practice, I used an EC2 Linux instance and managed it using AWS CLI.
What I Used
For this hands-on exercise, I used:
AWS Account
AWS CLI
Linux/WSL
EC2
IAM
SSH
Security Groups
Step 1: Configure AWS CLI
First, I checked whether AWS CLI was installed on my system.
aws --version
After confirming that it was installed, I configured it using:
aws configure
I entered my AWS credentials and selected my preferred AWS Region.
Then I verified the configuration:
aws sts get-caller-identity
This command helped me confirm that the CLI was connected to my AWS account successfully.
Step 2: Find an AMI
Before launching an EC2 instance, I needed an AMI (Amazon Machine Image).
The AMI provides the operating system and other required information for the instance.
I selected an Amazon Linux AMI available in my region.
I then used the AWS CLI to work with the AMI ID that I wanted to use.
Step 3: Create a Security Group
Next, I created a security group for my EC2 instance.
aws ec2 create-security-group \
--group-name my-ec2-sg \
--description "Security group for my EC2 instance"
The security group acts like a virtual firewall for the EC2 instance.
For my testing, I allowed SSH access so that I could connect to the server.
I also opened HTTP when I wanted to test a web server.
Step 4: Launch the EC2 Instance
After preparing the required resources, I launched the EC2 instance using the run-instances command.
aws ec2 run-instances \
--image-id <AMI-ID> \
--instance-type t3.micro \
--key-name <KEY-NAME> \
--security-group-ids <SECURITY-GROUP-ID>
The command returned information about the newly created instance.
I then checked the instance status to make sure it was running.
aws ec2 describe-instances
Seeing the instance move into the running state was the first confirmation that everything was working correctly.
Step 5: Connect to the Instance
Once the instance was running, I retrieved its public IP address.
I then connected to the server using SSH:
ssh -i my-key.pem ec2-user@<PUBLIC-IP>
After successful authentication, I was inside my EC2 Linux server.
This was one of the most useful parts of the exercise because I could now manage the cloud server just like a normal Linux machine.
Step 6: Install a Web Server
To make the setup more practical, I installed a web server on the EC2 instance.
For example:
sudo dnf update -y
sudo dnf install nginx -y
Then I started Nginx:
sudo systemctl start nginx
And enabled it to start automatically after reboot:
sudo systemctl enable nginx
I checked its status:
sudo systemctl status nginx
After that, I opened the EC2 public IP address in my browser and verified that the web server was accessible.
What I Learned
This hands-on exercise helped me understand that launching an EC2 instance is not just about creating a virtual machine.
Several AWS components work together:
IAM → AWS CLI → AMI → Security Group → EC2 → SSH → Application
I also understood the importance of security groups. Opening ports without thinking about who can access them can create unnecessary security risks.
Another thing I learned was how useful the AWS CLI can be. Instead of manually clicking through the AWS Console, I can perform many tasks directly from the terminal.
Important Security Practices
While experimenting with EC2, I kept the following points in mind:
Don't use the AWS root account for regular operations.
Follow the principle of least privilege with IAM.
Don't expose SSH (port 22) to everyone in production.
Protect your .pem private key.
Only open the ports that are actually required.
Stop or terminate resources when they are no longer needed.
Monitor AWS usage to avoid unexpected charges.
Conclusion
This was my first practical experience launching and configuring an EC2 instance using AWS CLI.
Initially, working with commands instead of the AWS Console felt a little different, but after going through the process, I understood the workflow much better.
The biggest takeaway for me was that learning AWS becomes much easier when I actually build and configure things instead of only reading about them.
My next step is to automate more of this process using tools such as Terraform and GitHub Actions.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article