1. Introduction
In this article, we’ll look at how to setup and configure Cloud9 environment that can support our Java and Spring Boot projects.
The AWS Cloud9 is an excellent web based generalist IDE (Integrated Development Environment) that you can use to code, build and run applications in various programming languages like Java, Node.js, JavaScript, Python, PHP, etc. It offers a coding experience with multiple languages with a built-in terminal.
The general advantages of using Cloud9 environment are –
- Full functionality of Cloud9 is accessible over a web browser.
- Removes the need to install and maintain a local IDE.
- IDE comes with a terminal that includes sudo admin privileges to AWS EC2 instance that is hosting our development environment.
- Assumes a pre-configured with AWS CLI as that assumes configured IAM role giving you easy access to AWS Services from the IDE and its terminal
- Stays true to AWS’s pay-as-you-use model by providing configurable settings to shut down the IDE and underlying EC2 instance after certain idle time.
Additionally, AWS Cloud9 provides the following features to improve our development experience when working specifically with Java –
- Enhanced Java language and debugging support
- Support for smart auto completion
- Code linting to highlight any compile time errors without the need to perform a build (similar to IntelliJ, Eclipse and other Java focused IDEs)
- Debugging support for Java. Cloud9 allows you to set breakpoints in your code similar to other popular Java IDEs
2. Create AWS Cloud9 environment
Depending on your usage context, you’ll setup Cloud 9 as an individual, a member of a team or a member of an enterprise.
For our purpose, we will setup Cloud9 as an individual.
2.1 Create an AWS Account
To provision a Cloud9 environment, we need an AWS account. If you already have an AWS account you can skip this section.
To create an AWS account
- Go to https://aws.amazon.com/
- Click Sign In to the Console.
- Click Create a new AWS account.
4. Follow the steps in the screens and complete the signup process. Its required to provide valid credit card details, email and mobile number.
5. You will receive a confirmation email once you successfully complete the required steps.
2.2 Provision the AWS Cloud9 environment
AWS Cloud9 environment is provisioned on top of an underlying AWS EC2 instance or our own server.
Following are the steps to provision a Cloud9 environment –
- Sign in to the AWS console
- Search Cloud9 in the services
- Select Cloud9 service from list, it will redirect to Cloud9 page
- If you’re the only one using your AWS account, you can access Cloud9 service directly by accessing the link https://console.aws.amazon.com/cloud9/.
- If you want to create your Cloud9 environment in any specific Region then change the region from top navigation bar. Here we are using N. Virginia (us-east-1)
6. Click Create environment button.
7. Enter Environment name and description (optional) on the page
8. Click Next Step on the screen.
It will redirect to configuration settings. Select “new EC2 instance” as the Environment type.
9. Select Instance Type and Platform.
Depending on what you want to use your Cloud9 environment for, you can select micro, small or large instance type.
For example, if you plan to use this environment as your full-fledged IDE for development purposes, you can select a small
or large
instance type.
On the other hand, if you plan to use this primarily to make minor changes and view code, you can select a micro instance.
10. Select Cost-saving setting
Cloud9 waits the amount of time specified in this field before shutting down the underlying Amazon EC2 instance for the environment.
11. (Optional) Expand Network settings (advanced).
Keep default VPC and Subset configuration. This is where you can create and configure your own new VPC and subnet if required.
12. Add tag if required and Click Next
13. Review and then click Create Environment
14. This may take a few minutes to complete.
Once the provisioning is complete, it’ll take you to the environment and display a Welcome page like this –
15. AWS Cloud9 service home screen looks like the below once the environment is created. You can access created Cloud9 IDE directly from here by clicking Open IDE.
3. Setup & Configure Java for your Cloud9 Environment
In this section, we will install and configure Java in our newly provisioned Cloud9 environment.
1. Before installing Java, let’s check whether it is already installed or not. To check open terminal session in the AWS Cloud9 IDE, run the below command.java --version
In the screenshot above, we can see that Java is already installed.
2. In case Java is not installed, you can follow the below steps to install. In this example, we are installing OpenJDK 8
sudo yum -y update
sudo yum -y install java-1.8.0-openjdk-devel
sudo yum install default-jdk // install latest JDK which available in launched instance.
sudo yum install java-11-openjdk-devel // install open sdk 11
3. To install a particular version, run yum search
command to list all the available Java packages and then yum install
the required package.
> yum search all java
4. Let’s create a sample Java file to test our environment.
In the AWS Cloud9 IDE, on the menu bar, select File and New File. create a file with the following code, and save the file with the name Cloud9Test.java
.
public class Cloud9Test {
public static void main(String []args) {
System.out.println("Hello, AWS Cloud9 IDE");
}
}
5. In the Cloud9 terminal, execute the below commands to compile and run Cloud9Test.java
.
javac Cloud9Test.java
java Cloud9Test
At this point, our Cloud9 environment is ready for us to help manage our Java based projects, including Spring Boot.
4. Build and Run a Spring Boot App in Cloud9
In this section, we will look at how to use our recently setup Cloud9 Java environment for developing or managing spring boot applications
4.1 Preparing the sample Spring Boot application
Step 1 – Navigate to Spring Initializer
Step 2 – Select the type of project and the language. We will create a Gradle project with Java as the language.
Step 3 – On Spring Initializer, configure the project meta data. We will leave all defaults as is, except that we’ll select Java 8.
Step 4 – On Spring Initializer, add “Spring Web” as a dependency.
At this point, click the “Generate” button. This will download a project zip file that includes all the scaffolding code including Application and build files for our our spring boot project.
Step 5 – Unzip the zip file downloaded in the previous step in a folder called “demo”.
Alternatively, prepare to use the following curl command from the terminal of your Cloud9 IDE
curl https://start.spring.io/starter.zip -o demo.zip
4.2 Setup spring-boot project files on our Cloud9 IDE
Step 1– Go to the AWS Cloud9 IDE instance we configured above in the article and select File menu and then Upload Local Files
Step 2 – Upload demo folder that we prepared from Spring Initializer in the 4.1 section above.
Alternatively, to bypass the entire process of downloading of the project locally and uploading it to Cloud9 manually, use the Cloud9 terminal and execute the following curl command
$curl https://start.spring.io/starter.zip -o demo.zip
This will download the demo zip file directly to your Cloud9 environment. Then, use the “terminal “unzip” command again from the same terminal to unzip the demo.zip and get the folder structure in the screenshot above.
At this point, we have our spring boot application code in our Cloud9 environment.
4.3 Making changes, building and running the spring-boot application on Cloud9
Step 1 – Let’s make a quick change to the DemoApplication.java in the src folder. Use the code below for this java file –
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@SpringBootApplication
@RestController
public class DemoApplication {
@RequestMapping("/")
public String cloud9Test() {
return "Welcome to Cloud9 Spring Application";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Step 2 – Select the demo folder on the navigator, right click and then click Open terminal here. This should open Cloud9 terminal. Them, run the below gradle command to build our application jar file.
$gradle clean build
Step 3 – You should see the build start and succeed. The application jar file will be created at the end of the build.
Step 4 – In the Cloud9 navigator section, select the build/libs folder where the application jar is generated.
Step 5 – Right click on this folder and open a terminal. In the opened terminal, start the application using the command below –
$java -jar demo-0.0.1-SNAPSHOT.jar
Step 6 – Spring boot application process will initialize and we should see a “started” message if the application starts successfully.
At this point, Cloud9 will also prompt us with a URL at which our application will be available over https. Copy that URL.
Step 7 – Navigate to the application URL.
We should see the output of our spring boot application’s controller. Note that the URL is powered by the processes running within our Cloud9 environment. So this is accessible only until our terminal process is running in the Cloud9 IDE.
At this point, we have successful built and run a spring boot web application in our Cloud9 environment. Yay!!
5. Conclusion
AWS Cloud9 is a versatile and easily accessible cloud IDE.
In this article, we saw how to set up Cloud9 and configure it for our Java development needs.
6. Related Articles
- Common Cloud9 Questions
- Troubleshooting AWS Cloud9: What to do if Cloud9 Cannot Resolve Hostnames
- If you are preparing for a Cloud Developer Interview read this Java Developer Q&A article
- Step by Step Guide on deploying Spring Boot applications to AWS