Post Syndicated from Neelay Thaker original https://aws.amazon.com/blogs/compute/running-java-applications-on-amazon-ec2-a1-instances-with-amazon-corretto/
This post is contributed by Jeff Underhill | EC2 Principal Business Development Manager and Arthur Petitpierre | Arm Specialist Solutions Architect
Amazon EC2 A1 instances deliver up to 45% cost savings for scale-out applications and are powered by AWS Graviton Processors that feature 64-bit Arm Neoverse cores and custom silicon designed by AWS. Amazon Corretto is a no-cost, multiplatform, production-ready distribution of the Open Java Development Kit (OpenJDK).
Production-ready Arm 64-bit Linux builds of Amazon Corretto for JDK8 and JDK 11 were released Sep 17, 2019. This provided an additional Java runtime option when deploying your scale-out Java applications on Amazon EC2 A1 instances. We’re fortunate to have James Gosling, the designer of Java, as a member of the Amazon team, and he recently took to Twitter to announce the General Availability (GA) of Amazon Corretto for the Arm architecture:
If you’re interested to experiment with Amazon Corretto on Amazon EC2 A1 instances then read on for step-by-step instructions that will have you up and running in no time.
Launching an A1 EC2 instance
The first step is to create a running Amazon EC2 A1 instance. In this example, we demonstrate how to boot your instance using Amazon Linux 2. Starting from the AWS Console, you need to log-in to your AWS account or create a new account if you don’t already have one. Once you logged into the AWS console, navigate to the Amazon Elastic Compute Cloud (Amazon EC2) as follows:
Next, select the operating system and compute architecture of the EC2 instance you want to launch. In this case, use Amazon Linux 2 because we want an AWS Graviton-based A1 instance we’re selecting the 64-bit (Arm):
On the next page, we select an A1 instance type. select an a1.xlarge that offers 4 x vCPU’s and 8GB of memory (refer to the Amazon EC2 A1 page for more information). Then, select the “Review and Launch” button:
Next, you can review a summary of your instance details. This summary is shown in the following pictures. Note: the only network port exposed is SSH via TCP port 22. This allows you to remotely connect to the instance via an SSH terminal:
Before proceeding be aware you are about to start spending money (and don’t forget to terminate the instance at the end to avoid ongoing charges). As the warning in the screen shot above states: the A1 instance selected is not eligible for free tier. So, you are charged based on the pricing of the instance selected (refer to the Amazon EC2 on-demand pricing page for details. The a1.xlarge instance selected is $0.102 per Hour as of this writing).
Once you’re ready to proceed, select “Launch” to continue. At this point you need to create or supply an existing key-pair for use when connecting to the new instance via SSH. Details to establish this connection can be found in the EC2 documentation.
In this example, I connect from a Windows laptop using PuTTY. The details of converting EC2 keys into the right format can be found here. You can connect the same way. In the following screenshot, I use an existing key-pair that I generated. You can create an existing key-pair that best suits your workload and do the following:
While your instance launches, you can click on “View Instances” to see the status of instances within my AWS account:
Once you click on “View Instances,” you can see that your newly launched instance is now in the Running state:
Now, you can connect to your new instance. Right click on the instance from within the console, then select “Connect” from the pop-up menu to get details and instructions on connecting to the instance. This is shown in the following screenshot:
The following screenshot provides you with instructions and specific details needed to connect to your running A1 instance:
You can now connect to the running a1.xlarge instance through instructions to map your preferred SSH client.
Then, the Amazon Linux 2 command prompt pops up as follows:
Note: I run the ‘uname -a’ command to show that you are running on an ‘aarch64’ architecture which is the Linux architecture name for 64-bit Arm.
Once you complete this step, your A1 instance is up and running. From here, you can leverage Corretto8.
You can now install Amazon Corretto 8 on Amazon Linux 2 following the instructions from the documentation. Use option 1 to install the application from Amazon Linux 2 repository:
$ sudo amazon-linux-extras enable corretto8
$ sudo yum clean metadata
$ sudo yum install -y java-1.8.0-amazon-corretto
This code initiates the installation. Once complete, you can use the java version command to see that you have the newest version of Amazon Corretto. The java command is as follows (your version may be more recent):
$ java -version
openjdk version "1.8.0_232"
OpenJDK Runtime Environment Corretto-8.232.09.1 (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM Corretto-8.232.09.1 (build 25.232-b09, mixed mode
This command confirms that you have Amazon Corretto 8 version 8.232.09.1 installed and ready to go. If you see a version string that doesn’t mention Corretto, this means you have another version of Java already running. In this case, run the following command to change the default java providers:
$ sudo alternatives --config java
Installing tomcat8.5 and a simple JSP application
Once the latest Amazon Corretto is installed, confirm that the Java installation works. You can do this by installing and running a simple Java application.
To run this test, you need to install Apache Tomcat, which is a Java based application web server. Then, open up a public port to make it accessible and connect to it from a browser to confirm it’s running as expected.
Then, install tomcat8.5 from amazon-linux-extras using the following code:
$ sudo amazon-linux-extras enable tomcat8.5
$ sudo yum clean metadata
$ sudo yum install -y tomcat
Now configure tomcat to use /dev/urandom as an entropy source. This is important to do because otherwise tomcat might hang on a freshly booted instance if there’s not enough entropy depth. Note: there’s a kernel patch in flight to provide an alternate entropy mechanism:
$ sudo bash -c 'echo JAVA_OPTS=\"-Djava.security.egd=file:/dev/urandom\" >> /etc/tomcat/tomcat.conf'
Next, add a simple JavaServer Pages (JSP) application that will display details about your system.
First, create default web application directory:
$ sudo install -d -o root -g tomcat /var/lib/tomcat/webapps/ROOT
Then, add the small JSP application:
$ sudo bash -c 'cat <<EOF > /var/lib/tomcat/webapps/ROOT/index.jsp
<title>Corretto8 - Tomcat8.5 - Hello world</title>
<td><%= System.getProperty("os.name") %></td>
<td><%= System.getProperty("os.arch") %></td>
<td><%= System.getProperty("java.vendor") %></td>
<td><%= System.getProperty("java.vendor.url") %></td>
<td><%= System.getProperty("java.version") %></td>
<td><%= System.getProperty("java.vm.version") %></td>
<td><%= application.getServerInfo() %></td>
Finally, start the Tomcat service:
$ sudo systemctl start tomcat
Now the Tomcat service is running, you need to configure your EC2 instance to open TCP port 8080 (the default port that Tomcat listens on). This configuration allows you to access the instance from a browser and confirm Tomcat is running and serving content.
To do this, return to the AWS console and select your EC2 a1.xlarge instance. Then, in the information panel below, select the associated security group so we can modify the inbound rules to allow TCP accesses on port 8080 as follows:
With these modifications you should now be able to connect to the Apache Tomcat default page by directing a browser to http://<your instance IPv4 Public IP>:8080 as follows:
Don’t forget to terminate your EC2 instance(s) when you’re done to avoid ongoing charges!
To summarize, we spun up an Amazon EC2 A1 instance, installed and enabled Amazon Corretto and Apache Tomcat server, configured the security group for the EC2 Instance to accept connections to TCP port 8080 and then created and connected to a simple default JSP web page. Being able to display the JSP page confirms that you’re serving content, and can see the underlying Java Virtual Machine and platform architecture specifications. These steps demonstrate setting-up the Amazon Corretto + Apache Tomcat environment, and running a demo JSP web application on AWS Graviton based Amazon EC2 A1 instances using readily available open source software.
You can learn more at the Amazon Corretto website, and the downloads are all available here for Amazon Corretto 8, Amazon Corretto 11 and if you’re using containers here’s the Docker Official image. If you have any questions about your own workloads running on Amazon EC2 A1 instances, contact us at [email protected].