Hosting Your Spring Boot Application with MySql Database using AWS Elastic Beanstalk.
On 2024-08-27 By atomicadmin
This post notes down all the steps in brief that are required to host your application on Elastic Beanstalk using MySQL RDS. Each step is a pointer to guide you thru the process and while following these steps, you may need to search for some "how to" documentaion on the web to accomplish the task. This overall guide documentation process helps keep the blog brief and helps consolidate the overall process at a single place.
Step 1 : Create a brand new gmail account for managing your web application.
Step 2 : Generate gmail web app password. You must have 2-Step-Verification enabled before setting up an application password.
Step 3 : Create Your AWS Account.
Step 4 : Set up budget and alerts in Billing and Cost Management.
Step 5 : Create MySQL DB Instance in RDS.
- Make sure to use free tier options.
- Choose default VPC, Subnet Group and Security Group.
- Choose the default Certificate Authority.
- Make Public Access to Yes to begin with.
- Choose password authentication as the authentication type and let RDS set the password for you.
- Enter the initial database name as the schema name you want use to connect from your app.
- If not required for your test project, disable automated backups.
- You can enable encryption with the default AWS KMS Key.
- After you click on submit, you can go to the DB instance which is getting created. At this time you can view the credentials and get the password for the master user as created by RDS.
- After the Instance is created, the endpoint for connecting to the DB is updated. Copy the End point and port Number.
- Now go to he DB Instance and click on the security group you had assigned for this. For this security group we need to edit the inbound rule to add your local machine IP Address to connect to port 3306(MySQL/Aurora) using TCP protocol.
- Open MySQL workbench on your local machine to connect and verify to this RDS end point using the master username and password.(https://stackoverflow.com/questions/28429493/how-to-connect-mysql-workbench-to-amazon-rds)
Step 6 : Now time to create the Elastic Beanstalk App.
- Build your spring boot application using maven or gradle and have the jar ready for upload while creating the Elastic Beanstalk Application.
- In IAM create a new role aws-elasticbeanstalk-ec2-role with all AWS Elastic Beanstalk Permissions. If you don't have this, then you see the error "The instance profile aws-elasticbeanstalk-ec2-role associated with the environment does not exist" later in the environment build pipeline.
- In AWS management console, create a new application and the related environment.
- Make sure to select the free tier options while doing so. If your use case suffices to have a single instance deployment, make sure you dont select or create a Load Balancer. You can avoid the cost of load balancer. There are alternatives to set up ssl using cloud front or getting external certificates to enable https on your bean stalk application.
- Selected a Java based Managed platform depending on the Java/Spring version used in your project.
- Upload the jar that you generated in the previous step.
- Create a new service role for Elastic Beanstalk to create and manage your application environment.
- Add your application environment variables in the Environment properties section.
SERVER_PORT : 5000 (This is necessary as elastic beans have an Nginx reverse proxy that will forward incoming requests to internal port 5000).
SPRING_DATASOURCE_URL : <the URL of your database>
SPRING_DATASOURCE_USERNAME : <the user name of your database>
SPRING_DATASOURCE_PASSWORD: <the password of your database>
SPRING_JPA_DATABASE_PLATFORM : org.hibernate.dialect.MySQL8Dialect
SPRING_PROFILES_ACTIVE : <your spring profile to be chosen>
- Now edit the inbound rules of the RDS instance's security Group to allow MySQL/Aurora access from the security group of the Elastic Beanstalk Environment.
Step 7 : To optimize costs, you can scale down the EC2 instances at night. Go to your environment configuration and navigate to instance traffic and scaling. Enter the shutdown and start cron expressions along with start and end dates in time-based scaling section.
You can plan on stopping the rds instance during off hours using lambda functions. (https://aws.amazon.com/blogs/database/schedule-amazon-rds-stop-and-start-using-aws-lambda/)