This is a guest post from Björn Wilmsmann, Philip Riecks, and Tom Hombergs, authors of the upcoming book Stratospheric: From Zero to Production with Spring Boot and AWS.
Spring Boot is the leading framework for building applications in the Java 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 and Amazon Web Services (AWS) using Spring 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 services Amazon Simple Storage Service (Amazon S3) and Amazon Simple Queue Service (Amazon SQS). In part 1, we’ll show how to display content of an S3 bucket with Thymeleaf, and in part 2, we’ll cover subscribing to an SQS queue and externalizing the configuration of our application using the Parameter Store of AWS Systems Manager.
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 corresponding AWS CloudFormation setup on GitHub.
Setting up Spring Cloud for AWS
build.gradle can be found on GitHub.
First, we need to configure the access to AWS. The AWS SDK for Java already offers several solutions for this, such as, using environment variables, a property file or loading them from the Amazon 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 our
application.yml by defining both
Because we don’t want to store the credentials in plain text inside our
application.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 the
application.yml and store the credentials for this profile inside
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 property
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 the
AmazonS3Client for us out-of-the-box (using the correct credentials and region). Hence, we can inject it into our
DashboardController and start using it:
Apart from injecting the
AmazonS3Client, we look up the property value (
custom.bucket-name) of the S3 bucket for which we want to display the content.
@PostConstruct, because we want our Java constructor to be free of any side effects, we fetch the location of our bucket. We need this
bucketLocation 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 the S3 bucket:
We can now access the Simple S3 File Viewer, after starting our application with
./gradlew bootRun, at http://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. In part 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.