AWS

Deploying a Database-Driven Web Application in Amazon Web Services

Adapted from: https://blog.idrsolutions.com/2015/10/how-to-set-up-amazon-cloudaws-elastic-beanstalk-on-the-netbeans-ide/

If you want to run a Java web application on the Amazon Cloud then you are going to have to become good friends with the Amazon Web Services (AWS) Elastic Beanstalk, which is a Platform As A Service (PaaS) that allows users to create, push and manage web applications in the Amazon Web Services console.

What you will need before you start:

  • The great thing about the Amazon Cloud is that the majority of the resources that you will need are already included in the latest NetBeans IDE.
  • The Amazon Beanstalk cloud service is already pre-installed and just needs to be set up.
  • To connect to AWS we have to use Glassfish servers which again are already installed.

Sign up for an Amazon Web Services account

First things first we need to get you an Amazon Web Services account. You can create an account and sign up on the AWS Console. The trial version is called AWS Free Tier and is active for a full 12 months. AWS has a wide range of services and utilities available, and with the Free Tier you gain access to a handful of them with a decent monthly amount of credit to use up. You can find out more about the allowance and Free Tier by looking at this page.

READ ALSO
Backup QNAP Turbo NAS data using CloudBackup Station

AWS Console

It can seem a bit daunting at first when you log into the console because of the range of services available. As this is a simple tutorial we are only going to use two of these services – Elastic Beanstalk and RDS Database Services.

Elastic Beanstalk

In order to deploy an application we need to create an environment that the application can run in. From the console’s compute section select Elastic Beanstalk and you will be greeted with the following screen:

2

We are going to create our own application in NetBeans later so for now click Create New Application in the top right corner. Supply a name for your application and click Next.

READ ALSO
ESB Amazon Infrastructure (AWS)

Now you will be asked to create an environment. We only want to deploy a standard application so click Create Web Server.

For the server we will need to use a Predefined configuration. Choose ‘Docker – Glassfish.’ We need to do this so that we can use the Glassfish servers with NetBeans later. For Environment Type choose ‘single instance’. If you go on to use Amazon cloud for your applications this should be the mode you create and test your application in. It will be lower on resources and easily upgradable later on if needed.

For the Application Code, select the option to upload your own code. Don’t add any additional resources. For the configuration details make sure the Instance Type is ‘t1.micro’ and leave the rest as the default settings. Click Next.

READ ALSO
Cloud Computing Infrastructure Tutorial

 

Click on the link for the environment (Sample-env-1)

Before we go onto the next section please check and make a quick note of your Elastic Beanstalk region. It appears next to your name in the top right menu. You will need this when you have to configure NetBeans later. Also notice the url for your application at the top of the screen.

Click on configuration in the menu options on the left of this screen.

Scroll to the bottom and select the option to create a new database.

Change the Deletion property to delete if you don’t want a snapshot taken when you delete/terminate the database. The snapshot will save your data but will take up some storage. Enter a username and password, keep the rest of the defaults, then hit apply.

READ ALSO
Understanding AWS concepts

Your environment screen will show that its configuration is being updated. This can take a while.

When you environment update has completed, go back to the Configurations screen. Scroll to the bottom and there will be a section for the data tier.

Click on the settings icon on the top right. You will be sent to the configuration page for this RDS instance. Click on the link to view in the RDS console.

Click on the configurations tab. It will provide you will all the information you need to setup a connection to your database from NetBeans (endpoint, port, database name, username)

Click the link for the second Security Groups (the one that starts with RDS)

READ ALSO
What is DevOps? Introduction to DevOps 7

Return to the AWS Management console. Find RDS under Database and click to open the Amazon RDS Console.

Click on the security group and edit the inbound rules for port 3306 to allow inbound traffic from anywhere.

Part III – Configure NetBeans

Configure NetBeans

To run web applications on the cloud we need to create a cloud instance which will create your necessary server instance for you. To do this navigate to your Services window and right click on Cloud and select the Add Cloud option. Select Amazon Beanstalk and click Next.

7

For the Region field you should select the region that you made a note of earlier in the tutorial. Your Access Key ID and Secret Access Key are in your credentials file that you downloaded. The file can be opened by a text editor e.g. WordPad. When copying the credentials make sure you are splitting the codes right. They are the last two parts of the file separated by commas. The picture below shows the Access Key ID selected.

READ ALSO
How to force users change password on first login Linux

8
After clicking Next you will see that the Server will be created using the environment we created earlier. Click Finish and you will now be able to see a new server and cloud instance in your Services window.

9

Create and Deploy a Web Application

To create a web application go to the Projects window, right click and select Create New Project. Then in the Java Web category select Web Application. On the next screen name your project. When you come to the Server and Settings page set the Server to the environment server we created in the above steps. The latest Java EE version supported in version 6. Click Finish and your project will be created.

READ ALSO
How to check last user login Oracle

Next create a new MySQL connection to the RDS instance you just created. You will need the endpoint address shown on the RDS console.along with your username and password. The default database name is ‘ebdb’.

After you make the connection, select execute command and run the cityzone.sql file.

Create a new WebApplication using the JSF framework. Call it awsmultizone.

On the server settings page, from the dropdown box, choose the AWS environment that you just created.

Replace the existing index.xhtml file with the index.xhtml file from the Big Java code.

Create error.xhtml and next.xhtml and replace the file with the appropriate file from the Big Java code.

READ ALSO
Ansible Interview Questions and Answers 2

Create a JSF ManagedBean called TimeZoneBean. Replace the file with the appropriate file from the Big Java code.

Replace the line of code in TimeZoneBean that says ‘jdbc/_default’ with ‘jdbc/multizone’

Create a new file in the project for the JDBC data source and connection pool (see instructions below).

Alter the web.xml file for the new data source ‘multizone’ (see instructions below). If you cannot set it up as a javax.sql.ConnectionPoolDataSource, alter the web.xml manually. (Note that the version of glassfish running on AWS does not want the jdbc/multizone entry, just multizone.

Setting up a JDBC data source and connection pool

The GlassFish Server Open Source Edition contains Database Connection Pooling (DBCP) libraries that provide connection pooling functionality in a way that is transparent to you as a developer. To take advantage of this, you need to configure a JDBC (Java Database Connectivity) data source for the server which your application can use for connection pooling.

READ ALSO
How to check single table size in oracle

For more information on JDBC technology, see The Java Tutorials: JDBC Basics.

You could configure the data source directly within the GlassFish server Admin Console, or, as described below, you can declare the resources that your application needs in a glassfish-resources.xml file. When the application is deployed, the server reads in the resource declarations, and creates the necessary resources.

The following steps demonstrate how to declare a connection pool, and a data source that relies on the connection pool. The NetBeans JDBC Resource wizard allows you to perform both actions.

Open the New File wizard by pressing the New File button in the IDE’s main toolbar. Select the GlassFish server category, then select JDBC Resource and click Next.

READ ALSO
Getting started with amazon-ec2

In step 2, General Attributes, choose the Create New JDBC Connection Pool option, then in the JNDI Name text field, type in jdbc/multizone.

The JDBC data source relies on JNDI, the Java Naming and Directory Interface. The JNDI API provides a uniform way for applications to find and access data sources. For more information, see The JNDI Tutorial.

Optionally, add a description for the data source.

Click Next, then click Next again to skip step 3, Additional Properties.

In Step 4, type in multizonepool for JDBC Connection Pool Name. Make sure the Extract from Existing Connection option is selected, and choose the jdbc connection from AWS from the drop-down list. Click Next.

READ ALSO
AWS Lambda using Python

Note: The wizard detects any database connections that have been set up in the IDE. Therefore, you need to have already created a connection to the multizone database at this point. You can verify what connections have been created by opening the Services window (Ctrl-5; ⌘-5 on Mac) and looking for connection nodes ( database connection node icon ) under the Databases category.

In Step 5, select javax.sql.ConnectionPoolDataSource in the Resource Type drop-down list.

Note that the IDE extracts information from the database connection you specified in the previous step, and sets name-value properties for the new connection pool.

Click Finish. The wizard generates a glassfish-resources.xml file that contains entries for the data source and connection pool you specified.

READ ALSO
Introduction to AWS CLI

In the Projects window, you can open the glassfish-resources.xml file that was created under the Server Resources node and note that, within the <resources> tags, a data source and connection pool have been declared containing the values you previously specified.

Referencing the data source from the application

You need to reference the JDBC resource you just configured from the web application. To do so, you can create an entry in the application’s web.xml deployment descriptor.

Deployment descriptors are XML-based text files that contain information describing how an application is to be deployed to a specific environment. For example, they are normally used to specify application context parameters and behavioral patterns, security settings, as well as mappings for servlets, filters and listeners.

READ ALSO
SSH Keys for Amazon EC2 instances

Perform the following steps to reference the data source in the application’s deployment descriptor.

  1. In the Projects window, expand the Configuration Files folder and double-click web.xml to open the file in the editor.
  2. Click the References tab located along the top of the editor.
  3. Expand the Resource References heading and click Add to open the Add Resource Reference dialog.
  4. For Resource Name, enter the resource name that you gave when configuring the data source for the server above (multizone).
  5. Type javax.sql.ConnectionPoolDataSource in the Resource Type field. Click OK.
  6. The Description field is optional, but you can enter a human-readable description of the resource, e.g., Database for multizone application.
READ ALSO
Using R on Amazon EC2 under the Free Usage Tier

The new resource is now listed under the Resource References heading.

  1. To verify that the resource is now added to the web.xml file, click the Source tab located along the top of the editor. Notice that the following <resource-ref> tags are now included.

Now for the complicated deployment process. Anyway first right click the project and click Run. Then you wait. And wait. And then wait a little bit more… and then voila your browser will open up your application in a new tab when it is finally ready Uploading applications can take a few minutes, which is a long and agonizing time to wait when you are trying to test a fix.

READ ALSO
Install VirtualBox

You can access this page from the browser by entering the Environment URL you created earlier for your project or if you can’t remember that, then you can access it from the AWS Console. Just go back to Elastic Beanstalk section, select your environment and there will be a link to your application.

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.