Building an AMI with packer that includes autoupdate on lauch

I started looking into this last year but got side tracked with life and projects at work.  I finally decided this past week to try and get this working after a co-worker said he needed this for an application he was working on.  So here are the steps I took.

Step 1. Build a packer json file.

{
    "builders": [{
        "access_key": "",
        "ami_name": "app {{timestamp}}",
        "instance_type": "t1.micro",
        "region": "us-east-1",
        "secret_key": "",
        "security_group_id": "sg-3987db51",
        "source_ami": "ami-c8cf3ba0",
        "ssh_username": "ubuntu",
        "type": "amazon-ebs"
    }],
    "post-processors": null,
    "provisioners": [{
        "inline": [
            "sudo apt-get install -y --force-yes git",
            "sudo add-apt-repository -y ppa:chris-lea/node.js",
            "sudo apt-get update",
            "sudo apt-get install -y python-software-properties python g++ make",
            "sudo apt-get install -y nodejs",
            "sudo apt-get install -y npm",
            "sudo npm install -g node-gyp",
            "sudo apt-get install -y redis-server",
            "echo 'America/New_York' | sudo tee /etc/timezone",
            "sudo dpkg-reconfigure --frontend noninteractive tzdata",
            "sudo sh -c 'echo "*  soft  nofile  10000"  >> /etc/security/limits.conf'",
            "sudo sh -c 'echo "*  hard  nofile  10000"  >> /etc/security/limits.conf'",
            "sudo sh -c 'echo "session required pam_limits.so" >> /etc/pam.d/su'",
            "mkdir projects",
            "cd /home/ubuntu/projects",
            "git clone https://:@bitbucket.org/name/app.git",
            "sudo cp /home/ubuntu/projects//node-upstart-app.conf /etc/init",
            "sudo cp /home/ubuntu/projects//get_latest_on_init.conf /etc/init",
            "sudo rm -rf /home/ubuntu/tmp",
            "cd /home/ubuntu/projects/app",
            "npm install"
        ],
        "type": "shell"
    }]
}



The builder section refers to the `building` of the AMI

- The access_key and secret_key are your amazon credentials.
- The ami_name is the name which will be stored in amazon under `My AMI’s`.
- The instance_type and region are the defaults for instance creation.
- The security_group_id is a group you have defined in Amazon. This one gives everyone ssh and http access.
- The source_ami is the AMI you are going to use to build your AMI from. - - This one can be a little tricky. I suggest using this website: https://cloud-images.ubuntu.com/locator/ec2/ The one here is using Ubuntu 14.04 64 bit.
- The ssh_username is the default user to login as and the type describes the builder I want to use(you could use vagrant or virtual box or digital ocean as well)

The provisioners section is what’s run after the AMI has been built but has not been finalized to go into your `My AMI’s`. I am using an inline shell provisioner.

In this provisioner, I do the following:
- Install git.
- Pull down a public repo.
- Creates a directory structure.
- Pull down a private repo.
- Copy 2 upstart files from the private repo.
- One upstart file starts and stops my node process.
- The other upstart file is run one time during system initialization.
- Runs a bash script to install node, redis, and other items I common use.
- Removes a tmp directory.(messes up next step if not removed)
- Run npm install on private repo.

Step 2. Create an upstart file to get your latest source and restart your node website.


#!upstart description "get latest" author "Mike" # used to be: start on startup # until we found some mounts weren't ready yet while booting: start on (local-filesystems and net-device-up) stop on shutdown # Automatically Respawn: respawn respawn limit 1 5 script export HOME="/home/ubuntu" cd /home/ubuntu/projects/app exec /usr/bin/git pull https://:@bitbucket.org/name/app.git exec restart node-upstart-app end script post-start script end script


The above file is the upstart file that gets run once after the system is first started.
This file does the following:
- Changes directories to the project I want pull from.
- Pulls the latest source from a private git repo.
- Calls another upstart file to restart another upstart service.