AWS Open Source Blog
Getting started with Spring Boot on AWS: Part 1
This is a guest post from Björn Wilmsmann, Philip Riecks, and Tom Hombergs, authors of the upcoming bookStratospheric: From Zero to Production with Spring Boot and AWS.
Spring Bootis the leading framework for building applications in theJava Virtual Machine (JVM)ecosystem. In a nutshell, open source Spring Boot adds auto-configuration on top of the Spring framework by following convention over configuration. In this two-part tutorial, we’ll demonstrate how easy it is to get started with Spring Boot andAmazon Web Services (AWS)usingSpring Cloud for AWS.
Introduction to Spring Cloud for AWS
At its core, the Spring framework enables Java developers (or those using any other JVM language, such as Kotlin) to write modern enterprise applications with ease by providing features like an infrastructure as code (IoC) container; an event framework; a Model, View, Controller (MVC) framework; common data access; and many more.
Spring Cloud for AWS is a sub-project of Spring supporting rapid development for cloud-native applications. When it comes to deploying applications, integration with the cloud provider is key. As AWS provides a Java SDK for connecting to services, there is some general bootstrapping involved that you need for each project: instantiating the AWS client with the correct region and credentials. Furthermore, you might not always want to work with the “low-level” API of the AWS clients but prefer integration with your tried-and-true development techniques: auto-configuration with convention over configuration.
Spring Cloud for AWS comes into play as an integrator of AWS services. In this tutorial, we’ll develop a demo application that integrates with the core servicesAmazon Simple Storage Service (Amazon S3)andAmazon Simple Queue Service (Amazon SQS).In part 1, we’ll show how to display content of an S3 bucket with Thymeleaf, and inpart 2,we’ll cover subscribing to an SQS queue and externalizing the configuration of our application using the Parameter Store ofAWS Systems Manager.
Prerequisites
As a prerequisite, you should be familiar with Java and have basic experience with Spring Boot. A general understanding of Amazon S3 and Amazon SQS is a plus.
On the AWS infrastructure side, we need the following: an S3 bucket with publicly accessible content and a notification configuration to send events on each file upload to a SQS queue that our application is subscribed to.
You can find the correspondingAWS CloudFormationsetup onGitHub.
Setting up Spring Cloud for AWS
The fullbuild.gradle
can be found onGitHub.
First, we need to configure the access to AWS. TheAWS SDK for Javaalready offers several solutions for this, such as, using environment variables, a property file or loading them from theAmazon Elastic Compute Cloud (Amazon EC2)Instance Metadata Service.
Spring Cloud for AWS lets us configure the credentials the “Spring Boot way.” Therefore, we can store the credentials inside ourapplication.yml
by defining bothcloud.aws.credentials.secret-key
andcloud.aws.credentials.access-key
:
Because we don’t want to store the credentials in plain text inside ourapplication.yml
,we can externalize them and pass them via command-line arguments or environment variable. For this demo, we’ll be using a mixed approach and define the AWS profile inside theapplication.yml
and store the credentials for this profile inside~/.aws/credentials
:
Content of~/.aws/credentials
:
What’s left is to configure the AWS region. Spring Cloud for AWS can automatically detect this based on your environment or stack once you enable the Spring Boot propertycloud.aws.region.auto
.
You can also set the region in a static fashion for your application:
Feature 1: Displaying content of a S3 Bucket with Thymeleaf
For the first feature of our demo application, we want to display the content of a predefined S3 bucket.
Spring Cloud for AWS configures theAmazonS3Client
for us out-of-the-box (using the correct credentials and region). Hence, we can inject it into ourDashboardController
and start using it:
Apart from injecting theAmazonS3Client
,we look up the property value (custom.bucket-name
) of the S3 bucket for which we want to display the content.
Using@PostConstruct
,because we want our Java constructor to be free of any side effects, we fetch the location of our bucket. We need thisbucketLocation
to construct the download links for the files later on.
For our Thymeleaf view, we are using Spring MVC to expose a controller endpoint that serves our view. As we render this view on the server side, we can pass data as part of the model to our view:
We can then access the model inside the view, to render a HTML table with a row for each file in theS3 bucket:
We can now access the Simple S3 File Viewer, after starting our application with./gradlew bootRun
,athttp://localhost:8080/.
In part 1 of this tutorial, we provided a brief introduction to Spring Cloud for AWS and began developing a demo application that integrates with core Amazon services. Inpart 2,we’ll continue our demonstration by incorporating additional features, including subscribing to an SQS queue and externalizing the application configuration.
Feature image via Pixabay.
The content and opinions in this post are those of the third-party author and AWS is not responsible for the content or accuracy of this post.