Deploy your application with EC2, Docker, Spring boot using AWS CLI

Jamal Errakibi
6 min readMar 8, 2020

--

In this tutorial, i will show you step by step how to create a docker image of your spring boot project and deploy it on an EC2 instance using AWS CLI (command line interface)

Here is the table of contents so you can jump to a specific section you’re interested in:

1- Install AWS CLI on your local machine

2- Setup EC2 instance

  • choose free tier AMI
  • create key pair
  • create a security group
  • ssh to the instance

3- Create docker image for your spring-boot project

4- Run container in EC2 instance

  • install docker on EC2
  • Run Container (Testing application)

Prerequisites!

Make sure you have an AWS account. If not head over here and create one. Amazon give’s you 12 months free tier to play with their services.

Additionally, you need to install docker on your local machine. This link guide you through the steps to install it.

1- Install AWS CLI

In this tutorial we are going to use AWS Command Line Interface instead of console.

If you’re using Windows, you need to download the .msi file from here and place it under your PATH folder.

To check if everything goes well, type aws --version

More details can be found on AWS Documentation.

2- Setup EC2 instance

EC2 (Elastic Compute Cloud) is your virtual computer where you can run your own application it provide a resizable compute capacity in the cloud. You can boot an AMI to configure your virtual machine (which amazon calls an instance).

The following command shows how to launch your EC2 instance

As you may notice there is some parameters that you should replace with your own :

--image-id ami-xxxxxxxx

--key-name MyKeyPair

--security-groups-ids sg-903004f8

Let’s deep dive in each one:

1- Choose free tier AMI

--image-id ami-xxxxxxxx :

You should provide here your own AMI, the main component of an AMI is a file system image that contain your operating system.

You must select an AMI to use. As you select an AMI, consider the following requirements you might have for the instances that you’ll launch:

  • The Region
  • The operating system
  • The architecture: 32-bit (i386), 64-bit (x86_64), or 64-bit ARM (arm64)
  • The provider (for example, Amazon Web Services)

See AWS Marketplace to choose your appropriate AMI, and follow the instruction until you get the code specific for this AMI.

2- Create key pair

--key-name MyKeyPair

A key pair is a combination of a public key that is used to encrypt data and a private key that is used to decrypt data

You need a key pair to be able to connect to your instances. For more information about key pair.

First question you may ask, how can i generate my own key pair to replace it in the command above.

To create a key pair, use the create-key-pair command with the --query option, and the --output text option to pipe your private key directly into a file

$ aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text | out-file -encoding ascii -filepath MyKeyPair.pem

Make sure you generate MyKeyPair.pem file, we will need it to ssh to our instance

3- Create a security group:

--security-groups-ids sg-903004f8

Security Group = a “firewall” securing Instances

A security group acts as a virtual firewall for your instance to control inbound and outbound traffic. When you launch an instance in a VPC. Security groups act at the instance level, not the subnet level. You can add rules to each security group that allow traffic to or from its associated instance

Let’s create a security group for our instance:

$ aws ec2 create-security-group --group-name my-sg --description "My security group" --vpc-id vpc-1a2b3c4d

(replace --vpc-id parameter with your own)

Thats it :) !!

Now launch your EC2 instance with your own parameters values.

Wait ! Don’t forget to replace — subnet-id parameter with your own.

If everything goes well you should see your instance up & running in AWS Console. Otherwise reach me on twitter, i may give some help.

4- SSH to the instance

It’s time to get access to our EC2 instance. YEEEES :)

Do you remember MyKeyPair.pem file we generated in the second step ?

it’s time to use it!

The following command connect us to our distant EC2 instance:

$ ssh -i /path/my-key-pair.pem ec2-user@<EC2 ip address>

An alternative method to connect to your instance is by using PuTTY, it a free SSH client for windows. make sure to read this article for more information.

Finally we succeed to connect to our instance :)

3- Create docker image for your spring-boot project

Docker is a plateforme that uses OS-level virtualization to deliver software in packages called container

Why docker ? Docker makes things easy when it comes to run softwares without worrying about setup and dependencies.

Let’s take a look at the typical way to create Docker images for our spring boot application and push it to docker hub.

1- Create Spring boot project & generate a JAR file.

First create a new Spring Boot project using start.spring.io:

$ curl https://start.spring.io/starter.zip -d bootVersion=2.3.0.M1 -d dependencies=web -o demo.zip
$ unzip demo.zip

To put everything together, make sure to create a REST controller with a url handler (@GetMapping) for testing purpose:

@GetMapping("/hello")
public String HelloAws() {
return "Hello AWS"
}

Then generate JAR file for your project:

$ mvn package

2- Create docker image using Dockerfile

We will assume that you know how to create and build a docker image for a Spring Boot application. If you don’t, check this link.

To convert JAR file generated by spring boot to docker image, we should create a docker file that looks like this (Add Dockerfile.prod at the top level of your project):

FROM openjdk:8-jdk-alpine
EXPOSE 8080
ARG JAR_FILE=target/demo-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} .
CMD [ "java", "-jar", "/demo-0.0.1-SNAPSHOT.jar"]

Then build your image using command below:

docker build -t jamalerdocker/demo -f Dockerfile.prod .

and push it docker hub (If you don’t have a docker hub account, Go and create one)

docker push jamalerdocker/demo

4- Run container in EC2 instance

let’s connect again to our ec2 instance:

$ ssh -i /path/my-key-pair.pem ec2-user@<EC2 ip address>

1- Install docker on EC2

To get Docker running on EC2 instance you should follow the steps below (these are all assuming you have ssh’d on to the EC2 instance).

  1. Update the packages on your instance

[ec2-user ~]$ sudo yum update -y

2. Install Docker

[ec2-user ~]$ sudo yum install docker -y

3. Start the Docker Service

[ec2-user ~]$ sudo service docker start

4. Add the ec2-user to the docker group so you can execute Docker commands without using sudo.

[ec2-user ~]$ sudo usermod -a -G docker ec2-user

2- TESTING the application

Now you can run your application like this:

[ec2-user ~]$ docker run -p 80:8080 <Your Docker Hub account>/demo

Summary

Congratulations! You’ve just created a Docker container for a Spring Boot app! Spring Boot apps run on port 8080 inside the container by default and we mapped that to the same port on the EC2 instance using “-p” on the command line.

KEEP IN TOUCH

Follow me on Twitter for more content

Resources:

--

--