Skip to main content

Command Palette

Search for a command to run...

AWS EC2 lifecycle and EC2 storage options

Published
3 min read

EC2 Lifecycle

Launching a new instance:

Initial stage in the life of an instance is creating a new instance. It provides us the flexibility to choose and configure the instance. The steps for creating a new instance have been explained in detail in the previous blog.

Stop and Start state:

For the purpose of saving costs, instances can be stopped at any point of time when it is not in use. Stopping and starting an instance changes its public address.

Reboot:

Rebooting an instance does not change instance's state and it retains the same public IP address.

Terminate:

Terminating an EC2 instance delete it permanently and deletes its associated storage. If it has an EBS volume with "Delete on termination" flag set to false, then that volume will not be deleted.

EC2 storage options

Elastic Block Storage

Elastic Block Storage (EBS) is a persistent block storage that can be used with EC2 instances. By default an EBS volume will be associated with an EC2 instance called root volume. We can add more EBS volumes to the instance depending on the use case.

Types: General Purpose SSD(gp2,gp3) , Provisioned IOPS SSD (io1, io2), Throughput Optimized HDD(st1) and cold HDD (sc1)

EBS volume can be attached to only one EC2 instance at time within the same Availability Zone. You can detach an EBS volume from one instance and attach it to another. However this process involves downtime as the volume needs to be unbounded from the first instance before it can be attached to another.

If you are using EBS provisioned IOPS SSD volumes(io1 and io2) they support a feature called multi-attach which allows you to attach a single volume to multiple instances within the same Availability zone

EBS volumes can be used for applications that requires persistent storage with single-instance attachment or high-performance storage needs.

Instance store

Instance store is a temporary block storage that is physically attached to the host machine. Its an ephemeral storage. Data is lost when the instance is stopped or terminated. The advantage of instance store is that it provides increased performance.

Instance store can be used for applications that require temporary, high-performance storage for ephemeral data.

Elastic File System (EFS)

Amazon EFS is a file storage that can be mounted on multiple EC2 instances simultaneously, making it ideal for shared access scenarios.

To use EFS with EC2 instances ,follow these steps:

1. Create an EFS File System:

- Go to the EFS Dashboard in the AWS Management Console.

- Click "Create file system" and follow the steps to create your file system.

- Configure network settings to ensure it is accessible from your EC2 instances (e.g., correct VPC, subnets, and security groups).

2. Install NFS Client:

- On your EC2 instances, install the NFS client. For Amazon Linux, you can use:

```sh

sudo yum install -y nfs-utils

```

3. Mount the EFS File System:

- Create a directory to serve as the mount point:

```sh

sudo mkdir /mnt/efs

```

- Mount the file system using the EFS mount helper or the NFS client:

- Using the EFS Mount Helper:

```sh

sudo mount -t efs fs-12345678:/ /mnt/efs

```

- Using the NFS Client:

```sh

sudo mount -t nfs4 -o nfsvers=4.1 fs-12345678.efs.<region>.amazonaws.com:/ /mnt/efs

```

Replace `fs-12345678` with your EFS file system ID and `<region>` with your AWS region.

4. Automate Mounting at Boot:

- To ensure the EFS file system mounts automatically after a reboot, add an entry to `/etc/fstab`:

```sh

echo "fs-12345678:/ /mnt/efs efs defaults,_netdev 0 0" | sudo tee -a /etc/fstab

```

- For NFS:

```sh

echo "fs-12345678.efs.<region>.amazonaws.com:/ /mnt/efs nfs4 defaults,_netdev 0 0" | sudo tee -a /etc/fstab

```

EFS can be used for hosting applications that needs scalable, shared file storage that can be accessed currently by multiple instances.

All the above storage options together ensure flexibility, performance, and scalability for a wide range of workloads.