Installing and Using Java8 on EC2

Getting Started

There are several goals for this blog post, but first we want to walk through the basics of creating and connecting to an Ec2 instance because it's so central to harvest the power of Aws resources. We will also be mindful of security so that you can become more comfortable with Aws shared security model. To take it one step further we'll show you how to install Java8 on your instance and compile and run Java8 lambda code against it.

Ec2 and Security Basics

We assume that you already have an Aws account and can access the console. As as a general comment about security you shouldn't use your root account to do things. You should use the Iam service and create another user and then assign him the minimal accesses he needs. Lock down the root login with multi-factor authentication and use the Google Authenticator app on your phone to get into it if you ever need to. Many people complain that they are unwilling to experiment with Aws because they are afraid they will get a giant charge on their credit card. A good security posture from the beginning will take this possibility off the table.

Launching an Instance in Detail

From the Ec2 dashboard select launch instance and choose the first option Amazon Linux AMI 2017.03.0 (HVM), SSD Volume Type - ami-c58c1dd3. This image contains a rich set of tools including the Aws command line, Python and even Docker. Select the general t2 micro instance and be sure to choose Configure Instance Details in lieu of the Review and Launch button. On the next screen be sure that Auto-assign Public IP is enabled. By default you'll launch into the default VPC and a public subnet. This is key because a public subnet effectively means that an internet gateway is attached and your instance will have internet access. Moving ahead you can configure security group. This is essentially the firewall to the instance itself. You can firewall the entire subnet with a NACL but that is beyond the scope of what we are doing here. You can choose the default security group but be sure that the inbound rules allow traffic on port 80 for http and 22 for ssh. This is critical because you will want to shell into the instance. For further security you can filter the source for ssh traffic to only come from your ip address. Amazon will automatically detect and populate your ip. You are now ready to launch. You can create a new key pair. I've named mine cert.pem for the tutorial. Be sure that wherever you choose to save the cert that you run chmod 400 cert.pem before you try to connect. From the Ec2 dashboard select the checkbox next to your new instance and from the actions drop-down menu at the top you can select connect. This will generate a connection string that you can cut and paste to the command line. In this case mine has the form ssh -i "cert.pem" [email protected]. The connection screen looks like this.

Upgrading the Java version

Now that you are connected you can run java -version to verify Java7 is currently installed. You can run sudo yum install java-1.7.0-openjdk-devel to have access to the javac command. Let's checkout some Java8 code and watch it fail to build under Java7. Install git with yum install git and check our sample code git clone Enter the Java8example directory and try to run a Java build with javac *.java and watch it not accept the Java8 syntax.

Let's upgrade it to Java8. Change over to root and run yum install java-1.8.0-openjdk and sudo yum install java-1.7.0-openjdk-devel. A this point we can run java -version again and notice we aren't onto java8 yet. We'll need to run alternatives --config java to switch over to running Java8. When this completes you'll be here.

Try out Java8 Lambda Expressions

I've included a simple build script you can run source buildAndRun.txt to compile and run the Java8 code. The git repo also includes the banner rpm file. You can install banner with rpm -i banner-1.3.4-3.el7.x86_64.rpm. You can now claim victory in style. When it's all over you'll be here


There is plenty more to say about Java8 but lambda expressions are a frequently pointed to new feature worth incorporating into your skill set. I encourage you to browse the simple code I've used here and learn more about Java8 before Java9 is released in July 2017.