Spring boot & Angular full stack application deployment with Docker , AWS EC2, AWS S3, jenkins,Github — PART 2: Configure EC2 and deploy spring boot image
Introduction :
As we already mentioned in part 1 of this article series , part 2 mainly focus on configuring Jenkins in an EC2 environment.
If you come across this part first , I recommend you to start from part 1 to understand the main objective of this article series to better understand the pipelines we’re building.
Before getting started we should first have a general idea about Aws EC2.
Amazon Elastic Compute Cloud (EC2) is a web service provided by Amazon Web Services (AWS) that allows users to rent virtual servers, known as instances, in the cloud.
so as we discussed earlier our main objective of this part is to create and configure ec2 and then deploy spring boot & postgres image.
Step 1 : Create EC2 instance
Search for the term ec2 in the search bar of aws
then in the ec2 dashboard click on Lunch instance
now , give the instance any name you want and chose ubuntu as an OS
then , create a key pair by clicking on Create new key pair
enter a key name of your choice and click on Create key pair , ensure the file is located in a safe place. it might be needed if you want to connect to your instance from another machine
Set network settings as follows (It’s not recommended to allow all requests but we’re avoiding wasting time for learning purposes )
Then you could click on Lunch instance to create the instance
Step 2 : Configure docker in our created EC2 instance
first of all , wait until Status check value is 2/2 checks passed
then select the instance and click on connect
Leave settings as they are and click on connect
Now you should be seeing a terminal screen
Let’s configure docker now
Run the following commands
1. Update Package Index:
sudo apt update
2. Install Dependencies:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
3. Add Docker GPG Key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
4. Set up Stable Docker Repository:
echo "deb [signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
5. Update Package Index Again:
sudo apt update
6. Install Docker and Docker Compose:
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose
Now check if docker is installed in the instance by running the following commands
docker --version
docker-compose --version
Congratulations 🎉 , you’ve successfully created an EC2 instance and installed docker.
Now you should push docker image we’ve created earlier to docker hub
I’ve wrote a full article about it you could check it :
Step 3 : Pull docker and postgres images using dockercompose
run the following commands to create docker compose file
touch docker-compose.yml
now let’s configure our docker compose file
nano docker-compose.yml
the file should look like this :
version: '3'
services:
postgres:
image: 'postgres:latest'
environment:
- POSTGRES_DB=database
- POSTGRES_PASSWORD=password
- POSTGRES_USER=username
ports:
- "5432:5432"
spring-app:
image: {pushed_image_name}
ports:
- "8080:8080"
depends_on:
- postgres
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/database
- SPRING_DATASOURCE_USERNAME=username
- SPRING_DATASOURCE_PASSWORD=password
Save the file. and run the following command to build and run the containers at once
docker compose up --build
Step 4 : Test deployed spring boot image API
Go back to instance dashboard and copy public IPV4
now go to postman or any API testing tool of your choice and test any endpoint of your choice
Congrats , you’ve just deployed your spring boot application to Aws EC2 ! 🎉
Conclusion :
Having skills in aws services and ecosystem in general is one of the most demanded skills in today’s competitive job market. in this article we’ve learned the basics of EC2 and docker.
in the next part we’ll cover the following topic : Deploying angular application to aws s3
see you soon !