Blogs (Digital Solutions Blog)

Liferay 6.x Integration with Amazon EC2

Siddharth Barsaniya
7 Years Ago

Liferay 6.x Integration with Amazon EC2

In this post we will explore:

  • What is Amazon EC2?
  • Steps to create and configure Amazon EC2 instances
  • How you can integrate your portlet with Amazon API and access EC2 instances from your local web portal
  • A case study on how to deal with instance related actions.

What is Amazon EC2?

Amazon provides web services and a web service interface to access those services. Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides scalable and resizable compute resources in the cloud that can be configured and used as needed.

Client applications are deployed on virtual computers called instances. Instances are created from the Amazon Machine Image (AMI), a template that contains a software configuration (for example, an operating system, an application server, and applications). Using AMI, you can launch multiple instances as shown in the following figure. For more details refer here.


  1. You must have have an Amazon Web Services (AWS) account.
  • Go to the link to create an AWS account and click on Sign Up .
  • Follow the link to get started with your account.
  1. In order to access Amazon EC2 instances through API, we need to create access and secret keys. We use these keys to get access to the amazon web services which provide us access to the configured ec2 instances.
  • Follow these steps:            
  • Login in to the AWS account.
  • Go to Security Credentials under your profile name menu tab on the right top corner of site. Click on it and it will ask you to continue or follow best practice. For our purpose click continue.
  • Under Security Credentials you will have panel that allows you to create access and secret key. Use these keys in your application’s API to get access to AWS services. Follow the link for more details.
  1. Download AWS SDK for Java:
  • Follow the link to download Java SDK.
  1. Download Liferay Portal 6.1 CE GA3 bundled with tomcat and configure it.

Steps to create EC2 instances:

Create a few EC2 instances that are available in the free tier for testing purposes. Follow the below steps to create instances:

  • Login into your account.
  • Under Computer & Networking look for EC2. Click on it.

  • Select the region for which you want to create an instance.

  • Look for Launch Instance. Click on it.

  • Let's go through the step-by-step process to create an instance. Create an Amazon Linux AMI instance named dsg-ec2VM available in the free tier for our testing purposes. Click on Select button.

  • Step 2 allows you to choose an instance type. Since we are into Free Tier, let's not make any changes here. Click on Next Configure Instance Details.

  • Step 3 Configure Instance Details. Don't make any changes here. Click on Next: Add Storage

  • Step 4 Add Storage. No changes required. Click on Next: Tag Instance.

Step 4 Add Storage.jpg

  • Step 5 Tag Instance. Here assign the value for the Name field as " dsg-ec2VM". Click Next: Configure Security Group.

Step 5 Tag Instance.jpg


  • Step 6 Configure Security Group. No changes Required. Click on Review and Launch.

Step 6 Configure Security Group.jpg

  • Step 7 Review Instance Launch. Review your instance configuration details. Click on Launch to finally launch the instance.

Step 7 Review Instance Launch.jpg

  • Clicking on Launch button shows you the pop up.  If this is the first time you are creating an instance, choose the option "Create a new key pair" from the drop down, or go for "Choose an existing key pair". After successfully creating the key pair, click on Launch Instance.

Step 8 PopUp.png

  • Check for the Launch Status: Click on View Instance.

Launch Status.jpg

  • That's it. You are redirected to the Instances page where you can find list of all the instances.


  • Right click on instance and you can see various actions for instance. Some common actions are Terminate, Reboot, Stop and Start.

Instance Operations.jpg


After creating instances, let's see how we can access them from our portlet.

Requirement Case:

Suppose we have a requirement to allow users to access EC2 instances and perform operations on them from our local web portal.


  • Create a new liferay-portlet. Copy following jars in to the lib folder. Refer amazon-java-api for code examples. Also Amazon-Java-SDK comes with sample code that can help with the development.
    • aws-java-sdk.jar
    • commons-codec.jar
    • commons-logging.jar
  • We first need to configure the portlet by providing the access and secret key. These keys will be used by the Amazon API to identify and communicate with account.


  • Once the configuration is done, you can use the API to fetch the list of EC2 instances we just created and display them as per the requirement. Each instance has a unique instance ID. In our case for the instance dsg-testvm instance ID is "i-8668baa8". We can use the instance ID to identify the instance and perform actions accordingly.


  • For each instance we can have an action menu that allows users to perform actions like StartStopReboot and Terminate on instances.


more actions.jpg

Case Study:

Case-0: When we configure a portlet, make sure to validate the access and secret keys. Also encrypt them before saving into preferences.

invalid config.jpg


Case-1: When we perform any action on instance for example:

  • Scenario-1: We started instance i-bn3456, but note that the instance is not going to start immediately.  For any operation initiated on an instance, the instance state first changes to pending/starting/stopping/terminating state (respective to actions), and after some interval of time (not sure how much time, it varies instance to instance and action to action), the instance gets started and the instance state changes to stable instance states started/stopped/terminated (respective to actions)You will notice this while performing actions on instances from the amazon-EC2 console.
  • Problem: A user may have started the instance and on the page under instance state column it displays “starting”, but the user has no way to know when exactly the instance is started. So the user needs to refresh the page again and again to check if the instance is started or not and note there are many instances on the page.
  • Solution:  Prefer Ajax request for each instance’s each action that monitors the instance state continuously i.e. it repeatedly pings aws-service to get the current state of the instance and sends the response to the page once the required stable instance state is achieved. Use loading mask under the instance state column if the state is unstable and replace it with the stable state on receiving response.



Case-2: Each instance has an action menu and it contains actions to be performed on the instance. Make sure you do not show all the actions at the same time.

  • Scenario-1:  If the instance in the stopped state the user should see only Start action.
    • Problem: Showing other actions like Stop, Reboot, and Terminate makes no sense. If the instance is not yet started it would not make sense to perform the Stop action.

    • Solution: Display actions based on the current state of the instance and after every action update the action menu.
  • Scenario-2:  On each instance the user should be allowed to perform only one action at a time.
    • Problem: If the user starts the instance and other actions are not disabled, the user may try to perform the same action or different actions like terminating the instance or trying to stop the instance even if the instance is in pending starting state and not actually started. This leads to invalid user operation on the instance.

    • Solution : When the user initiates action on the instance, disable other actions, until you get the response (i.e. stable instance state), than update the action menu as discussed in case-2 scenario-1.



Case-3: It may take time for the instance to come into a stable state after every action. We can’t force the user to wait till the state becomes stable.

  • Scenario-1: User performs an action on the instance and then closes the browser, tab or initiates a different request on the same page, like visiting Home page.
    • Problem: Each action for the instance initiates the ajax request, and while the request is in progress the user closes the window . This causes the ajax request to fail and it has no way to respond with a stable state. So when the user again comes back to the instance page, he/she finds the instance in an unstable state. This will not affect the action because it is then handled by the thread created by the request, however the user now has no way to know whether the action is completed or not. This leads us to the Case-1 Scenario-1.

    • Solution: Every time the user loads the instance list page, verify the instance state. In the case the state is unstable, then for each instance initiate the ajax request and check for the stable state for that instance until it responds back with the stable state. When the instance state is unstable, use loading mask and replace it with stable state once the response is received.


Other than this we can also configure instance specific permissions. For example:

  • Whether the user has access to the instance.
  • For instance i-bn3456 (where i-bn3456 is instance ID) the user has access to start, stop and reboot the instance and this varies per the role and permissions.
  • Example: For Instance i-8668baa8, user Test Test is instance administrator and has access to start, stop, reboot and terminate the instance.

amazon permissions.jpg


That's it!

Please let us know how it goes!