A new colleague and friend of mine, Cosmin and I were talking at lunch the other day about EC2. He has been playing with AWS and EC2 for longer than I have and had some great suggestions. One of them was to prefer automating EC2 Instance creation and setup with shell scripts over using Instance Snapshots, because:
- You don’t have to remember all the nit-picky details of getting the new instance configured exactly the way you like it
- Every minute detail will happen the same every time
- The script serves as a reminder of every step taken to create, start and configure the instance
- You don’t have to pay the AWS charges for Snapshot storage!
So I started building an EC2 instance creator script. So far it successfully spawns a new instance, assigns the instance a static IP address, and SSH’es into the new instance: all by typing a single command at the command prompt! Here is the script:
I’ve been spending a lot of time creating and terminating EC2 instances, as well as having instances up and running for hours at a time (30 hours total over the last couple weeks if I’m reading the report correctly. The cost for all this experimenting? Ninety-Seven CENTS!
You’ll notice this script uses ec2-run-instances with the –user-data-file parameter, and that is where the magic happens. Once the instance is running, the script you pass in that parameter is automatically run. I intend to use that script to:
- Setup my home directory structure
- Run a package installer (in this case, yum) to add everything I need from the repository
- Any other miscellaneous setup like creating users, assigning permissions, setting up daemons and starting them, etc.
My intention is to add a public project to github to accompany the gist cited above once I have this second script in decent shape. For now, feel free to poke holes in the script above. I do a couple quirky things, but I have reasons for doing them the way I did. Feel free to add your questions or suggestions for improvements in the comments section below!