ChatGPT解决这个技术问题 Extra ChatGPT

How do you add swap to an EC2 instance?

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?


r
rogerdpack

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 /sbin/mkswap /var/swap.1
sudo chmod 600 /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

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.
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/dd
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.
enough old, but my swap memory remain 0% used even after restart, any help?
If you want to check if the swap is active: $> free -m
j
jpsecher

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

this is definitely the most comprehensive answer imo
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.
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.
@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/…
According to Amazon, it's not guaranteed that you keep your instance storage after reboot, won't the fstab fail then?
D
David Levesque

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

But from which of my filesystem it will take the space to create Swap?
@Hussain 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.
Can confirm this works on a t2.micro instance. I did mine @ /mnt/swap though as I like a clean root.
Upvoted this answer because it is the shortest and does the job. However, swapfile chmod 600 command should be added.
s
scumah

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

A
Ali

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.


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 :-)
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.
@bobsoap 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.
@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/details
M
Machavity

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

dd if=/dev/zero of=/home/swapfile1 bs=1024 count=8388608 Here count is kilobyte count of swap space mkswap /home/swapfile1 vi /etc/fstab make entry : /home/swapfile1 swap swap defaults 0 0 run: swapon -a


D
David Jacques

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

J
Javeed Shakeel

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

r
rdoroshenko

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.


From the docs: "In its current form, Swapspace is probably not a good choice for systems that need to remain responsive at all times;"
C
Chetan kapoor

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/


I don't see what this answer contributes, besides just being a shellscript that basically does what the accepted answer already explained.
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.
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.