AWS EC2 Hands-On | A Step-by-Step Guide

Step-by-Step Guide to Launching Your First Virtual Server on AWS

AWS EC2  Hands-On | A Step-by-Step Guide


Amazon Elastic Compute Cloud (EC2) is a fundamental service in Amazon Web Services (AWS) that lets you rent virtual servers for running your applications. Knowing how to use EC2 is crucial for anyone wanting to harness the benefits of cloud computing for their projects or businesses.

This guide will walk you through the essential steps of working with EC2, such as creating key pairs for secure access, launching virtual servers, and managing security groups.

You'll also discover how to SSH into your instances, connect using AWS EC2 Instance Connect, and attach IAM roles for better security and control. By the end of this guide, you'll have a solid grasp of EC2 and feel confident deploying and managing your own cloud infrastructure.

Creating Key-Pair

Step 1. Signing in to AWS Console

  1. Open your web browser and navigate to the AWS Management Console.

  2. Sign in using your AWS account credentials.

Step 2. Accessing EC2 Dashboard

  1. Once logged in, locate the "Services" dropdown menu at the top left corner of the console.

  2. From the dropdown, select "EC2" under the "Compute" category.

Step 3. Creating a Key Pair

  1. Before launching your instance, you need to create a key pair for secure SSH access.

  2. In the EC2 dashboard, navigate to "Key Pairs" under the "Network & Security" section in the left sidebar.

  3. Click on the "Create Key Pair" button.

  4. Provide a name for your key pair, and select the file format (typically PEM).

  5. Click "Create Key Pair" and save the private key file (.pem) to a secure location. This key will be used to connect to your instance via SSH.

Launching EC2 Virtual Server

Step 1. Launching a New Instance

In the EC2 dashboard, click on the "Launch Instance" button

Step 2. Choose an Amazon Machine Image (AMI)

Select an appropriate AMI based on your requirements, such as Amazon Linux, Ubuntu, or Windows Server.

Step 3. Choose an Instance Type and Key Pair (login):

Select the instance type based on your workload requirements (e.g., t2.micro for a basic test). and key pair login from dropdown.

Step 4. Configure network settings and Security Group

  1. Set parameters such about security group and rules for what traffic to allow

  2. Define firewall rules by creating or selecting an existing security group to control traffic to your instance.

Step 5. Configure Storage

  1. Specify the size and type of storage volume for your instance.

  2. Add Tags: Optionally, add tags to your instance for better organization and management.

Step 6. Add User Data Script

  1. Go to Advance Settings and all the way to the bottom paste user Data.

# Update the package repository and install Apache
yum update -y
yum install -y httpd

# Start the Apache service
service httpd start

# Enable Apache to start on system boot
systemctl enable httpd

# Create a simple HTML page to verify Apache installation
echo "<html><head><title>Test Page</title></head><body><h1>Apache is running!</h1></body></html>" > /var/www/html/index.html

Step 7. Review and Launch

  1. Review Instance Launch: Review the configuration details of your instance.

  2. Click "Launch" to initiate the instance creation process.

Step 8. Accessing and Managing Your Instance

  1. Once your instance is launched, you can monitor its status from the EC2 dashboard.

  2. To access your instance, click on the "Instances" link in the navigation pane, then select your instance from the list.

  3. From here, you can view details about your instance, including its public IP address and status.

  4. To test the running instance go to http://<IP_address> or click of public IP above, just ensure it is http:// and not https://

  5. To start, stop, or terminate your instance:

    • Start: Select your instance, click on the "Actions" dropdown menu, and choose "Instance State" > "Start".

    • Stop: Similarly, select your instance, click on the "Actions" dropdown menu, and choose "Instance State" > "Stop".

    • Terminate: Be cautious as this action will permanently delete your instance and its associated data. Select your instance, click on the "Actions" dropdown menu, and choose "Instance State" > "Terminate". Confirm the termination when prompted.

      Note: Once you Stop and restart the server, the public IP changes, though private IP remains the same

Congratulations! You have successfully launched your first virtual server on AWS EC2 and learned how to manage it using the AWS Console. Remember to manage your instances responsibly to avoid unnecessary costs and ensure security

Security Groups

Step 1. Create a New Security Group

  1. In the EC2 dashboard, locate and click on "Security Groups" in the left-hand sidebar under the "Network & Security" section.

  2. Click on the "Create Security Group" button.

  3. Provide a name and description for your security group.

  4. Define inbound and outbound rules based on your requirements. You can specify protocols, ports, and IP ranges for both inbound and outbound traffic.

  5. Click on the "Create" button to create your new security group.

Step 2. Associate Security Group with an Instance

  1. Select the newly created security group from the list.

  2. Click on the "Actions" dropdown menu and choose "Edit inbound rules" or "Edit outbound rules" to modify the rules if needed.

  3. Once satisfied with the rules, navigate to the "Instances" tab in the bottom panel.

  4. Select the instance(s) you want to associate with the security group.

  5. Click on the "Actions" dropdown menu and choose "Security" > "Change Security Groups."

  6. Select and add the newly created security group and click "Save."

Step 3. Test Security Group Configuration:

  1. Access the instance associated with the security group.

  2. Test inbound and outbound connectivity based on the configured rules.

  3. Ensure that traffic is allowed or denied as per your defined rules.

Step 4. Monitor and Manage Security Groups:

  1. Regularly review and update your security group rules as needed.

  2. Utilize AWS CloudTrail and Amazon CloudWatch for monitoring and logging security group activity.

  3. Consider implementing automated processes for managing security groups, such as AWS Lambda functions or AWS Config rules.

Step 5. Delete or Modify Security Groups:

  1. If no longer needed, you can delete security groups by selecting them and clicking on the "Actions" dropdown menu, then choosing "Delete Security Group."

  2. Modify existing security groups by selecting them and clicking on the "Actions" dropdown menu, then choosing "Edit inbound rules" or "Edit outbound rules" to make adjustments.

How to SSH

Step 1. Retrieve Key Pair

If you haven't already, create or select an existing key pair. You may refre to section Creating Key-Pair above

Or follow to download existing key-pair

  1. Select Key Pairs: In the EC2 Dashboard, select "Key Pairs" from the navigation pane under "Network & Security."

  2. Locate Key Pair: You'll see a list of existing key pairs. Locate the key pair associated with your EC2 instance.

  3. Download PEM Key: Select the key pair, and then click the "Download Key Pair" button. This will download the PEM key file (with a .pem extension) to your local machine.

  4. Save the PEM File: Save the downloaded PEM file to a secure location on your computer.

Step 2. Retrieve your Public IP

  1. Locate your EC2 instance: In the EC2 Dashboard, you'll see a list of your instances. Click on the instance for which you want to find the public IP address.

  2. View Public IP: Once you've selected your instance, you should see its details displayed in the lower panel. Look for the "IPv4 Public IP" field. This field will display the public IP address assigned to the EC2 instance.

  3. Copy the Public IP: You can click on the copy icon next to the public IP address to copy it to your clipboard for easy access.

Step 3. Connect via SSH

  1. In security group of your instance, ensure Port 22 is open for SSH

  2. Open your terminal or command prompt.

  3. Change the permissions of your private key file to ensure it's secure:

     # Linux
     chmod 400 /path/to/your-key.pem
     # Windows
     icacls /path/to/your-key.pem /inheritance:r /grant:r '%USERNAME%:R'
  4. Connect to your EC2 instance using SSH. Replace your-key.pem with the path to your private key file and ec2-user with the appropriate username for your AMI (e.g., ubuntu, centos, admin, etc.):

     ssh -i /path/to/your-key.pem ec2-user@your-instance-public-ip

    If you're using a custom SSH port (not the default port 22), add the -p flag followed by the port number.

  5. You will be prompted to confirm the connection and authenticate using the private key.

  6. Once authenticated, you will gain access to your EC2 instance via SSH.

Step 4. Disconnect and Exit

  1. To disconnect from the EC2 instance, simply type:

  2. This will return you to your local terminal/command prompt.

EC2 Instance Connect Using AWS Management Console

Amazon EC2 Instance Connect provides a simple and secure way to connect to your EC2 instances using the AWS Management Console.

Step 1. Select Your Instance

In the EC2 Dashboard, select the EC2 instance you want to connect to by clicking on its instance ID.

Step 2. Choose "Connect" Option

In the instance details pane, click on the "Connect" button at the top.

Step 3. Select "Connect using EC2 Instance Connect"

  1. In the connect to instance dialog, select "Connect using EC2 Instance Connect" tab.

  2. Review the connection details and click on the "Connect" button.

Step 4. Access the Instance

You'll be connected to the EC2 instance using EC2 Instance Connect through your web browser. You can now perform any necessary tasks on the instance using the terminal provided.

Attaching IAM Role to EC2 Instance

Step 1. Creating or Locating an IAM Role:

  1. If you haven't created IAM roles yet, I recommend checking out the step-by-step guide "AWS IAM: Hands-On | A Step-by-Step Guide" at the link provided: AWS IAM: Hands-On | A Step-by-Step Guide. It offers clear instructions for setting up and using IAM roles effectively.

  2. We creates "TestingE2Role" role with "IAMReadOnly" policy attached and will be using this

Step 2. Navigate to EC2 Instance:

  1. From the services menu, select "EC2" under the "Compute" section.

  2. In the EC2 Dashboard, select the EC2 instance to which you want to attach the IAM role by clicking on its instance ID.

Step 3. Modify Instance IAM Role:

  1. In the instance details pane, click on the "Actions" dropdown menu.

  2. Select "Security" and then click on "Modify IAM role."

Step 4. Attach IAM Role:

  1. In the "Modify IAM role" dialog, select the IAM role you want to attach to the instance from the dropdown menu.

  2. Click on the "Save" button to attach the IAM role to the EC2 instance.

Step 5. Review and Test

  1. Once the IAM role is attached to the EC2 instance, it will have the permissions associated with that IAM role. Any AWS CLI or SDK commands executed on the EC2 instance will inherit the permissions of the attached IAM role.

  2. To verify that the IAM role is correctly attached and has the desired permissions, you can run AWS CLI commands or SDK calls from the EC2 instance.

    For example in EC2 Instance Connect Using AWS Management Console, you can run the aws iam list-users command to list IAM users and verify that it works as expected.