A couple of us here at Hotelicopter.com 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.
- Create a new Beanstalk environment, or identify an existing Beanstalk environment that you want to modify.
- 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!
- 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: ec2-50-22-133-61.compute-1.amazonaws.com
– You’ll have to connect as ec2-user, so your command line will look something like this:
ssh -i ~/.ssh/your_key firstname.lastname@example.org
- Once you are connected, run the following commands to download the Oracle HotSpot JVM and set it as the default.
chmod +x jdk-6u25-linux-x64-rpm.bin
sudo alternatives --install /usr/bin/java java /usr/java/default/bin/java 20000 --slave /usr/bin/keytool keytool /usr/java/default/bin/keytool --slave /usr/bin/rmiregistry rmiregistry /usr/java/default/bin/rmiregistry
sudo alternatives --install /usr/bin/javac javac /usr/java/default/bin/javac 20000 --slave /usr/bin/jar jar /usr/java/default/bin/jar --slave /usr/bin/rmic rmic /usr/java/default/bin/rmic
sudo update-alternatives --config java
sudo ln -s /usr/java/default/jre /usr/lib/jvm/jre
sudo ln -s /usr/share/java /usr/lib/jvm-exports/jre
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.
- Find the instance in the Instances list under the EC2 tab.
- Right click on it, and choose Create Image. (Your SSH session will be terminated as part of the AMI creation process.)
- 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.
- Once the image is complete, go back to the Elastic Beanstalk tab in the AWS Console.
- Find the environment where you want to use this new AMI. From that environment’s Action menu, choose Edit/Load Configuration.
- Paste the ID of your new AMI into the Custom AMI ID field.
- 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.