Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy a Spring boot program to Amazon Elastic Beanstalk?

I'm very new at both AWS and Spring Framework.

I've built a simple web program following the getting started guide in Spring website. Also, I already made my Elastic Beanstalk application and environment running Tomcat 8 and Java 8. What I'm trying to is;

  1. build this simple program using Gradle and produce an executable jar( or war).
  2. upload this executable jar(or war) from 'upload and deploy' button in my Elastic Beanstalk dashboard.

I'm using IntelliJ now and my program works well if I double-clicked the produced executable jar. However, after I uploaded this jar to my Elastic Beanstalk, this program just produces HTTP 404 error.

What I don't understand is, if I use Gradle 'build' task or 'bootRun' task, the produced executable jar file works fine. However, if I build a war file through Gradle 'war' task and put it on my local Tomcat, the server produces HTTP 404 error same as AWS.

This is my build.gradle file.

    buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.7.RELEASE")
        classpath("org.apache.tomcat:tomcat-catalina:8.0.28")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'

jar {
    baseName = 'gs-authenticating-ldap'
    version =  '0.1.0'
}

war {
    baseName = 'gs-authenticating-ldap'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

Besides, my program only has two Java classes: src/main/java/hello/HomeController.java and src/main/java/hello/Application.java

src/main/java/hello/HomeController.java is:

package hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @RequestMapping("/")
    public String index() {
        return "Welcome to the home page!";
    }
}

src/main/java/hello/Application.java is:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

As a summary:

  • build 'bootRun' -> browse "localhost:8080" : Works fine.
  • build 'build' -> produce 'xxx.jar(executable)' -> double click to execute this jar -> browse "localhost:8080" : Works fine
  • build 'build' -> produce 'xxx.jar(executable)' -> upload to AWS -> browse "xx.elasticbeanstalk.com" : HTTP 404 error
  • build 'war' -> produce 'xxx.war' -> run this war on local Tomcat using IntelliJ -> browse "localhost:8080" : HTTP 404 error
  • build 'war' -> produce 'xxx.war' -> upload to AWS -> browse "xx.elasticbeanstalk.com" : HTTP 404 error

What I want to do is 1) develop on IntelliJ, 2) build it on Gradle, 3) test it on locally, and 4) upload it on AWS to run. Please, help me.

If you have any further explanation or information, please let me know. Thanks!

like image 669
byron1st Avatar asked Jan 08 '23 04:01

byron1st


1 Answers

I had a similar issue, what you need to do is first make your Application class extends SpringBootServletInitializer:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

It will initialize your app using Spring Framework’s Servlet support (Tomcat will be able to register the servlet and start the app).

Then you have to tell Gradle that the environment runtime will be provided by the application server. To do that add the following line under dependencies:

dependencies {
    // …
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    // …
}

Build a new war (build war) and deploy it.

For more information here is the documentation on how to deploy a Spring Boot app as a war: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html

like image 157
Benoit Wickramarachi Avatar answered Jan 16 '23 18:01

Benoit Wickramarachi