-
Volumes are like hard drives or flash drives.
-
Volumes are created as Elastic Block Storage (EBS) devices.
-
An instance should have at least one volume, where the operating system runs. This volume is usually called the root volume.
-
It is a good practise to have a separate volume for your data (like having an external drive that you can connect and disconnect to several different instances).
-
In tutorial we will create an additional volume and access it in our instance.
-
We will also create an S3 storage bucket and learn to access it
1. Create volume
From the EC2 console left hand menu bar, click “Volumes” under the “Elastic Block Storage” section. In the resulting volumes page, you might see other volumes (in the screenshot below, we see the 8 GB root volume). Click the “Create Volume” button to create a new volume.
1.1. Configure Volume
In the Create Volume window, enter “1” for Size, choose the “us-west-2b” for Availability zone (as us-west-2b is where the instance is running). Click “Create Volume” and “Close” the next window.
1.2. Volume state
In the screenshot below, our volume window show two volumes. The 8 GB root volume that has the “State” as “in-use” and the 1 GB volume that we just created, with the “State” as “available”.
To check what volumes are currently mounted/attached in our instance, run the command “lsblk” command your instance terminal;
The above screenshot shows that there is only one volume/device (xvda) attached to the instance and it is mounted in the root (“/”), with 8G size.
1.3. Attach volume
Click and select the new 1 GB volume that we just created.
Click “Actions” and “Attach Volume”.
In the “Attach Volume” window, click the box next to “Instance” option and select the appropriate instance from the popup list. In our below example, we have one instance that is “running”, select this instance and click “Attach” button.
Now if you run the “lsblk” command, you will see the new volume/device showing up in our instance, as xvdf, but not mounted yet.
1.4. Formatting the volume
Formatting the new volume has to be done only once. After that you can attach/mount detach/unmount the new volume as you would do with a regular physical external flash drive or hard drive. As the new device is attached as xvdf, run the following command to format it;
sudo mkfs -t ext4 /dev/xvdf
1.5. Mounting the new volume
Now that we have formatted the new volume, we need a place to mount it.
Create a new directory in your home directory, using the following command;
mkdir data
The command “ls” lists the files and folder. In the above screen shot the ‘ls’ command lists the “data” directory that we just created with the “mkdir data” command.
Use the following command to mount our new /dev/xvdf volume in this new ‘data’ directory using the following command;
sudo mount /dev/xvdf data
Now, the new data volume is available for you to work like any other directory in your instance.
Once you are done working, you could unmount the device and then detach it from the instance. This detached volume could then be attached to any other instance, any other time or archived for future use or shared with others.
1.6. Unmounting the volume
Run the below command to unmount the ‘data’ volume that we just mounted;
sudo umount data
1.7. Detach volume
To detach a volume from an instance, select that volume from the “Volumes” menu, click Actions and chose “Detach Volume” option. Confirm “Yes Detach” in the prompting window, to detach this volume from your running instance.
1.8. Delete volume
If you dont want to volume anymore and if you dont want to be paying for this storage, you could delete the volume, using the Volume menus, Actions - Delete Volume option;
2. Using AWS Simple Storage Service (S3)
Any object/data could be easily and securely stored in the cloud using AWS S3 services. There are several different ways to do this, with a bunch of different pricing options. Here we will learn to use the basic S3 operations for low cost data storage.
2.1. Access S3
An S3 bucket is like a folder to store your files. It is unique and accessed as a web url. Click the “Services” menu option in the AWS management console and click “S3” under the “Storage” section;
2.2. Create S3 bucket
In the Amazon S3 service window, click the “Create bucket” button.
Enter a unique name for your bucket and click “Next” in the “Create bucket” window.
Click “Next” in the next two windows and then click “Create bucket” in the last window to create your S3 bucket.
2.3. Upload data to S3 bucket
In the Amazon S3 window, click on the name of the bucket where you want to upload/download data.
In the bucket window, click “Upload” button.
In the resulting window, either drag and drop files from your local computer or Click “Add files” button to browse and add files from your local computer to the S3 bucket.
Once your files are added, click the “Upload” button to upload your files to the S3 bucket.
2.4. Accessing stored data
The S3 bucket and its files can be accessed just by clicking on the bucket name and clicking on the files of interest (for downloading or sharing).