Deploy Spring Boot Aplcation on Elastic Bean Stalk Service
Introduction#
In this article, we’ll guide you through deploying a Spring Boot application on AWS Elastic Beanstalk using crucial services like Load Balancers, EC2, Auto Scaling, and RDS. By leveraging these services, you can build a highly available, scalable, and fault-tolerant application infrastructure in the cloud.
1. Elastic Beanstalk#
AWS Elastic Beanstalk is a fully managed platform that lets you deploy and scale web applications easily. It abstracts away the underlying infrastructure, so you can focus on writing your application code rather than managing servers.
Key Features:#
- Supports several programming languages, including Java (Spring Boot).
- Automatically handles infrastructure provisioning, load balancing, scaling, and health monitoring.
- Allows customization for more advanced configuration and fine-tuning.
2. EC2 (Elastic Compute Cloud)#
AWS EC2 provides scalable compute capacity in the cloud. It allows you to run virtual servers (instances) where your application will reside. When you deploy using Elastic Beanstalk, EC2 instances are created behind the scenes to host your application.
Key Features:#
- Provides resizable compute capacity.
- Lets you choose instance types based on CPU, memory, and storage requirements.
- Integrated with other AWS services like Auto Scaling and Load Balancing.
3. Auto Scaling#
Auto Scaling ensures that your application can scale automatically based on demand. It helps in maintaining optimal performance by adding or removing EC2 instances dynamically based on traffic or resource consumption.
Key Features:#
- Scales out (adds more instances) during peak load and scales in (removes instances) during low demand.
- Helps in achieving cost-efficiency by not over-provisioning resources.
- Configured using rules based on CPU usage, network I/O, or custom metrics.
4. Elastic Load Balancer (ELB)#
An Elastic Load Balancer (ELB) distributes incoming application traffic across multiple EC2 instances to ensure high availability and fault tolerance. It helps in maintaining a smooth and balanced user experience by routing traffic to healthy instances.
Key Features:#
- Supports automatic scaling and high availability.
- Redirects traffic based on the health of EC2 instances.
- Supports different protocols like HTTP, HTTPS, and WebSockets.
5. RDS (Relational Database Service)#
AWS RDS is a managed relational database service that supports multiple database engines such as PostgreSQL, MySQL, and Oracle. It provides automatic backups, software patching, scaling, and high availability out of the box.
Key Features:#
- Managed service for relational databases like PostgreSQL, MySQL, and Oracle.
- Supports multi-AZ deployment for high availability.
- Automated backups, monitoring, and scaling.
let’s dive into the deployment process.#
Steps to Set Up AWS Elastic Beanstalk Environment#
First of all Login to AWS and open Elastic Beanstalk Service from services.
You will see the below page click on create button to create application.
Step 1 : Configure environment#
You will see the below page after clicking on create application button.
Environment tier section select web server environment.
In application information section give application name, as we are going to deploy our basic library management spring boot project so I have given name as Library-management-service
Next in Environment information section give name to environment, we will give as Library-management-service-env-1, there could be many environments under one application. After giving name to environment check the domain availability.
Next Scroll down you will see the Platform section.
Select platform type as managed platform.
Selct Platform as Java, as our employee service application is a spring boot application.
In Platform branch select the branch as 21 latest java version.
In Platform version select the recommended version.
Next In Application code section select sample application for now later we will upload our jar file.
Next scroll down and In configuration presets select Single instance (free tier eligible).
And click on Next button to move to next step.
Step 2 : Configure service access#
Here, you need to assign service role for the instances and the elastic beanstalk so that they can access other services and use them.
In service role section select Create and use new service role.
Give service role name to env as aws-elasticbeanstalk-service-role and it will be created automatically with required permissions.
Now in EC2 instance profile select aws-elasticbeanstalk-ec2-role if it is already created and click next button.
If you have not created service role for ec2 then go to IAM service and click on roles.
After clicking you will be redirected to below page, select service or use case for ec2 and keep all other hings default and click on next button.
On next page you have to add permissions in permission policies search and add AWSElasticBeanstalkCustomPlatformforEC2Role and click on next button.
On the next page give name to role we will give name as aws-elasticbeanstalk-service-role and click on then simply click on create role button.
Step 3 : Set up networking, database, and tags#
In the next step Select default vpc from dropdown in VPC section.
In Instances settings section check the activated box to assign public IP address to the instances.
Next in Instance subnets check all the availability zone.
Next section is Database section we will not configure any DB here as we already have set our RDS instance. And also it is not recommended to configure DB here because if we delete our environment then we will loss our database too.
Next scroll down and simply click on next button.
Step 4 : Configure instance traffic and scaling#
In the next step keep all things default and scroll down and go to capacity section.
In Auto Scaling group select environment type as load balanced if you want to increase ec2 instance count as traffic increases.
Select min and max count of instances, we will keep min 1 and max 4.
Keep On-Demand instances, rest keep all default.
Next scroll down and you will see the Architecture keep x86_64.
In Instance type select t3 micro or other as per your requirement.
Next is Scaling triggers where we will decide how and when our instances will be scaled up and scaled down.
Keep Metric as CPUUtilization as we will decide the trigger on CPU utilization on instances.
Keep statistics as average and Unit as Percentage
Keep period as 1 min and breach duration as 5 min with upper threshold as 80% and lower threshold as 40%.
Keep scale up increment as 1 and scale down increment as -1
Overall this settings says if the CPU utilization is beyond 80% over a period of 5 min when it is checked every 1 min it will scale up 1 instance. Similarly scale down by 1 instance if CPU utilization is below 40%.
Next scroll down you will see Load balancer settings.
Select visibility as public
Check all availability zones in load balancer subnets.
Next in Load balancer type select Application load balancer and dedicated
In Listners section add HTTP listener with port 80 and enabled, you can add more.
Next in processes you will see the health check and default it is “/” path which in your code you have to add controller for health check.
In the next sections Rules and log file access keep it default and click on Next button.
Step 5 : Configure updates, monitoring, and logging#
In the next step, in monitoring section select basic monitoring and keep rest as default.
Next in Managed platform updates you can check box Activated to get weekly updated window of your environment.
Next in email notifications you can add your email to get notification about changes in your environment.
Next after scrolling you will see Rolling updates and deployment section.
In deployment policy select All at once and batch size type as fixed if you want to deploy the new version of code all at once to all instances or you can select rolling option from drop down so that code will be deployed one by one to instances. Rest keep all as default and scroll down.
Next scroll down and you will get Environment properties section, here you have to add all the environment properties required for your spring boot application and simpl click next button.
Step 6 : Review#
In the last step you will see the review page review all your configuration and click on submit button.
Your elastic beanstalk env will be launched.
Once it is launched you will see the health status as green
Now click on domain to see the basic web application.
You will see this page.
So now you have successfully launched your elastic beanstalk environment with basic web application and now we have to deploy our spring boot library management applications jar file to elastic beanstalk environment.#
To upload the jar file click on upload and deploy button.
Click on choose file and select the jar file of your application.
After uploading file below you will see the deployment preference again, keep Rolling.
Click on Deploy Button.
Once the deploy of jar file is completed you will see health status as green and now click on your domain to see if you get ok response from your health controller.
So congratulations you have successfully deployed your jar file to elastic beanstalk environment.#
In this article, we've walked through the deployment of a Spring Boot application on AWS Elastic Beanstalk, utilizing essential services like Load Balancers, EC2, Auto Scaling, and RDS. By leveraging these services, you've learned how to create a scalable, high-availability architecture that can automatically adjust to changes in demand while maintaining performance. This setup not only simplifies your deployment process but also provides a robust infrastructure for your applications in the cloud. With the steps outlined, you should now be well-equipped to deploy your own Spring Boot applications effectively on AWS.