Friday, August 26, 2022

Fastest Dev in the Cloud: Using JHipster and AWS for rapid application development

 Summary: How to become the fastest dev in the cloud:

  1. Use a rapid development platform. In this blog, we will use JHipster for Java.
  2. Use a cloud-based IDE. We will use Cloud9 on AWS.
  3. Use AWS cloud resources to deploy your application. For demo purposes, we will just use EC2

Background: Why I wish I had this technology 10 years ago




We were developing an analysis and visualization platform using Google Web Toolkit. After more than 2 years of development, most of the functionality was already implemented. Suddenly, there were internal changes in the client's organization -- management changed hands. The new management had a preferred off-the-shelf analysis software, with much better performance and visualizations.

As a result, we worked really hard to throw in additional fancier charts and other features. We worked triple and quadruple shifts to get something working that could compete with the off-the-shelf solution. We couldn't compete, because the technology to create a comparable web-based solution simply didn't exist back then.

We lost. Two years of work on an app that would never be used. If only we could've shortened the development time.

Other challenges of software development projects:

  1. Obsolete even before going out to market
    1.  Too much effort and time needed to implement even basic features
  2. MVP is too raw 
    1.  Security is not there
    2.  Some basics are not there
    3.  Either UI is beautiful but lacking many features, or features are complete but UI is ugly
  3. Performance is poor
    1.  quickly runs out of hardware resources

Fast forward to today. We now have JHipster and AWS.

This is the kind of development toolkit that we want:

  1. Quick to develop with the best practices already baked in
  2. Easy to customize
  3. Cloud-ready
  4. Allows the dev team to focus on the more complex functionality

JHipster as a rapid development platform has the following components that speed up development:

  1. Code generator to auto-generate boilerplate code. It can also auto-generate code for entities, repositories and services, given a domain model.
  2. Coding conventions. The project it generates follows a certain set of standards and common configurations, so that you don't have to spend time deciding on which libraries and tools to use, etc. 
  3. It offers integrations with various cloud platforms for easy deployment


JHipster x AWS development process:

  1. Setup cloud IDE
  2. Generate boilerplate
  3. Domain diagram
  4. Generate domain code
  5. Write custom code
  6. Deploy to cloud

Setup cloud IDE (Cloud9)

  1. Create IAM user. The procedures for doing this yourself are out of scope of this post for now. Below are the permissions you can ask your AWS administrator to add.
    1. This IAM user should have permissions for the following AWS services:
      1. EC2
      2. S3
      3. RDS
      4. Cloud9
    2. Depending on your desired deployment method, you should also add permissions for the following services:
      1. Elastic BeanStalk
      2. ECS 
      3. ECR
      4. Lightsail
    3. Enable both AWS console login and AWS CLI
    4. Create access key for use with AWS CLI 
  2. Create Cloud9 environment. For this post, we'll create a Cloud9 environment in the us-west-1 region
    1. Go to Cloud9 service in the AWS console
    2. Click create environment
    3. Set a name. Description is optional. 
    4. You can leave other settings as is. Recommendation: choose t3a.medium instance type so that tests will run faster.
    5. Click Create environment. Wait for the IDE to load.
  3. Install additional tools. The IDE has Java, Node, npm, docker, and Git. If you plan to use Docker to deploy and run your application, there are additional steps to take.
    1. The docker installed on the Cloud9 instance needs to be replaced with one that has the Cloud Integrations. To do this: (source)

      curl -L -O https://github.com/docker/compose-cli/releases/download/v1.0.17/docker-linux-amd64
      mv docker-linux-amd64 docker
      chmod +x docker
      sudo ln -s /usr/bin/docker /usr/local/bin/com.docker.cli
      ./docker --context default ps
      sudo mv docker /usr/local/bin/docker
      bash
      docker version

    2. Docker compose also needs to be installed:
sudo curl -L "https://github.com/docker/compose/releases/download/v2.10.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
    1.  

JHipster Setup

Use the Cloud9 terminal to setup a JHipster project:
  1. Install jhipster-generator:

    npm install -g generator-jhipster

  2. Run JHipster generator. This can be done either interactively or with parameters. First create a project folder, go into the folder, then run jhipster.

    mkdir jhipsterdemo
    cd jhipsterdemo
    jhipster


  3. Build and test. The project comes with maven as well as npm with a package.json that contains the most common scripts. Build by either running ./mvnw or npm run app:start
  4. View the running app in another tab.
    You can use the Cloud9 URL: https://{id of cloud9 instance}.vfs.cloud9.{region}.amazonaws.com
    Or you can use the public URL of the Cloud9 IDE's EC2 instance
Note: when testing the application, you may encounter issues in signing in or registering a new user. The cloud9 terminal may show this error message:

Unauthorized: Full authentication is required to access this resource 

This may be caused by the CORS config blocking access to the app. For testing purposes, you may add the test URLs to the allowed origins under the CORS config.

Config file: /src/main/resources/config/application-dev.yml
Config to update: jhipster -> cors -> allowed-origins



Domain code development:

  1. Design the domain in JDL studio
  2. Export JDL file from studio and import to Cloud9
    1. In JDL Studio, you can find the download button at the upper right.
    2. In Cloud9, go to File -> Upload Local Files. Then select your JDL file.
  3. Generate code from the jdl file:

    jhipster jdl jhipster-jdl.jdl

  4. Extend generated code with custom code. Create Java classes and methods as needed. 
  5. Re-build and test. Same commands as above. Recommended: clean the project first with ./mvnw clean


Deploy to AWS:

There are many options to deploy your project. For this exercise, we will deploy to EC2.

  1. Create a new EC2 instance through the AWS Console
    1. Name your instance
    2. AMI: choose Amazon Linux (under Quick Start)
    3. Instance type: choose t3.small
    4. Key pair name: jhipsterdemo. Make sure to save the pem file to be used to ssh into the EC2 instance.
    5. Security group:
      1. Allow SSH traffic
      2. Allow HTTP and HTTPS traffic
    6. Click Create instance
  2. Update the security group to allow inbound traffic to port 8080. (For now, we will deploy the app with the dev profile. For production, use the prod profile.)
    1. View the EC2 instances. Then select the instance you created.
    2. Click the EC2 instance's Security group
    3. Click edit inbound rules
    4. Add a rue for:
      1. Type: Custom TCP
      2. Port range: 8080-8089
      3. Source: 0.0.0.0/0
    5. Click Save rules
  3. Start the EC2 instance
    1. Copy the EC2 instance's private IP address. You will use this for ssh
    2. Copy the EC2 instance's public IP address. You will use this for viewing the deployed application.
  4. Perform the rest of the steps in Cloud9 IDE
  5. Upload the pem file to Cloud9. Note: make sure you save it outside the project folder, so as not to accidentally commit it to code repository. 
    1. Set the right permissions on the pem file: chmod 400 jhipsterdemo.pem
  6. Generate a jar file with (back inside the project folder): npm run java:war:dev
  7. Copy the jar file into EC2:

    scp -i jhipsterdemo.pem ./target/{your_project_jar_filename}.jar ec2-user@{your_ec2_internal_ip_address}:~/

  8. ssh into the EC2 instance, then install a JDK

    ssh -i jhipsterdemo.pem ec2-user@{your_ec2_internal_ip_address}
    sudo yum install java-17-amazon-corretto-jmods.x86_64

  9. Run the application (still inside the EC2 ssh): java -jar {your_project_jar_filename}.jar

What we've accomplished:

  1. Complete development environment setup in the cloud
  2. Generated code for major functional & non-functional requirements
  3. Rapid conversion from domain diagram to code
  4. Deployment to cloud

What next?

  1. Go to jhipster.tech to learn more about what you can do with JHipster
  2. Let's connect for further mentoring: https://bit.ly/elielgoco

 

No comments:

Post a Comment

Fastest Dev in the Cloud: Using JHipster and AWS for rapid application development

 Summary: How to become the fastest dev in the cloud: Use a rapid development platform. In this blog, we will use JHipster for Java. Use a c...