How to Deploy a Spring Boot REST API on AWS Elastic Beanstalk

Bryan BarrettBryan Barrett
4 min read

In this article, we will deploy our Spring Boot REST API application to AWS using Elastic Beanstalk. We will explain how to generate the jar file and configure Elastic Beanstalk. When the deployment is complete, we will test our endpoints using Postman. At this point, we will have finished deploying the backend to the cloud, with only the frontend UI remaining.

As always, the video below offers a visual demo of everything covered in this blog for your reference. Continue reading for the step-by-step guide.

Create Snapshot Locally

In a previous blog we covered building and running a Spring Boot REST API locally in IntelliJ. To deploy this to AWS, we need to build a snapshot jar file of our application to upload. A handy maven command, which we can execute from the Terminal window in IntelliJ, is :

mvn clean install package

This will clean the project, build the application, and generate the snapshot jar file in the /target folder

We can confirm this file is generated by navigating to that folder. We will later upload this file:

Create EC2 Role for Elastic Beanstalk

In the previous blog we created an EC2 instance to host our database instance. When we did this the simple setup wizard auto generated the necessary Roles and Security Groups in order for it to have all permissions to function. To keep things modular, we will be creating another EC2 instance to host our REST API. One small prerequisite before doing this is creating a Role with necessary permissions since the Elastic Beanstalk setup wizard doesn't do this for us.

💡
A Role allows EC2 instances to perform actions on your behalf. Instead of storing access keys directly on the instance, you can associate a role with an EC2 instance, which will then provide temporary security credentials to applications running on that instance
  1. Navigate to IAM Dashboard

  2. On the left hand sidebar, Click "Roles"

  3. Click "Create Role"

  4. Leave Trusted entity type as "AWS Service", and under "Use Case", select "EC2". Click Next.

  5. Under Permission Policies, type "ElasticBeanstalk" into the search filter. The 3 policies you need to check are:

    1. AWSElasticBeanstalkMulticontainerDocker

    2. AWSElasticBeanstalkWebTier

    3. AWSElasticBeanstalkWorkerTier

  6. On the next screen, enter a role name. I used "aws-elasticbeanstalk-ec2-role". Click "Create Role" at the bottom.

Create Elastic Beanstalk Application

With our snapshot jar generated and our EC2 role ready, we can create our Elastic Beanstalk application and deploy our REST API.

  1. Navigate to the Elastic Beanstalk Dashboard

  2. Click "Create Application"

  3. On the first page:

    1. Leave "Web server environment" selected.

    2. Enter an application name. This will automatically generate an environment name, which you can leave as is.

    3. Under Platform, select Java if you are following our sample MyNotes application, or choose the appropriate language for your API. The two fields below will be filled with the latest platform versions, which you can leave as is unless your app specifically needs an older version.

    4. Under Application Code, choose "Upload Your Code." In the version label field, input a unique label for your app (e.g., "mynotes-api-0.0.1") in a format that you can increment for future upgrades (e.g., "mynotes-api-0.0.2"). Below, click Local File, then the "Choose File" button, and select your snapshot jar file.

    5. Click Next at the bottom of the page.

  4. On the "Configure service access" page:

    1. Under Service Role, click "Create and use new service role".

    2. Under EC2 key pair, select your key pair value created in the previous blog. This isn't required however.

    3. Under EC2 instance profile, select the EC2 role we created earlier in this article.

    4. Click Next.

  5. On the "Set up networking, database, and tags" page:

    1. Select our default VPC in the dropdown.

    2. Under instance subnets, let's select us-east-1a, us-east-1b, and us-east-1c. If you see multiple options, choose the ones labeled "RDS-Pvt-subnet".

    3. At the bottom, click Skip to review.

  6. On the review page, we have just one thing to edit

    1. Scroll down to Step 5: Configure updates, monitoring, and logging

    2. Click Edit

    3. Scroll down to Environment properties

    4. Add any key/value pairs from the application.properties file in our REST API or your equivalent. Note the property name casing format, uppercase and underscores only.

    5. If you're following the MyNotes sample app, you'll need to add the keys in the image above, with the values from the previous blog.

    6. Click Next, back on the review page, and click Submit

Within a few minutes, your application will be ready. On the overview page, you can click the Domain value to open your default endpoint in the browser. We will use this in Postman to test our other endpoints.

At this point, our REST API is live on AWS and successfully communicating with our AWS database. The only remaining step is to deploy our frontend, which we will cover in the next blog, and link it up.

0
Subscribe to my newsletter

Read articles from Bryan Barrett directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Bryan Barrett
Bryan Barrett