Athira KK
14 min readApr 15, 2024

EC2 Instance Launch Process and EBS Snapshot Management

Hey Techies…👋

The launch of an ec2 instance:

  • Choose an AMI (Amazon Machine Image): This is a template for the virtual machine you want to create. It contains the operating system, software, configuration, and sometimes data.
  • Select an Instance Type: This determines the computational resources (CPU, RAM, storage, etc.) of your EC2 instance. There are various types optimized for different use cases.
  • Configure Instance Details: This includes settings like networking (VPC, subnet), IAM role, and user data (scripts or data to be run at instance launch).
  • Add Storage: Configure the amount and type of storage for your instance. This is typically done using Amazon EBS (Elastic Block Store), which provides block-level storage volumes.
  • Add Tags: Assign metadata to your instance in the form of key-value pairs. This helps with organization, management, and billing.
  • Configure Security Group: Define firewall rules for controlling inbound and outbound traffic to your instance. Security groups act as virtual firewalls.
  • Review and Launch: Review your instance configuration, make any necessary changes, select an existing key pair or create a new one for SSH access (for Linux instances), and then launch the instance.

After launching the instance, you can access it using SSH (for Linux instances) or RDP (for Windows instances) using the key pair you selected or created. Remember to properly secure your instance and follow best practices for managing AWS resources.

Let’s walk through the process of launching an EC2 instance.

An EC2 instance essentially functions as a virtual machine.

Firstly, log in to your AWS console.

Next, choose your desired region. For instance, I’ll be selecting the North Virginia region. It’s advisable to select a region based on your current location, as some regions may incur higher costs compared to those in the US.

Each region is identified by a code, such as US East 1 for North Virginia.

To navigate to the EC2 service, click on “Services” and locate EC2 either by browsing through all services or by directly searching for EC2.

Once in the EC2 dashboard, you’ll find an overview of your instances, volumes, and other relevant information. Keep in mind that the dashboard doesn’t auto-refresh, so be sure to manually refresh after making any changes.

Scrolling down, you’ll notice zones within the region, each identified by a code like 1A, 1B, etc. Typically, a region will have at least two zones.

Click on “Instances” and then “Launch Instance” to initiate the instance creation process.

Before proceeding, it’s worth noting that the AWS console interface may evolve over time, but the core options and functionalities remain consistent.

Now, let’s begin by assigning a name and optional tags to the instance. Tags can be useful for filtering and organization purposes.

Moving on to selecting the Amazon Machine Image (AMI), you’ll find a variety of options including quick start AMIs like Amazon Linux, Ubuntu, and Windows, as well as community AMIs and those from the AWS Marketplace. Carefully review the details and charges associated with each AMI.

For this example, let’s choose the Ubuntu AMI provided by Amazon Web Services, which is free.

Ensure that the instance type selected aligns with your requirements. For free-tier usage, T2 micro instances are suitable for most learning purposes.

Proceed to configure the network settings, including security groups. Security groups act as mandatory firewalls for EC2 instances.

Customize the security group to allow necessary inbound and outbound traffic.

You can specify the storage size, typically defaulted to 10 GB, and further advanced details like user data, which allows for automated provisioning upon instance launch.

Finally, review your configuration and launch the instance.

Upon launching, the instance may take a few moments to become available. You can monitor its status in the EC2 dashboard.

Next, establish a connection to the instance; in this example, I’m using EC2 Instance Connect.

There are two methods to install Apache2:

  • By incorporating commands in the user data section before initiating the instance launch, as demonstrated below:
  • Or, by executing the commands after connecting the ec2 instance via terminal.
sudo apt update
sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl status apache2

Troubleshooting tip: If encountering connection issues, ensure the correct path to the SSH key or navigate to the directory containing the key before attempting to connect.

Once connected, you can verify the instance status, access web services like Apache, and configure firewall rules as needed to enable external access to your services.

Additionally, click on the Security Group, edit inbound rules, add the following rule, and save the rules. It’s important to note that allowing all IPs is solely for learning purposes and may vary depending on your project requirements.

Go to the browser and copy the public IP followed by ‘:80’ to view the Apache page.

***************************************************************************

Elastic IP

  • Powering Off the EC2 Instance:
  • Take note of the current public and private IP addresses of the instance.
  • Stop the instance using the AWS Management Console.
  • Observing IP Address Changes:
  • After stopping the instance, observe that the public IP address is released while the private IP address remains unchanged.
  • Refresh the console to verify the changes.

Elastic IP Benefits:

  • Elastic IP addresses offer the advantage of maintaining a static public IP address even in scenarios where EC2 instances are restarted or recreated.
  • They provide a stable endpoint for applications or services that require a consistent public IP address.
  • With Elastic IP, there’s no need to update DNS records or reconfigure applications each time an instance is stopped or replaced.
  • It simplifies infrastructure management and enhances reliability by ensuring uninterrupted connectivity to resources hosted on EC2 instances.
  • Elastic IP addresses are particularly useful for scenarios requiring a fixed public IP address, such as hosting websites, APIs, or VPN gateways.
  • Allocating an Elastic IP:
  • Allocate an Elastic IP address from the AWS pool.
  • Associate the Elastic IP with the desired EC2 instance.

EC2 Instance Components:

  • In addition to the EC2 instance itself, other components include the network interface and volumes (virtual hard disks).
  • The network interface manages connectivity and is associated with firewall rules, public IP addresses, and security groups.
  • Volumes represent the virtual hard disks attached to the instance, storing data and operating system files.
  • Understanding these components is essential for managing and configuring EC2 instances effectively.

Navigating the EC2 Dashboard:

  • Encourage exploration of the EC2 dashboard to view information about resources like key pairs, security groups, and volumes.

Adjusting Instance Settings:

  • Demonstrate how to change instance type and other settings like Elastic IP disassociation, network interface attachment/detachment, and security group changes.

Reverting Instance Changes:

  • Revert any instance changes made during the demonstration, such as changing the instance type, by powering on the instance and reverting the settings back to their original state.

Elastic Block Store (EBS)

EBS serves as the virtual hard disk for EC2 instances, offering both EBS volumes and snapshots for data storage and backup.

EBS Volumes:

  • EBS volumes, akin to physical hard disks, house the operating system data of EC2 instances and can store various types of data such as databases, web servers, and files.
  • During volume creation, users specify an availability zone, ensuring alignment with the instance’s location.
  • Data stored in EBS volumes is automatically replicated within the same availability zone, mitigating the risk of data loss due to local failures.

Snapshots:

  • Snapshots, used for backing up entire volumes, provide an additional layer of data protection.
  • These snapshots capture the state of the EBS volume at a specific point in time, facilitating data recovery and restoration.

Types of EBS Volumes:

  • EBS volumes come in various types catering to different workload requirements and performance needs.
  • General purpose volumes, SSD-based, are recommended for most workloads due to their balanced pricing and speed.
  • Provisioned IOPS volumes offer enhanced performance and are ideal for large databases requiring high input-output operations per second (IOPS).
  • Throughput optimized HDD volumes suit big data and data warehousing applications, while cold HDD volumes are cost-effective solutions for infrequently accessed data.
  • Magnetic volumes, the cheapest option, are suitable for backups and archives but offer lower performance.

Choosing the Right Volume Type:

  • The selection of an appropriate volume type depends on factors such as workload characteristics, performance requirements, and budget considerations.
  • AWS documentation provides detailed information and use cases for each volume type, aiding users in making informed decisions.

After establishing a connection to the instance, execute the following commands. Please note that these commands can also be added to the instance creation process in the user data area:

systemctl status apache2
cd /tmp
wget https://www.tooplate.com/zip-templates/2119_gymso_fitness.zip
unzip -o 2119_gymso_fitness.zip
cp -r 2119_gymso_fitness/* /var/www/html/
systemctl restart apache2

These commands are designed to check the status of the Apache web server, navigate to the temporary directory, download a ZIP file containing website templates from a specified URL, unzip the downloaded file, copy the contents to the Apache document root directory, and finally restart the Apache service to apply the changes.

Here, we can observe the available volume types for creating an EC2 instance.

Navigate to the Volumes section located on the left-hand side of the EC2 instances page and update the name of the volume associated with the instance mentioned earlier.

Adding extra storage to an EC2 instance, it’s crucial to consider the availability zone. Both the instance and the volume need to be in the same zone to ensure connectivity. When gathering requirements for additional storage, such as for web server images, it’s important to adhere to the free tier limit of 30 GB for EBS volumes. For instance, if there’s a need for five gigabytes of storage for web server images, it’s imperative to ensure that the total storage remains within the free tier limit.

Click on create volume:

Tags, you can give upon your wish, then click on Create Volume

Choose the created volume, navigate to actions, and then opt for attaching the volume once it appears as available on the dashboard.

Select the instance , device name and click on attach volume.

(Please note I’ve selected /dev/xvdf in above screenshot as Device name).

If we run the “fdisk -l” command, we could see the new volume listed:

fdisk -l

List Disks: Start by running the command fdisk -l to list all disks. For instance, /dev/xvdf represents the root volume with a size of 8 GB, and it has a partition /dev/xvdaf1.

  • Understand Partitions: Linux assigns partitioning numbers (e.g., xvda1) rather than names. Verify the root volume by checking its mount point using df -h. If /dev/xvda1 is mounted to the root directory, it contains the operating system data.
  • Create Partition: Identify the additional disk, such as /dev/xvdf, and use fdisk to create a partition. Choose the disk path (e.g., /dev/xvdf), follow the prompts to create a new partition, and write the changes using w.
fdisk -l

Format Partition: Use the appropriate formatting utility, such as mkfs.ext4, to format the partition. For example, run mkfs.ext4 /dev/xvdf1 to format the partition with ext4 format.

mkfs.ext4 /dev/xvdf1

Temporary Mount: Mount the partition temporarily using the mount command. For instance, mount /dev/xvdf1 /var/www/html/images. Verify the mount using df -h.

Create a directory: “ mkdir /tmp/image_backups”, and move all data from Images to newly created one

Then mount the volume and verify as below:

Permanent Mount: Edit the /etc/fstab file to configure permanent mounting. Add an entry with the partition path, mount point, format type, and other options. Save the file and test the configuration using mount -a.

Save the file and exit.

Move Data: Move data from the backup directory to the mounted partition. For example, use mv command to move data from /tmp/img-backups to /var/www/html/images., and restart the apache2 service

Verify: Check if the data is accessible through the browser. If not, consider disabling SELinux temporarily by modifying the /etc/selinux/config file and rebooting the system.

Take the public IP and paste in the browser: you could see the page.

EBS Snapshots

Unmount the volume we created above as below:

Next, navigate to the AWS console, choose the volume that was created, and proceed to detach it by selecting the respective option.

Create a new volume same as shown above and attach it to the same ec2 instance.

Follow the same steps as mentioned above to create a new partition, format , and then create a directory

Add the new volume to the /etc/fstab.

Then mount it.

Install mariadb-server package with the below steps:

apt install mariadb-server
systemctl start mariadb
systemctl enable mariadb
systemctl status mariadb

List the /var/lib/mysql contents:

ls /var/lib/mysql/

Snapshot functionality primarily serves as a backup and restoration tool, crucial for mitigating data loss in the event of system failures. However, snapshots offer more than just backup capabilities.

In the event of data loss, the initial step involves unmounting the partition to prevent data overwriting. If the goal is to restore the existing partition, snapshots may not be applicable, as they provide a new volume containing the data. In such cases, unmounting the partition and employing recovery tools become essential.

When utilizing snapshots, unmounting the partition is the preliminary action, followed by detaching the corrupted volume. Subsequently, a new volume is created from the snapshot, retaining all data and partition configurations. Upon creation, the new volume is ready for attachment and mounting, effectively replacing the corrupted volume.

In essence, snapshots facilitate data restoration through volume replacement rather than repair, streamlining the recovery process and ensuring data integrity.

Take a Snapshot:

  • Navigate to the Volume section in the AWS console.
  • Select the relevant volume, then click on “Actions” and choose “Create Snapshot.”
  • Provide a meaningful description and add tags for easy identification.
  • Click on “Create Snapshot” and wait for it to enter the completed state.

Delete Data:

  • If you accidentally delete data or encounter corruption, proceed with data deletion.
  • Access the directory containing the data you wish to remove, e.g., /var/lib/mysql.
  • Use the command rm -rf * to remove all files and directories within the specified location.

Unmount:

  • Stop any relevant services to prevent further data overwrites.
  • Unmount the affected directory, e.g., /var/lib/mysql.

Detach Corrupted Volume:

  • Navigate to the Volumes section and locate the corrupted volume.
  • Rename the volume for easy identification, e.g., “corrupted.”
  • Select “Actions” and choose “Detach Volume” to disconnect it.

Recover from Snapshot:

  • Head to the Snapshots section and select the desired snapshot.
  • Click on “Actions” and choose “Create snapshots from volume”.
  • Customize volume settings such as type, size, and zone if needed.
  • Add appropriate tags for clarity
  • Click on “Create Volume” to finalize the recovery process.

In the Volumes console. goto Volumes and rename the new volume as “recovered”.

Goto actions and attach the volume

Below, we examined the disks and found that they were not mounted. Consequently, we executed the mount -a command to rectify the issue. Upon verification, the disks were automatically mounted, allowing us to recover the deleted data by executing ls /var/lib/mysql/.

In the snapshot console, you’ll find various options beyond basic backup functionality. These include deleting snapshots, creating volumes from existing snapshots (which we’ve already covered), and managing fast snapshot restores for large volumes, albeit with additional charges.

Additionally, you can leverage snapshots to create images, known as Amazon Machine Images (AMIs). If the snapshot originates from a root volume, you can directly create an image from it.

Another useful feature is the ability to copy snapshots. By clicking the “copy” button, you can duplicate snapshots to different regions, which is particularly handy for multi-site setups. Moreover, during the copying process, you have the option to enable encryption for added security.

Furthermore, the snapshot interface offers a “modify permissions” button, allowing you to adjust access settings. This enables you to make snapshots public or share them with specific AWS accounts by providing their account IDs.

In summary, snapshots offer a simple yet powerful toolset with various benefits, making them an indispensable asset for managing AWS resources effectively.

Stay tuned for practical applications and hands-on examples.

Thank you 😊🫰🫶

Athira KK
Athira KK

Written by Athira KK

AWS DevOps Engineer | Calico Big Cats Ambassador | WomenTech Global Ambassador | LinkedIn Top Cloud Computing Voice

Responses (3)