Deploying a Spring Boot application on AWS Elastic Beanstalk
Step 1: Prepare Your Spring Boot Application
Create a Spring Boot Application: Ensure that your Spring Boot application is working locally. You can use Spring Initializr to create a new Spring Boot project.
Package the Application: Use Maven or Gradle to package your application into a .jar or .war file. For Maven, run the following command:
mvn clean install
Step 2: Set Up AWS Elastic Beanstalk
1. Sign in to AWS Management Console: Log in to the AWS Management Console.
2. Create an Elastic Beanstalk Application:
- Go to Elastic Beanstalk in the AWS Management Console.
- Click on Create a new application.
- Give your application a name (e.g., my-spring-boot-app).
3. Create an Elastic Beanstalk Environment:
- Choose Web server environment for a Spring Boot application.
- Select the platform as Tomcat (Elastic Beanstalk uses Tomcat to run .war files, but Spring Boot typically runs as a standalone .jar).
- For Application Code, select Upload your code and upload the .jar file that you generated earlier.
- Configure any necessary environment settings such as instance size, VPC, and security groups.
Step 3: Configure Elastic Beanstalk Environment
1. Elastic Beanstalk Configuration:
- Choose the platform: Select Tomcat for Spring Boot (even if you are deploying a .jar file).
- You can use the default configuration for most cases. You may also want to change environment variables or specify a different instance type depending on your application's requirements.
2. Configure Application Deployment:
- Ensure the application package is a .jar file (Spring Boot uses embedded Tomcat by default, so no need for an external Tomcat server).
- Under "Application Version", upload your .jar file.
Alternatively, you can use the Elastic Beanstalk CLI (eb cli) to create and deploy your application. Below are the steps for using the eb CLI.
Step 4: Deploy Using AWS EB CLI
1. Install AWS EB CLI: If you haven’t already, install the AWS Elastic Beanstalk CLI on your machine. Follow the instructions in the AWS documentation.
2. Initialize EB CLI: Navigate to the root directory of your Spring Boot project and run:
eb init
eb create
eb deploy
Step 5: Monitor and Troubleshoot
1. Access Logs: If your application doesn’t work as expected, you can retrieve logs from Elastic Beanstalk by running:
eb logs
Step 6: (Optional) Set Up a Custom Domain and HTTPS
- Custom Domain: If you want to use a custom domain for your Spring Boot app, configure a domain in Route 53 or another DNS provider and point it to your Elastic Beanstalk environment's URL.
- HTTPS: To enable HTTPS, you will need to set up an SSL certificate through AWS Certificate Manager (ACM) and configure the Elastic Load Balancer (ELB) to use the certificate.