AWS ephemeral storage format script
I created this script so that I could utilise the free instance store provided with my EC2 instance as swap. Special swap instance stores are provided with m1.small and m1.medium instances (usually /dev/xvdb3) and mounted automatically (Unless you upgrade from a micro but that is another issue I’ll cover in another post). T1 and T2 instances don’t have instance stores or special Swap instance stores. You could use this script with a second EBS volume on the T1 and T2 instances for swap to give that buffer for the small amount of memory provided (Make sure you use the EBS SSD backed storage otherwise you will be charged per million I/O’s).
This script will format the instance store on boot and enable swap, by default it formats /dev/xvdb which shouldn’t contain data on boot as ephemeral storage (what the instance store sites on top of) is wiped on shutdown. For safety the script will not write any data to any /dev/ device if it has an existing partition table.
Usage for swap is as follows;
sudo /home/ubuntu/scripts/aws-ephemeral-format.sh -t 82 -s 1024 -f swap
-t File system type code
-s cylinder size
-f File system type [ swap | ext2 | ext3 | ext4 ]
You specify the size of the partition in cylinders (see fdisk -l for number of cylinders logical disk)
You can add the following after the script to redirect the output and any errors to a file;
>> /tmp/swapon_log.log 2>&1
To apply this script so it runs at boot you can add it to /etc/rc.local but ensure you add before the exit 0 as the rc.local file is run on boot time and MUST exit with the 0 status otherwise it will cause other issues with your instance.
You can download my script from Github here;