February 2, 2012 - Warren Held

Amazon Elastic Compute Cloud: Adding Swap Space in Amazon Linux AMI

First go into your AWS Management Console.  You will need to go to “Volumes” under the Elastic Block Store grouping.  Create a new volume.  Make the volume 1gb in size and set the availability zone to the same zone as the other EBS volumes attached to your instance.

After you create the volume, click on it and click the “Attach Volume” button.  Select your instance and name the device something that you haven’t used on the other EBS volumes attached to your instance. (/dev/sdf through /dev/sdp for Linux instances)

Go to your linux instance console and run:

[bash]sudo fdisk -l[/bash]

The output should show the volume you created and attached:

Disk /dev/xvdg: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes<br />I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

(/dev/xcdg is /dev/sdg) Your disk name will be different depending on what you labeled it when you attached it.  Once you know your instance sees the drive, you have to format it.  Run:

[bash]sudo mkfs -T swap /dev/(your drive label) [/bash]

So in my case I ran

[bash]sudo mkfs -T swap /dev/sdg[/bash]

Once that is complete run:

[bash]sudo mkswap /dev/(your drive label)[/bash]

Then run:

[bash]sudo swapon /dev/(your drive label)[/bash]

Your swap space should be all set up and activated.  You can check by running:

[bash]free -l[/bash]

We have a problem. When you reboot your swap will not be mounted.  To fix that we need to edit /etc/fstab with your preferred text editor (I like vim. If you don’t have vim you can use vi):

[bash]sudo vim /etc/fstab[/bash]

You will need to add the line shown below (using your drive’s label)

Amazon EC2