I have an EC2 instance for which I want to configure a swap instance volume.
I can do this relatively easily, by launching the instance with the instance store volume attached, and then running the following commands...
$ sudo mkswap /dev/xvdb
$ sudo swapon -p 1 /dev/xvdb
I can then run the following command to verify that it has worked...
$ sudo swapon -s
Filename Type Size Used Priority
/dev/xvdb partition 335515644 0 1
I can make this persist after a reboot by adding a line like so in the /etc/fstab
/dev/xvdb swap swap pri=1 0 0
The issue is that if I stop the instance, and then start it again. The swap disapears. I have to re-create it with mkswap and swapon.
What is the correct way to configure an instance to automatically attach the instance volume as swap space?
I tried adding the following to the instance user-data...
#!/bin/sh
mkswap /dev/xvdb
swapon -p 1 /dev/xvdb
However this didn't do anything. I still need to manually re-create the swap each time I start the instance.