Custom AWS AMI Swap and Temp Folder

384 views Asked by At

I want to create an AMI. This AMI should mount the ephemeral storage of my instances on startup and create a swap file and a folder for /tmp

What I'm currently doing is after I booted to run a couple of commands:

sudo mkdir /media/ephemeral1/
sudo mount /dev/xvdc /media/ephemeral1/
sudo mkdir /media/ephemeral1/temp
sudo mount -B /tmp /media/ephemeral1/temp

and: (xvdb is alway auto-mounted at /media/ephemeral0/ ?)

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

do I just add all of this to my ~/.bashrc or what is the correct way to solve this. At the end I want an AMI that I can slap on a Instance with ephemeral storage and just get both a temp dir and a swap.

1

There are 1 answers

3
BryceH On BEST ANSWER

First off, .bash_rc is the wrong place for this because it will run each time Bash is launched. You may want to look at using user data on launch. Since you're moving these to ephemeral storage you must keep in mind that ephemeral storage can be different per instance type and class. Baking this type of change into an AMI directly is not possible since AMIs must be EBS backed. Therefore, your best option is to use user data as outlined below -

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html