How do you add swap to an EC2 instance?
Asked Answered
E

11

345

I'm currently running an ec2 micro instance and i've been finding that the instance occasionally runs out of memory.

Other than using a larger instance size, what else can be done?

Extempore answered 18/6, 2013 at 16:17 Comment(2)
Also see AWS docs instance store swap volumes, and this AWS knowledge center post.Gehlenite
And Why don't EC2 ubuntu images have swap?Gehlenite
E
636

A fix for this problem is to add swap (i.e. paging) space to the instance.

Paging works by creating an area on your hard drive and using it for extra memory, this memory is much slower than normal memory however much more of it is available.

To add this extra space to your instance you type:

sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo chmod 600 /var/swap.1
sudo /sbin/mkswap /var/swap.1
sudo /sbin/swapon /var/swap.1

If you need more than 1024 then change that to something higher.

To enable it by default after reboot, add this line to /etc/fstab:

/var/swap.1   swap    swap    defaults        0   0
Extempore answered 18/6, 2013 at 16:17 Comment(11)
Could you please explain what does it mean "if=...", "of=...", bs=1M and count=1024 because I've never seen if, of and = in the argument list.Lacylad
if means 'input file', of means 'output file', bs means 'block size' and count is the number of blocks you want to allocate… you can read the man page of the command for more info: linux.die.net/man/1/ddInga
That dd command line means "copy from /dev/zero to /var/swap.1. Do that by reading 1024 blocks of size 1 megabyte". It's a quick way to create a 1GB file full of zeroes.Stampede
enough old, but my swap memory remain 0% used even after restart, any help?Quinacrine
If you want to check if the swap is active: $> free -mWelles
Hi. Did this. Wonder why it's not being shown in htop? [Sorry for the noob doubt]Bipartisan
Having found minor differences in different articles regarding the line to add to /etc/fstab to make changes permanent, I've investigated by myself and feel a bit disappointed by the seemingly consensus here, specially concerning 2nd param, written as swap on every single answer here - man page for fstab states that For swap partitions, this field should be specified as none. This is the value I've found in most tutorials. About the 4th one, I recommend reading [this] (unix.stackexchange.com/questions/365953/…)Chronology
Once I'm done with the swap (I just needed additional memory for a one-time software package upgrade), how do I remove it to free up that space? Should I? Thank you!Bieber
in last command getting permission denied error I tried it with adding sudo at beginning! any suggestion?Swish
You can use fallocate which is almost instantaneous (baeldung.com/linux/create-large-file) instead of dd which can take a while with multiple GB files. fallocate -l 20G /swapfile works great on an EKS AMI.Galloromance
TLDR: Avoid using fallocate, although it's faster, it's not suitable for a swap file. From MKSWAP man: Note that a swap file must not contain any holes. Using cp(1) to create the file is not acceptable. Neither is use of fallocate(1) on file systems that support preallocated files, such as XFS or ext4, or on copy-on-write filesystems like btrfs. It is recommended to use dd(1) and /dev/zero in these cases. Please read notes from swapon(8) before adding a swap file to copy-on-write filesystems.Encage
O
239

Swap should take place on the Instance Storage (ephemeral) disk and not an EBS device. Swapping will cause a lot of IO and will increase cost on EBS. EBS is also slower than the Instance Store and the Instance Store comes free with certain types of EC2 Instances.

It will usually be mounted to /mnt but if not run

sudo mount /dev/xvda2 /mnt

To then create a swap file on this device do the following for a 4GB swapfile

sudo dd if=/dev/zero of=/mnt/swapfile bs=1M count=4096

Make sure no other user can view the swap file

sudo chown root:root /mnt/swapfile
sudo chmod 600 /mnt/swapfile

Make and Flag as swap

sudo mkswap /mnt/swapfile
sudo swapon /mnt/swapfile

Add/Make sure the following are in your /etc/fstab

/dev/xvda2      /mnt    auto    defaults,nobootwait,comment=cloudconfig 0   2
/mnt/swapfile swap swap defaults 0 0

lastly enable swap

sudo swapon -a
Overgrowth answered 7/3, 2014 at 10:39 Comment(17)
this is definitely the most comprehensive answer imoCalculable
To mount a file system one must specify the file system type. To find the file system the command is: $df -T This will show you the devices and their file systems. Mostly ext4. For me it was /dev/xvda1, not 2.Cindelyn
According to docs.aws.amazon.com/AWSEC2/latest/UserGuide/… it is not longer possible to add Instance stores to t1/t2 instances. After trying regardless, it would seem to be correct.Hampstead
@alfetopito You can probably create a literal swapfile on an existing partition instead of adding another partition as swap. A nice guide for Centos 6 - digitalocean.com/community/tutorials/…Hampstead
@Hampstead I see, tks. Since I don't have much space available on an t2.micro instance the only way out is to create the swap on an EBS volume then.Zebadiah
According to Amazon, it's not guaranteed that you keep your instance storage after reboot, won't the fstab fail then?Slaughter
@Slaughter A reboot of the operating system will not effect the instance store. Only a shutdown or stop of the instance will remove this. docs.aws.amazon.com/AWSEC2/latest/UserGuide/…Overgrowth
Can also add flag nobootwait option to the swap in fstab to avoid boot halt after a full instance shutdown and restart. Then run the steps again above to recreate swap.Overgrowth
@Overgrowth yeah, but I would prefer a full solution that survives complete shut-down of an aws instance. I guess I'll write init scripts then.Slaughter
@Slaughter Unfortunately you will have to do something like that as nothing located on the instance store survives a full shutdown/stopOvergrowth
Any idea why I'm getting this error? sudo mount /dev/xvdb /mnt mount: wrong fs type, bad option, bad superblock on /dev/xvdb, missing codepage or helper program, or other error In some cases useful info is found in syslog - try dmesg | tail or so. in dmesg I get [ 6.631257] EXT4-fs (xvda1): resizing filesystem from 2096891 to 7864059 blocks [ 6.639219] EXT4-fs (xvda1): resized filesystem to 7864059 [ 7.440513] cgroup: new mount options do not match the existing superblock, will be ignoredWashing
btw i'm trying to create a 3 x swap of 2048GB eachWashing
@Washing I think when using the mount command you have forgotten to specify the partition. I'm many cases the instance store is /dev/xvda2 but this may vary but a number at the end is partition.Overgrowth
"Swapping will cause a lot of IO and will increase cost on EBS." This might be the case for certain volume types, but not gp2, which only changes for storage. From the docs: "I/O is included in the price of the volumes, so you pay only for each GB of storage you provision." Latency concerns aside, it seems that cost might not be a concern when it comes to setting up EBS-backed swap volumes.Knitted
@BrianGerstle You are correct while this is not a monetary cost directly you may still quickly deplete your I/O credits if swapping for a period of time. This will reduce I/O performance to the baseline IOPS which will effect swapping performance and other applications that share the EBS volume.Overgrowth
compare docs.aws.amazon.com/AWSEC2/latest/UserGuide/… which is along the same lines but with some different details (eg they unmount the instance volume instead of mount it, confusingly, and the liens in /etc/fstab are different)Shelving
Can't we directly use swap memory without server restart? My server has CPU at 100% and this newly created swap file is showing 0 used and priority -1.Swaddle
E
45

You can add a 1 GB swap to your instance with these commands:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
sudo mkswap /swapfile
sudo swapon /swapfile

To enable it by default after reboot, add this line to /etc/fstab:

/swapfile swap swap defaults 0 0
Elnora answered 18/6, 2013 at 16:56 Comment(4)
But from which of my filesystem it will take the space to create Swap?Carboni
@Carboni It will create the swap in the root filesystem since the path is /swapfile. But you could also mount another filesystem and create the swap file there, e.g. /mnt/blah/swapfile.Elnora
Can confirm this works on a t2.micro instance. I did mine @ /mnt/swap though as I like a clean root.Caddie
Upvoted this answer because it is the shortest and does the job. However, swapfile chmod 600 command should be added.Worrell
S
43

After applying the steps mentioned by ajtrichards you can check if your amazon free tier instance is using swap using this command

cat /proc/meminfo

result:

ubuntu@ip-172-31-24-245:/$ cat /proc/meminfo
MemTotal:         604340 kB
MemFree:            8524 kB
Buffers:            3380 kB
Cached:           398316 kB
SwapCached:            0 kB
Active:           165476 kB
Inactive:         384556 kB
Active(anon):     141344 kB
Inactive(anon):     7248 kB
Active(file):      24132 kB
Inactive(file):   377308 kB
Unevictable:           0 kB
Mlocked:               0 kB

SwapTotal: 1048572 kB

SwapFree: 1048572 kB

Dirty:                 0 kB
Writeback:             0 kB
AnonPages:        148368 kB
Mapped:            14304 kB
Shmem:               256 kB
Slab:              26392 kB
SReclaimable:      18648 kB
SUnreclaim:         7744 kB
KernelStack:         736 kB
PageTables:         5060 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     1350740 kB
Committed_AS:     623908 kB
VmallocTotal:   34359738367 kB
VmallocUsed:        7420 kB
VmallocChunk:   34359728748 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:      637952 kB
DirectMap2M:           0 kB
Suborn answered 29/10, 2013 at 21:24 Comment(0)
F
16

If you are on t2 instances (t2.micro, t2.medium, t2.small), there is no ephemeral or instance storage available to you. So you need to just create your swap in EBS which depending on your use case may or maynot be a good idea. Otherwise follow @David 's answer, and create your swap on the ephemeral storage to avoid paying EBS costs.

More info: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html there is a table that shows how much ephemeral storage you get for each instance type.

Flint answered 21/2, 2015 at 11:58 Comment(4)
Can confirm this is generally a bad idea. If you have a process that goes rogue (I've had a node server do that, and a python/celery setup), you'll incur a ton of charges when it reads and writes to your EBS block all week without you knowing :-)Auria
According to aws.amazon.com/ebs/pricing , IO is included in all EBS volumes except Provisioned IPOS SSD (io1). You only per GB. This could have been a recent change in the pricing model - but it means that even though EBS is much slower than true Instance Storace, there are no additional charges for reads and writes.Pallium
@Pallium Would you mind to clarify that ? I've checked your link, and found this For example, if you provision a volume with 1000 IOPS, and keep this volume for 15 days in a 30 day month, then in a Region that charges $0.10 per provisioned IOPS-month, you would be charged $50 for the IOPS that you provision ($0.10 per provisioned IOPS-month * 1000 IOPS provisioned * 15 days/30). You will be charged for the IOPS provisioned on a volume even when the volume is detached from an instance.. Which seems that AWS will charge for read and write operations.Classless
@ValterHenrique The text you quoted applies to volumes with provisioned IOPS only, but you have the choice between several dfferent volume types. If you are using a "normal" volume without provisioned IOPS (like the general-purpose SSD volume type called "gp2"), read and write operations are not charged extra. Maybe a better link to compare: aws.amazon.com/ebs/detailsPallium
U
13

Using David's Instance Storage answer initially worked for me (on a m5d.2xlarge) however, after stopping the EC2 instance and turning it back on, I was unable to ssh in to the instance again.

The instance logs reported: "You are in emergency mode. After logging in, type "journalctl -xb" to view system logs, "systemctl reboot" to reboot, "systemctl default" or "exit" to boot into default mode. Press Enter for maintenance"

I instead followed the AWS instructions in this link and everything worked perfectly, including after turning the instance off and on again.

https://aws.amazon.com/premiumsupport/knowledge-center/ec2-memory-swap-file/

sudo dd if=/dev/zero of=/swapfile bs=1G count=4

sudo chmod 600 /swapfile

sudo mkswap /swapfile

sudo swapon /swapfile

sudo swapon -s

sudo vi /etc/fstab
/swapfile swap swap defaults 0 0
Unsuccessful answered 26/2, 2019 at 11:46 Comment(0)
R
9

You can create swap space using the following steps Here we are creating swap at /home/

  1. dd if=/dev/zero of=/home/swapfile1 bs=1024 count=8388608
    Here count is kilobyte count of swap space

  2. mkswap /home/swapfile1

  3. vi /etc/fstab
    make entry :
    /home/swapfile1 swap swap defaults 0 0

  4. run:
    swapon -a

Rectocele answered 28/5, 2014 at 11:18 Comment(0)
D
4

We can add swap space in any server

create a file using dd command

 #dd if=/dev/zero of=/swapfile bs=1M count=2048
                    or
 #dd if=/dev/zero of=/swapfile bs=1024M count=2

bs is blocksize and count refers to the size in MB or GB

we can use vice versa

After creation change the permission of file :

 #chmod 600 /swapfile 

Then makeswap the file :

 #mkswap /swapfile 

Then enable the swap file with swapon command :

 #swapon  /swapfile 

Check with free command whether swap is enabled or not :

 #free -h
 #swapon -s
Dedicate answered 16/1, 2018 at 14:35 Comment(0)
R
1

Try swapspace http://pqxx.org/development/swapspace/

Most distros have it packaged.

On EC2 you might want to change "swappath" to /mnt or high-iops disk.

Redblooded answered 7/2, 2014 at 12:49 Comment(1)
From the docs: "In its current form, Swapspace is probably not a good choice for systems that need to remain responsive at all times;"Invaginate
M
0

You can use EFS to tackle this storage problem on EC2. To do this-

  1. Create EFS Volume
  2. Mount it to the directory of application, you're using on your EC2.

EFS is a scalable, fully managed file storage service. Refer to this link https://aws.amazon.com/efs/ for more info

Misapprehend answered 28/11, 2023 at 6:10 Comment(0)
A
-5

You can use the following script to add swap on Amazon Linux.

https://github.com/chetankapoor/swap

Download the script using wget:

wget https://raw.githubusercontent.com/chetankapoor/swap/master/swap.sh -O swap.sh

Then run the script with the following format:

sh swap.sh 2G

For a complete tutorial you can visit:

https://installvirtual.com/shell-script-to-create-swap/

Allele answered 1/8, 2018 at 8:51 Comment(3)
I don't see what this answer contributes, besides just being a shellscript that basically does what the accepted answer already explained.Erny
I just created a shell script it saves time so there will be 2 steps to create swap instead of running 5 commands and if swap already exists it will show you swap is already there.Allele
Exactly. A shellscript that I am supposed to run as root. Made by someone, somewhere, that might change or disappear without a notice. All to save 4-5 command lines that were posted and accepted 5 years ago.Erny

© 2022 - 2024 — McMap. All rights reserved.