---
# System prepended metadata

title: AWS Series - EC2 instance configuration & Spring Boot app deployment
tags: [AWS]

---

*Author: [Vincent Lau](https://www.linkedin.com/in/vincent-lau-30435bb6/)
Note: This material is intended for educational purposes only. All rights reserved. Any unauthorized sharing or copying of this material, in any form, to any individual or party, for any use without prior permission, is strictly prohibited.*

# AWS Series - EC2 instance configuration & Spring Boot app deployment

After launching the EC2 instance, you have to access the instance and configure and setup the server. 

## Remote Connection Software
We need an open-source software to perform FTP/ SFTP (File Transfer Protocol) and SSH (Secure Shell) connecting with the remote server. **CyberDuck (MacOS) & Putty (Windows) are one of the options to achieve**.

[Cyberduck](https://cyberduck.io/download/) is an open-source FTP (File Transfer Protocol), SFTP (Secure File Transfer Protocol), and WebDAV (Web Distributed Authoring and Versioning) client for macOS and Windows. It provides a user-friendly interface for connecting to and managing files on remote servers or cloud storage services. Cyberduck supports various protocols and offers features such as file synchronization, encryption, and integration with external editors.


[PuTTY](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html) is a free and open-source terminal emulator, serial console, and SSH (Secure Shell) client primarily used on Windows operating systems. It allows users to establish secure remote connections to servers and network devices using SSH, Telnet, or serial connections. PuTTY provides a command-line interface for managing remote systems, executing commands, and transferring files securely. It also supports various encryption and authentication methods.

## Health Check Before Login
**[Before Login]** You can find the public address on the AWS EC2 instance page (IP or DNS).

![](https://hackmd.io/_uploads/Byw5J_Sv3.png)


**[Before Login]** Make sure your private key is in ~/.ssh folder.

![](https://hackmd.io/_uploads/SJsRkdSDn.png)

**[Before Login]** Run this command, if necessary, to ensure your key is not publicly viewable.
``` shell
# make it non-public viewable
chmod 400 ~/.ssh/ec2-demo-key-pair.pem
```
![](https://hackmd.io/_uploads/ry0v5tSvn.png)

## Steps to login EC2 instance

Launch Cyberduck or Putty, upload the jar file to AWS server via SFTP protocol (port 22). That's why we had set this inbound rule (port 22) in our security group for the EC2 instance.
- URL: The public address shown Instance Detail Page
- Username: ubuntu (default user)
- SSH Private Key: Select your own private key 

![](https://hackmd.io/_uploads/r10yiFBP2.png)

**[After login success]** You should find the screen similar to below. It shows the current folder. 

![](https://hackmd.io/_uploads/r1cfjYrv2.png)


## Steps to configure the EC2 instance

#### Connect to instance via Terminal SSH

``` linux
ssh -i "~/.ssh/ec2-demo-key-pair.pem" ubuntu@ec2-54-205-68-248.compute-1.amazonaws.com
```

![](https://hackmd.io/_uploads/SkI9iFHPn.png)


### Download & Install JDK

**Download JDK 17**. Input the following command after connection success.

To get the most recent archive, go to the [JDK 17 releases page](https://jdk.java.net/17/).

``` linux
wget https://download.java.net/java/GA/jdk17/0d483333a00540d886896bac774ff48b/35/GPL/openjdk-17_linux-x64_bin.tar.gz
```

Result:

![](https://hackmd.io/_uploads/ryFontSPn.png)

Using the tar command, **extract** the downloaded OpenJDK 17 archive file.

``` linux
tar xvf openjdk-17_linux-x64_bin.tar.gz
```

Result:

![](https://hackmd.io/_uploads/ByhX6tHP3.png)
![](https://hackmd.io/_uploads/H16spKSD2.png)

**Move** the files from result folder to /opt directory.

``` linux
sudo mv jdk-17 /opt/
```

![](https://hackmd.io/_uploads/rJKSCKBwn.png)


Now, **configure** Java environment:

``` linux
sudo tee /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=/opt/jdk-17
export PATH=\$PATH:\$JAVA_HOME/bin
EOF
```

![](https://hackmd.io/_uploads/r1b_CKHv3.png)

**Source** (execute) the profile file:

![](https://hackmd.io/_uploads/HkhiAKHD3.png)

Check the java command **JAVA_HOME**:

``` linux
echo $JAVA_HOME
```

![](https://hackmd.io/_uploads/rywgJ5Svn.png)


Check the **Java Version**:

``` linux
java --version
```

![](https://hackmd.io/_uploads/B1x-JqHwn.png)

Installation Completed! Now you may remove the downloaded package.

``` linux
rm openjdk-17_linux-x64_bin.tar.gz
```
![](https://hackmd.io/_uploads/By_rg5Svn.png)

![](https://hackmd.io/_uploads/Hy4bg9Hv3.png)


## Steps to deployment Spring Boot App

Now, you can **drag & drop** the jar files from **[~/.m2]** or **[project-folder/target]**.

i.e. Make sure you had "mvn clean install" for your Spring Boot App. The corresponding jar files will be generated.

![](https://hackmd.io/_uploads/ryuo-5HP3.png)

- Drop the file to "CyberDuck" or "Putty" from **~/.m2/**

![](https://hackmd.io/_uploads/Sy0SX5Bw3.png)

Uploading ...

![](https://hackmd.io/_uploads/rJEY7cSvh.png)

Completed.

![](https://hackmd.io/_uploads/rJIn75rD2.png)

You can check the result in Terminal as well (after ssh login)

![](https://hackmd.io/_uploads/B1FMNqrv2.png)

Command to run the Spring Boot Apps, with server log.

``` linux
java -jar project-crypto-coingecko-0.0.1-SNAPSHOT.jar > coingecko.log 2>&1 &
```

In the command java -jar project-crypto-channel-0.0.1-SNAPSHOT.jar > filename.log 2>&1 &, the 2>&1 portion is used for redirecting the standard error (stderr) stream to the same location as the standard output (stdout) stream.

**`\>`** is the output redirection operator in the shell. It redirects the standard output to a file specified after it. In this case, filename.log is the file where the standard output will be redirected.

**`2`** represents the file descriptor for the standard error stream.

**`&`** indicates that what follows is a file descriptor, not a file name.

**`1`** represents the file descriptor for the standard output stream.

**`2>&1`** is the syntax used for redirecting the standard error stream to the same location as the standard output stream. In other words, it combines both streams into a single output location, which in this case is the filename.log file.

With **`2>&1`**, any error messages generated by the Java application running the JAR file will also be redirected to the filename.log file along with the standard output.

Overall, the command **runs the Java application as a background process (`&` at the end)**, redirects both standard output and standard error to the filename.log file (> filename.log 2>&1), and starts the process.

![](https://hackmd.io/_uploads/S1M3E5BDn.png)


Command to view log files:
``` linux
cat my.log
```

Command to kill process listening to a port:

``` linux
# kill process listening to port 8085
kill -9 `lsof -t -i:8085`
```

## Validate Result
After launched your spring boot app, you can test it via the public ip by postman.

Sample Service Endpoint: http://ec2-54-205-68-248.compute-1.amazonaws.com:8085/crypto/coingecko/v1/coin/market

![](https://hackmd.io/_uploads/SyOew9rDh.png)
