DEV Community

Cover image for How to Create an EC2 VM in AWS (Step-by-Step Guide for Beginners)
Ibrahim Bio Abubakar
Ibrahim Bio Abubakar

Posted on

How to Create an EC2 VM in AWS (Step-by-Step Guide for Beginners)

Amazon EC2 (Elastic Compute Cloud) is one of the most popular services in AWS, allowing you to launch and manage virtual machines in the cloud. In this guide, we’ll walk through creating a Demo EC2 instance using the Amazon Linux AMI and a t2.micro free-tier instance type. We’ll also add a simple user data script to run on startup.


Prerequisites

  • An AWS account
  • Basic familiarity with AWS Management Console

Step 1: Log in to AWS Management Console

  1. Head over to AWS Console.
  2. From the Services menu, search for EC2 and click on it.

Image 2


Step 2: Launch an Instance

  1. Click Launch instance.

Image 2

  1. Give your instance a name: Demo EC2.

Image 3


Step 3: Choose Amazon Machine Image (AMI)

  1. In the Application and OS Images section, choose:
  • Amazon Linux (Free Tier eligible).

Image 4


⚡ Step 4: Select Instance Type

  1. From the Instance type dropdown, select:
  • t2.micro (Free Tier eligible).

Image 5


Step 5: Configure User Data (Startup Script)

  1. Scroll down to Advanced details.
  2. In the User data section, paste the following script:
   #!/bin/bash
   echo "Hurray!!! We got this working" > /home/ec2-user/startup.txt
Enter fullscreen mode Exit fullscreen mode

Image 6

This script will create a file named startup.txt with the message inside.


Step 6: Configure Key Pair

  1. Either select an existing key pair or create a new one.
  2. This will allow you to SSH into your instance later.

Image 7


Step 7: Configure Network Settings

  1. Choose default VPC and subnet (unless you want custom networking).
  2. Allow SSH traffic from your IP.
  3. Allow HTTP traffic from the internet.

Image 8


Step 8: Launch the Instance

  1. Review all settings.

Image 9

  1. Click Launch instance.

Your Demo EC2 instance will now spin up in the cloud!

Image 10


Step 9: Verify User Data Script

  1. Once the instance is running, select connect for EC2 Instance Connect.

Image 11

  1. Ensure the Connection type is set to Connect using a Public IP, Puplic IPv4 address is selected and the Username is ec2-user; then select Connect.

Image 12

We have now successfully connected to the Amazon Linux 2023 machine:

  1. Run:
   cat /home/ec2-user/startup.txt
Enter fullscreen mode Exit fullscreen mode

  1. You should see:
   Hurray!!! We got this working
Enter fullscreen mode Exit fullscreen mode

Conclusion

In just a few steps, you’ve launched your first Amazon EC2 instance using Amazon Linux on a t2.micro free-tier machine. With the user data script, we confirmed automation works right at startup.

Next steps: Try customizing the user data script to install software packages, run web servers, or deploy applications automatically.


Top comments (0)