DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Spring Boot: Handle AWS RDS Password Change or Rotation Without Restarting
  • How to Build a Chat App With Spring Boot and DynamoDB
  • Spring2quarkus — Spring Boot to Quarkus Migration
  • A Practical Guide to Creating a Spring Modulith Project

Trending

  • Stop Building Monolithic AI Brains, Build a Specialist Team Instead
  • Designing Microservices Architecture With a Custom Spring Boot Starter and Auto-Configuration Framework
  • Why Traditional CI/CD Falls Short for Cloud Infrastructure
  • A Software Engineer’s Guide to Thrive in Gen AI Era: Master It or Fade Out
  1. DZone
  2. Coding
  3. Frameworks
  4. Run Your Spring Boot Application on AWS Using Elastic Beanstalk

Run Your Spring Boot Application on AWS Using Elastic Beanstalk

Follow along with this post to learn the basic configuration to get your Spring Boot application running in the cloud on AWS.

By 
$$anonymous$$ user avatar
$$anonymous$$
·
Jul. 15, 16 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
28.7K Views

Join the DZone community and get the full member experience.

Join For Free

beanstalk

Although I previously wrote how easy it is to get your Spring Boot application up and running on AWS by using Boxfuse I think it is a good idea to have a notion of some alternative ways to achieve the same. One of these alternatives is by using AWS Elastic Beanstalk. In this post, I will describe the basic configuration to get your Spring Boot application running in the cloud on AWS. After the Elastic Beanstalk is created, you will have at least one EC2 instance running on AWS with an Elastic Load Balancer in front of it. An Auto Scaling Group is also provided as well as, of course, some Security Groups. When put in a diagram, it looks like this (by the way, the database part will not be used in the example of this post):
aeb-architecture2
I assume you already have an account for AWS. If not, go get your free subscription for the first year (mind the conditions to make sure it stays free).

Set up AWS IAM

The first step to take is to create a new user that will be able to run AWS Elastic Beanstalk. This user will only have these permissions, so if the account details fall in wrong hands, the damage will be minimized (explained here). When you create your user, don't forget to download your credentials for that user, you will need those later on.

I put the new user in a group 'spring-boot-users' and assigned the following roles and policies to that group: 'AWSElasticBeanstalkFullAccess.'

While we are here, you might also want to create the following two roles (they should be created automatically when creating your first Elastic Beanstalk environment with the management console or EB CLI, but I recall I had some issues with them and ended up creating them myself):

Install Elastic Beanstalk client interface

With all this in place, we are almost ready to get our hands dirty. I prefer using the CLI instead of the management console because by using the CLI, you can script the whole thing, which makes life much easier. To install the CLI (I am using a Mac) simply run: brew install awsebcli

Set up the credentials of your new user to be used with the CLI. I add them to my '~/.aws/credentials' file like this:

[default]
aws_access_key_id = 1234567890
aws_secret_access_key = hrhyueiryt983745983erti+UXsF6IvC

[spring-boot]
aws_access_key_id = 0987654321
aws_secret_access_key = hdgdfghkjg384957893745dc7/Xrsekgu7V4


So, in my credential file, I have added a profile 'spring-boot' with the corresponding key and secret.

Set up Your Spring Boot Project

Now everything is set up to get an EB environment up and running. Go to your Spring-boot project folder, and in that directory, perform the following command: eb init.

The configuration for the EB will now be set up based on the input you supply.

In your project folder, you will now see a new folder: '.elasticbeanstalk.'  Within it, there's a file called 'config.yml.' Because I build my application with Maven, I add the following line to the 'config.yml' so it will deploy my latest build snapshot version to the Elastic Beanstalk:

deploy:
 artifact: target/jwt-spring-security-demo-1.0.0-SNAPSHOT.jar

I also enter a value for the profile so the CLI will pick the correct credentials from my credential file that I modified before. The configuration file ends up like this:

branch-defaults:
  master:
    environment: null
    group_suffix: null
deploy:
  artifact: target/jwt-spring-security-demo-1.0.0-SNAPSHOT.jar
global:
  application_name: spring-boot-demo
  default_ec2_keyname: spring-boot-demo
  default_platform: Java 8
  default_region: eu-central-1
  profile: spring-boot
  sc: git


Create the Elastic Beanstalk Instance

If you perform 'eb create' in the prompt and accept the default values, you will get your own Elastic Beanstalk instance with your Spring Boot application on it (please note that it takes few minutes for everything to be up and running). When the creation is finished, you can see your Elastic Beanstalk instance in the management console.

Test the Elastic Beanstalk instance

Now, when you now run 'eb open,' it will start a browser pointing to your application. Right now mine is saying 502 Bad Gateway:
Screenshot at Jul 12 13-16-07
There is one issue left that I have to solve. The default stack is supplied with an EC2 instance that has Nginx running on it as a reversed proxy, and it listens to port 80. My Spring Boot applications run on port 8888, but this port is not accessible from the outside (not opened in the default security group that was created by the Elastic Beanstalk). The issue can be solved in three ways (at least):

  • Open up port 8888 on the EC2 instance and have the Elastic Load Balancer 'talk' to that port. That way we are bypassing the Nginx on port 80.
  • Configure Nginx so it passes the incoming traffic on port 80 to 8888.
  • Make our Spring Boot application run on port 5000 — that is the default the Nginx is forwarding too.

I will choose the last option for this post to keep the Elastic Beanstalk configuration simple. After modifying the port in the 'application.yml' file like this:

server:
 port: 5000


and rebuilding the application, I can redeploy it with: eb deploy.

Now when I access the URL "http://spring-boot-demo-dev2.eu-central-1.elasticbeanstalk.com/hello," I get the expected result:
Screenshot at Jul 12 16-29-45
That's it for this post. There is a lot more to show about AWS Elastic Beanstalk in combination with a Spring Boot application that I will show in some future posts.

AWS Spring Framework Spring Boot application

Published at DZone with permission of $$anonymous$$. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot: Handle AWS RDS Password Change or Rotation Without Restarting
  • How to Build a Chat App With Spring Boot and DynamoDB
  • Spring2quarkus — Spring Boot to Quarkus Migration
  • A Practical Guide to Creating a Spring Modulith Project

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: