Public Cloud

step-by-step guide to create a website on Amazon Linux AMI on AWS

Damian Igbe, Phd
Sept. 6, 2024, 5:20 p.m.

Subscribe to Newsletter

Be first to know about new blogs, training offers, and company news.

Here are the steps in this project:

  1. Create a key pair
  2. Create an EC2 instance
  3. Connect to the EC2 instance
  4. Install Apache Webserver using Linux commands from the CLI (Command Line Interface)
  5. Clone the website from Git Repositor
  6. Access your website from the web browser
  7. Create AMI (Amazon Machine Image)

Step 1: Create a key pair

Creating key pairs on AWS is essential for securely accessing your EC2 instances. Here's a step-by-step guide on how to create key pairs in AWS:
 
1. Log in to the AWS Management Console
- Go to the [AWS Management Console](https://aws.amazon.com/console/).
- Log in with your credentials.
 
2. Navigate to the EC2 Dashboard
- In the AWS Management Console, find and select EC2 from the list of services. This will take you to the EC2 Dashboard.

3. Open the Key Pairs Section

- In the left-hand menu of the EC2 Dashboard, find and click on Key Pairs under the Network & Security section.

4. Create a New Key Pair

- Click the Create key pair button.

5. Configure Key Pair Settings

- Name your key pair: Enter a name for your key pair in the Key pair name field. Choose a descriptive name that helps you remember its purpose.

- Select the key pair type: Choose between RSA or  ED25519. RSA is more widely used, but ED25519 is a newer and more secure option. If you’re unsure, RSA is a safe choice.
- Select the file format:
  - PEM for use with OpenSSH or PuTTY.
  - PPK if you are using PuTTY on Windows.
 
 6. Download the Key Pair
- Click the Create key pair button to generate the key pair.
- Your browser will prompt you to download the key file (`.pem` or `.ppk` depending on your selection). Download and save this file securely. AWS will not keep a copy of this key, and you will need it to access your EC2 instances.
 
7. Store the Key Pair Securely
- Ensure you store the downloaded key file in a secure location. Losing this file means you will not be able to access your EC2 instances associated with this key pair.
 
8. Use the Key Pair
- When launching a new EC2 instance, you will be prompted to select an existing key pair. Choose the key pair you created to access your instance securely.
 
Additional Notes
- Permissions: Make sure the key file permissions are set correctly (e.g., `chmod 400 your-key.pem` on Unix-based systems).
- Backup: Consider creating a backup of the key pair in a secure location to prevent loss of access.
 
That’s it! You’ve successfully created a key pair on AWS.

 

Step 2: Create an EC2 instance with Amazon Linux AMI

Here’s a step-by-step guide to create an EC2 instance with Amazon Linux AMI on AWS:
 
1. Sign In to AWS Management Console
   - Go to [AWS Management Console](https://aws.amazon.com/console/).
   - Sign in with your AWS account credentials.
 
2. Navigate to EC2 Dashboard
   - In the AWS Management Console, type "EC2" in the search bar and select "EC2" to open the EC2 Dashboard.
 
3. Launch Instance
   - Click on the “Launch Instance” button.
 
4. Choose an Amazon Machine Image (AMI)
   - You’ll be taken to a page where you select an AMI.
   - Under the “Quick Start” tab, find the "Amazon Linux" AMI. There are usually two types: Amazon Linux 2 and Amazon Linux AMI. Choose Amazon Linux 2 if you want the latest version.
 
5. Choose an Instance Type
   - Select the instance type based on your needs (e.g., `t2.micro` for a free tier eligible instance).
   - Click “Next: Configure Instance Details”.
 
6. Configure Instance Details
   - Configure the settings as needed. For most cases, the default settings are sufficient. You can specify things like the number of instances, network, and subnet.
   - Click “Next: Add Storage”.
 
7. Add Storage
   - Configure the storage settings. The default volume size and type are usually sufficient, but you can modify them if needed.
   - Click “Next: Add Tags”.
 
8. Add Tags
   - (Optional) Add tags to help you manage your instance. For example, you could add a “Name” tag with a value like “MyAmazonLinuxInstance”.
   - Click “Next: Configure Security Group”.
 
9. Configure Security Group
   - Define rules to control access to your instance. For example, you might add a rule to allow SSH access (port 22) from your IP address.
   - Click “Review and Launch”.
 
10. Review and Launch
   - Review your settings. If everything looks good, click “Launch”.
 
11. Select Key Pair
   - You will be prompted to select an existing key pair or create a new one. This key pair is used to securely connect to your instance via SSH.
   - Select an existing key pair or create a new one, and then acknowledge that you have access to the selected key pair.
   - Click “Launch Instances”.
 
12. Access Your Instance
   - Once the instance is launched, you’ll be redirected to the “Instances” page. You should see your new instance in the list.
   - Select your instance, and note its Public IP address or DNS name.
     
That’s it! You’ve successfully created and launched an EC2 instance with Amazon Linux AMI.

 

Step 3: Connect to the EC2 instance from the Management console

To connect to a Linux EC2 instance from the AWS Management Console, follow these steps:
 
1. Log in to AWS Management Console:
   - Go to the [AWS Management Console](https://aws.amazon.com/console/).
   - Sign in with your credentials.
 
2. Navigate to the EC2 Dashboard:
   - In the console, search for and select EC2 from the services menu.
 
3. Select Your Instance:
   - In the EC2 dashboard, click on Instances in the left-hand menu.
   - Find and select the instance you want to connect to.
 
4. Get the Instance Details:
   - Ensure your instance is in the running state.
   - Click on the instance to view its details.
 
5. Connect to the Instance:
   - Click on the Connect button at the top of the page.
   - A pop-up window will appear with connection options.
 
6. Use EC2 Instance Connect (Browser-Based):
   - In the Connect pop-up, select the EC2 Instance Connect tab.
   - Ensure you have the required permissions to use EC2 Instance Connect. 
   - Click the Connect button. This will open a new browser-based terminal session.

 

Step 4: Install Apache Webserver using Linux commands from the CLI (Command Line Interface)

 Here are step-by-step instructions to install Apache on an Amazon EC2 instance running Amazon Linux:

1. Perform the following  from  browser-based terminal session(CLI)  in the previous step  (step 3)
 
2. Update Package Repository
   - Update the package repository to ensure you have the latest package lists:
 
sudo yum update -y
 
3. Install Apache HTTP Server
   - Install the Apache HTTP server using the `yum` package manager:
    
sudo yum install httpd -y
     
4. Start Apache Service
   - Start the Apache service:
     
sudo systemctl start httpd
     
5. Enable Apache to Start on Boot
   - Configure Apache to start automatically when the instance boots:
     
sudo systemctl enable httpd
     
6. Adjust Firewall Rules (Security Group)
   - Make sure the EC2 instance’s security group allows HTTP (port 80) traffic:
     - Go to the [EC2 Management Console](https://aws.amazon.com/console/).
     - Navigate to “Instances” and select your instance.
     - In the “Description” tab, find the “Security groups” and click on the security group link.
     - Edit the inbound rules to allow HTTP traffic on port 80:
       - Type: `HTTP`
       - Protocol: `TCP`
       - Port Range: `80`
       - Source: `0.0.0.0/0` (or specify a more restricted IP range if desired)
 
7. Verify Apache Installation
   - Open a web browser and navigate to your instance’s public IP address or DNS name.
   - You should see the Apache test page, which confirms that Apache is installed and running correctly.
 
8. (Optional) Modify the Apache Default Page
   - You can edit the default Apache page by modifying the `/var/www/html/index.html` file:
     
   sudo nano /var/www/html/index.html
     
   - Make your changes, save the file, and refresh your browser to see the updates.
 
That’s it! You’ve successfully installed and started Apache on your Amazon EC2 instance.

 

Step 5: Clone the website from Git Repository

Here’s a step-by-step guide to clone a repository from GitHub:
 
1. Install Git (if not already installed)
 
sudo yum update -y
sudo yum install git -y
   
2. Clone the Repository
 
   - Run the `git clone` command followed by the URL you copied:
    
     
3. Copy the repo files to the location where Apache webserver will server the files 
 
   sudo cp edukate/* /var/www/html/
     
4. Verify that the edukate website is running in the next step
 
Additional Commands (Optional)
 
   - Check Git Status: To see the status of the repository.
 
git status
     
   - View Git Logs: To view commit history.
 
git log
     
   - Update Your Local Repository: To pull the latest changes from the remote repository.
 
git pull
     
 
That’s it! You’ve successfully cloned a repository from GitHub.

 

6. Access the website from the browser 

 
To access a webpage from an Amazon EC2 instance, follow these steps:
 
1. Ensure Web Server is Running on the EC2 Instance. This step is optional for this lab because its already done in step 4.
 
   - Install a Web Server (e.g., Apache):
 
sudo yum install httpd -y
     
   - Start the Web Server:
 
sudo systemctl start httpd
     
   - Enable the Web Server to Start on Boot:
   
sudo systemctl enable httpd
     
2. Configure Security Group. This step is optional for this lab because its alraedy done in step 4.
 
   - Allow HTTP Traffic:
     - Go to the [EC2 Management Console](https://aws.amazon.com/console/).
     - Navigate to “Instances” and select your instance.
     - In the “Description” tab, find the “Security groups” and click on the security group link.
     - Edit the inbound rules to allow HTTP traffic:
       - Type: `HTTP`
       - Protocol: `TCP`
       - Port Range: `80`
       - Source: `0.0.0.0/0` (or restrict it to specific IP ranges if desired)
 
3. Verify the Public IP or DNS
 
   - Find the Public IP or DNS Name:
     - In the EC2 Management Console, select your instance and look for the “Public IP” or “Public DNS” under the instance details.
 
4. Access the Webpage from Your Local Machine
 
   - Open a Web Browser:
     - Type the Public IP or Public DNS name of your EC2 instance into the address bar of your web browser.
 
5. (opional) Troubleshooting, if the above step does not work.
 
   - Check Web Server Status:
 sudo systemctl status httpd
   - Verify Security Group Rules:
     Make sure that HTTP traffic (port 80) is allowed in the security group associated with your EC2 instance.
   - Check Network ACLs and VPC Settings:
     Ensure that Network ACLs or VPC settings are not blocking HTTP traffic.
 
By following these steps, you should be able to access the webpage served by your EC2 instance from a web browser on your local machine.

 

Step 7: Create AMI (Amazon Machine Image)

Creating an AMI from an EC2 Instance in AWS: A Step-by-Step Guide

In the world of cloud computing, Amazon Web Services (AWS) offers a powerful feature called Amazon Machine Images (AMIs) that lets you create snapshots of your EC2 instances. This guide walks you through the process of creating an AMI from an EC2 instance, which can be useful for backup purposes, scaling, or replicating environments.

Why Create an AMI?

Creating an AMI from an EC2 instance allows you to:

  1. Backup: Preserve the state of your instance, including its configuration, applications, and data.
  2. Scaling: Quickly launch new instances with the same configuration.
  3. Replicate: Deploy identical environments across different regions or accounts.

Step-by-Step Process

1. Prepare Your EC2 Instance

Before creating an AMI, ensure your EC2 instance is in the desired state:

  • Clean Up: Remove any temporary files or data that you don’t need.
  • Update Software: Ensure that the instance is up-to-date with the latest patches and updates.
  • Stop Services: Optionally, stop any services or applications that might be writing data to ensure consistency.

2. Open the EC2 Management Console

  1. Log in to your AWS Management Console.
  2. Navigate to the EC2 Dashboard by selecting EC2 from the Services menu.

3. Select Your Instance

  1. In the Instances section, find and select the instance you want to create an AMI from.
  2. Make sure the instance is in the desired state (running or stopped). It’s often recommended to stop the instance to ensure a consistent image, though this is not always required.

4. Create the AMI

  1. With the instance selected, click the Actions button.
  2. Choose Image and templates and then select Create image.

    • Image name: Enter a descriptive name for your AMI.
    • Image description: Optionally, provide a description to help identify the AMI later.
    • No Reboot: Decide if you want to allow or prevent rebooting the instance. By default, AWS reboots the instance to ensure a clean state. If you select "No Reboot," the AMI creation will proceed without restarting the instance, but this may affect the consistency of the image.
  3. Click Create image. AWS will start the process of creating the AMI.

5. Monitor the AMI Creation

  1. Go to the AMIs section under Images in the EC2 Dashboard.
  2. You will see your new AMI listed with a status of Pending. The AMI status will change to Available once the creation process is complete.

6. Use Your AMI

Once your AMI is available, you can:

  • Launch New Instances: Use the AMI to launch new EC2 instances with the same configuration.
  • Share AMI: Make your AMI available to other AWS accounts or regions by modifying its permissions.
  • Create Launch Configurations: Use the AMI in auto-scaling groups or other deployment scenarios.

Best Practices

  1. Regular Backups: Regularly create AMIs to keep your backups up-to-date.
  2. Monitor AMI Usage: Keep track of your AMIs and delete those that are no longer needed to manage storage costs effectively.
  3. Document AMIs: Maintain documentation on what each AMI contains, including any specific configurations or software versions.

Conclusion

Creating an AMI from an EC2 instance is a straightforward process that offers flexibility and reliability for managing your cloud infrastructure. By following these steps, you can efficiently create, use, and manage AMIs, ensuring your environments are easily replicable and recoverable.

Congratulations, you have been able to deploy a website on Amazon Linux EC2 instance and created an AMI from the instance. We can use the AMI in other projects without having to perform all the above steps each time. 

Note that this website has no database backend but we will have other projects with the data base integrated.

Zero-to-Hero Program: We Train and Mentor you to land your first Tech role