Creating a Custom AMI for Elastic Beanstalk

A couple of us here at spent several hours trying to create an AMI with the HotSpot JVM that would run on Elastic Beanstalk. After creating an AMI, our Beanstalk application would not run.

In hopes of saving others some of the pain that we went through, here are the steps for creating a custom AMI for Elastic Beanstalk that replaces the IcedTea JVM with the Oracle HotSpot JVM. This information was collected from multiple posts in the AWS user forums.

  1. Create a new Beanstalk environment, or identify an existing Beanstalk environment that you want to modify.
  2. Identify one EC2 instance running in this Beanstalk environment. You can identify an instance by following these steps:
    – Click the EC2 tab in the AWS Management Console.
    – Click the Load Balancers link in the left nav bar.
    – In the list of load balancers, click the name of the Beanstalk environment you want to work with.
    – In the bottom pane, click the Instances tab.
    – Copy one of the instance IDs listed under Instances. It should look something like i-5ce58d3e
    – Click the Instances link in the left nav bar.
    – Paste the instance ID into the search box. You should now see only one instance listed.
    – Right click on that one instance and select Launch More Like This from the context menu.
    – Follow the steps in the Launch Instances Wizardto start the instance. In most cases, you will not change any of the values in the Wizard. Just make sure that you’ve selected a key pair for the instance you are about to launch; otherwise, you will not be able to open an SSH connection to it later.These steps are essential. You cannot create a viable Beanstalk AMI from a running Beanstalk instance, and you cannot create one from a standard EC2 instance. At this writing (January 5, 2012), you can only create a viable Beanstalk AMI from an EC2 instance running outside Beanstalk that was cloned from an EC2 instance running inside Beanstalk!
  3. Once the instance is up and running, open an SSH connection to the instance.
    – Find the public IP of the instance. If you click on the instance name in the EC2 tab’s list of instances, the public IP will appear in the lower pane. It usually looks something like this:
    – You’ll have to connect as ec2-user, so your command line will look something like this:
    ssh -i ~/.ssh/your_key
  4. Once you are connected, run the following commands to download the Oracle HotSpot JVM and set it as the default.

At this point, if you run java -version, you should see that your server is running the Sun/Oracle JVM.

Now you need to create an AMI from the this instance you’ve just built.

  1. Find the instance in the Instances list under the EC2 tab.
  2. Right click on it, and choose Create Image. (Your SSH session will be terminated as part of the AMI creation process.)
  3. The AWS console will show you the ID of the image it is building. Copy this ID. You will need it in the next step.
  4. Once the image is complete, go back to the Elastic Beanstalk tab in the AWS Console.
  5. Find the environment where you want to use this new AMI. From that environment’s Action menu, choose Edit/Load Configuration.
  6. Paste the ID of your new AMI into the Custom AMI ID field.
  7. Click Apply.

You may have to restart the application or rebuild the environment to force the changes to take effect. Both options are available on the Action menu.

You can SSH into an EC2 instance within your Beanstalk environment and run java -version to see which JVM is running.

You can also create any number of new Beanstalk environments with this new AMI. Currently, when setting up a new Beanstalk environment,  you can choose only the vanilla 32 or 64 bit Beanstalk AMI running either Tomcat 6 or Tomcat 7. You can then edit the environment’s configuration after it’s up and running, and follow steps 5-7 above to replace the default AMI with your custom AMI.