Hello friends today in this tutorial, we will check how we can deploy our spring boot application in AWS EC2 easily. Now a days Spring boot and AWS has got lots of momentum and lots of projects are being deployed in AWS with underlying framework as spring boot. We will today see with the very simple application and check the steps in detail on the configuration part so that application got deployed smoothly.
What we will do in the hands-on section
Spring boot project can be easily deployed in AWS using an AWS service called Elastic Beanstalk, which is a PaaS provided by AWS which can take care of provisioning and deploying our application in AWS with few clicks.
Today we will not go the Elastic Beanstalk path, rather we will manually deploy the application using EC2 User data script to automate the environment setup and few more steps.
So, in today’s demo we will mainly do that following things to test our application
- Create Spring boot project from scratch
- Create one sample application, this could be any application, but for simplicity we will expose one simplest REST Api and deploy that in AWS EC2.
- Test the application in local
- Develop the required configuration components for AWS
- Deploy and test the application from AWS deployed URL
Few Key Terminologies
Before starting the actual hands on part, let’s first familiar with the technologies we will use today. Hope that the readers who have reached this section of the article, those terminologies are known to you, still below are some bullet points regarding those.
– Amazon Web Service, it is the cloud offering from Amazon and currently very popular. Currently there are around 100 services available under AWS umbrella which covers many technical domains like IaaS, Paas, SaaS, Networking, Compute, Storage, Bigdata, Machine Learning, AI, etc. AWS currently the market leader in cloud offering domain and many companies are adopting AWS for cost effectiveness and other cloud parameters like easy scalability, High Availability, Fault Tolerance. AWS cost model is mostly pay per use, so customers have to pay only the services they have used based on the attributes of pricing for that service.
- Elastic Compute Cloud is the AWS offering for Infrastructure as service (IaaS). It provides virtual servers in cloud which is very cost effective and highly customizable based on the demand.
– Application framework based on Spring framework with lots of extra feature. Right now, one of the most popular framework for building web development specially Microservice.
So now let’s start our hands on to proceed with the steps mentioned above.
Creating Spring boot project
As a first step we need to Hire Java developers for one spring boot project which will be deployed in AWS EC2. The easiest way to do that is to
- Go to https://start.spring.io/, generate one spring boot project skeleton by providing the Artifact id, Group ID and required dependencies, for our testing we need to just select web starter dependency. After entering all that information, we can generate the project using Generate Project button like below.
- Once we have downloaded the project, we need to unzip that in suitable location and import that project in eclipse or your favorite IDE as maven project. Now try one initial mvn clean install command on that project to ensure that all the spring boot related jars got downloaded properly.
Adding sample Rest Controller
Now we will write one simple REST endpoint for our testing. The code is very simple, it is just the famous hello world, so here it is
Test the Rest Controller in Local
Now we need to test once this Rest Api to verify that everything is working as expected. To do that just run the application as spring boot application from eclipse by running the @SpringBootApplication annotated class present in the same package which is automatically got generated initially while we created the project in the earlier step.
Once the application will run in local machine, it will by default run in 8080 port and access the below URL from browser, if everything goes well you should get response.
Create Required Configurations for AWS deployment
As we have already highlighted at the beginning, we will not use Elastic Beanstalk to deploy the application in AWS, rather we will go with the conventional way to deploy the application manually by using one script that will be provided while we create the EC2 server.
For this I have created one script in file EC2_USER_DATA.md placed in the home directory of the attached project. Below is the content of that file. In short it will do the following steps while creating the EC2 Server.
- Install required updates to the Ec2 operating system, so that we don’t face any OS related issue.
- yum update -y
- Install Java 1.8 and httpd server in EC2.
- Remove old version of Java
- Create a directory in Ec2 server
- Copy the Spring boot application jar from S3 service to a directory in the newly created EC2 server
- Give required permission to the newly copied application file.
- Now do the necessary changes in HTTP to redirect 80 port traffic to 8080 as our actual application will run in 8080 but from EC2 we will access that from port 80 externally.
- Then start the HTTP server as service, so that EC2 restart can again start the application automatically.
- And finally start the spring boot as service as well, so that while EC2 restart the application comes up properly
We need to do another configuration in pom.xml to make the jar executable by adding this section in pom.xml
Here is the User data script. User data is the textual script provided while creating Ec2 server, so that few things can be automated in the newly created server.
Now do the final build and Upload the file in S3 service
Now we will deploy this application to AWS, to do that we first need to upload the deployable artifact to S3 from where the above-mentioned script will copy the jar and place in EC2 server.
To do that
- first build the artifact in local machine by maven command mvn clean install
- Now we need to create one bucket called springbootprojects in AWS S3 and upload the jar file generated in the target directory of the project
- Make sure you create the bucket in ap-south-1 region in S3, as our user data script is written as per that only, if you create it in different region then please change the region in the User data script, otherwise Ec2 will not be able to copy the file.
- Here is the guideline on how to create and upload files in AWS S3. https://docs.aws.amazon.com/AmazonS3/latest/user-guide/upload-objects.html
Create EC2 role for S3
Now we need to create one IAM role for S3 which needs to assign to Ec2 server to access the S3 bucket that we have created earlier. This will enable Ec2 server user data script to copy the application executable jar file to EC2 directory for deployment. I am not mentioning it in details, AWS has very good documentation, Please follow and create once EC2 role for S3 read only access.
Create one Security Group for accessing the Application from outside AWS
Next prerequisite step is to open port 80 to outside world. This is kind of standard firewall open in networking terminology. In AWS this can be achieved by creating/modifying one Security Group and assign to the EC2 instance. In security group, we should only expose the required ports to the outside world for security reason. For our scenario, we will only expose 80 and 22 for HTTP access and TCP access so that log in to the Ec2 instance through terminal and also access the web application from browser/REST client/another outside client application.
AWS has very good documentations as well on how to create security group, you can follow the below links or any other links that are available online.
EC2 instance and deploy the application
Now we have completed the required prerequisite like unloading the jar file in S3 and creating security group to open port for access application and finally the Ec2 role for S3 access.
Now we need to create one EC2 instance and deploy the application. To create EC2 instance we need to do the below steps
- Login to AWS console and select EC2 service
- Now create Launch Instance button from the EC2 landing page
- Then select the AMI and go to the next page
- Now select the instance type (Choose only the free tire eligible, else charges will apply)
- Now in instance details page we need to select two important things, rest of the things you can keep as default like VPC, subnet etc.
- In this page first select the IAM role that we have created earlier, in my case I named that role as AmazonS3FullAccess
- Then we need to paste the USER DATA in the Advance Details section. You can copy the whole code as motioned above/attached code base.
- Now choose/edit the security group to allow HTTP traffic to port 80
- Finally choose one key pair and finish the instance creation step by clicking Launch Instance button
- Now go back to the EC2 landing page, you will see instance is being launched. Wait till instance state is running and all status checks are completed. You can see those in the EC2 page itself
- Now note down the public DNS/IP address from the instance description tab of the same page, we will use this DNS/IP to access the application
Access the application deployed in AWS
As we had noted the IP/DNS of the server where the application has been deployed, we can easily access the application with this IP/DNS.
Congratulations, if everything goes well you have now developed one sample spring boot REST web service and deployed in a single EC2 instance and able to access that application from outside AWS. This is one way of deploying application to EC2, there are obvious other ways as well which we will see in next articles.