Spotted a bug? Have a great idea? Help us make google.dev great!

Cloud Shell is a browser-based command-line tool to access Google Cloud resources. Cloud Shell makes it easy to manage your Google Cloud Console projects and resources without having to install the Cloud SDK and other tools on your system.

You'll use Cloud Shell to build and launch a Spring Boot Java app without ever leaving your browser. To do so, you'll use the sample code from Building an Application with Spring Boot.

What you'll learn

  • How to use Cloud Shell
  • How to create a simple Spring Boot Java app in Cloud Shell
  • How to edit the Java app using the code editor in Cloud Shell
  • How to launch the Java app from Cloud Shell

What you'll need

  • A Google Cloud project
  • A Browser, such Google Chrome
  • Familiarity with standard Linux text editors, such as Vim, Emacs, and GNU Nano

How will you use use the codelab?

Read only Read it and complete the exercises

How would you rate your experience with building HTML and CSS web apps?

Novice Intermediate Proficient

How would you rate your experience with Google Cloud services?

Novice Intermediate Proficient

Self-paced environment setup

If you don't already have a Google Account, then you must create one. Sign into Cloud Console and create a new project.

Screenshot from 2016-02-10 12:45:26.png

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in the codelab as PROJECT_ID.

Next, you need to enable billing in the Cloud Console to use Google Cloud resources. Running through the codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running.

New users of Google Cloud are eligible for a $300 free trial.

Cloud Shell

You'll use Cloud Shell, a command-line environment running in Google Cloud. The Debian-based virtual machine (VM) instance is loaded with all the development tools you'll need (gcloud command-line tool, git, and others) and offers a persistent 5GB home directory.

This Debian-based virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. This means that all you will need for this codelab is a browser (yes, it works on a Chromebook).

To activate Google Cloud Shell, from the developer console simply click the button on the top right-hand side (it should only take a few moments to provision and connect to the environment):

activateCloudShell.png

Click the "Start Cloud Shell" button:

Screen Shot 2017-06-14 at 10.13.43 PM.png

Once connected to the cloud shell, you should see that you are already authenticated and that the project is already set to your PROJECT_ID :

gcloud auth list

Command output

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)
gcloud config list project

Command output

[core]
project = <PROJECT_ID>

Cloud Shell also sets some environment variables by default which may be useful as you run future commands.

echo $GOOGLE_CLOUD_PROJECT

Command output

<PROJECT_ID>

If for some reason the project is not set, simply issue the following command :

gcloud config set project <PROJECT_ID>

Looking for your PROJECT_ID? Check out what ID you used in the setup steps or look it up in the console dashboard:

Project_ID.png

IMPORTANT: Finally, set the default zone and project configuration:

gcloud config set compute/zone us-central1-f

You can choose a variety of different zones. Learn more in the Regions & Zones documentation.

Start Cloud Shell

Navigate to Cloud Console from another browser tab or window and log in.

Open Cloud Shell by clicking on the following icon in the top-right part of the screen:

activateCloudShell.png

After the Spring Boot command-line tool is installed, you can initialize and bootstrap a new "Hello, World" web app.

$ curl https://start.spring.io/starter.tgz \
  -d dependencies=web -d baseDir=helloworld | tar -xzvf -

That creates a new directory with a new Maven project, along with Maven's pom.xml, a Maven wrapper, and an app entry point.

Open the code editor by clicking Launch code editor in the Cloud Shell menu.

After the code editor opens, find the helloworld/src/main/java/com/example/demo/DemoApplication.java file.

After the code opens, create a new RESTful controller to respond "Hello." In the DemoApplication.java file, add a new Helloworld class definition in addition to the current one.

src/main/java/com/example/demo/DemoApplication.java

package com.example;

...

// Add the import
import org.springframework.web.bind.annotation.*;

@SpringBootApplication
public class DemoApplication {
...
}

// Add the controller
@RestController
class Helloworld {
        @GetMapping("/")
        public String greet() {
                return "Hello!";
        }
}

Save the file!

You can start the Spring Boot app normally with the Spring Boot plugin.

$ cd $HOME/helloworld
$ ./mvnw -DskipTests spring-boot:run

After the app starts, click on the web preview icon in the Cloud Shell toolbar, then click Preview on port 8080.

A tab in your browser opens and connects to the server that you started.

You learned to build and launch a new Java web app directly from Cloud Shell.

Learn more

License

This work is licensed under a Creative Commons Attribution 2.0 Generic License.