Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.
Get started with mocking and improve your application tests using our Mockito guide:
Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.
Get started with understanding multi-threaded applications with our Java Concurrency guide:
Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:
Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
But these can also be overused and fall into some common pitfalls.
To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:
Get started with Spring and Spring Boot, through the Learn Spring course:
>> LEARN SPRINGExplore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:
Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.
I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.
You can explore the course here:
Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.
Get started with Spring Data JPA through the guided reference course:
Refactor Java code safely β and automatically β with OpenRewrite.
Refactoring big codebases by hand is slow, risky, and easy to put off. Thatβs where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.
Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions β one for newcomers and one for experienced users. Youβll see how recipes work, how to apply them across projects, and how to modernize code with confidence.
Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.
1. Introduction
In this quick article, weβre going to build a Spring Cloud application that uses instance profile credentials to connect to an S3 bucket.
2. Provisioning Our Cloud Environment
Instance profiles are an AWS feature that allows EC2 instances to connect to other AWS resources with temporary credentials. These credentials are short-lived and are automatically rotated by AWS.
Users can only request temporary credentials from within EC2 instances. However, we can use these credentials from anywhere until they expire.
To get more help specifically on instance profile configuration, check out AWSβs documentation.
2.1. Deployment
First of all, we need an AWS environment that has the appropriate setup.
For the code sample below, we need to stand up an EC2 instance, an S3 bucket, and the appropriate IAM roles. To do this, we can use the CloudFormation template in the code sample or simply stand these resources up on our own.
2.2. Verification
Next, we should make sure our EC2 instance can retrieve instance profile credentials. Replace <InstanceProfileRoleName> with the actual instance profile role name:
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/<InstanceProfileRoleName>
If everything is setup correctly, then the JSON response will contain AccessKeyId, SecretAccessKey, Token, and Expiration properties.
3. Configuring Spring Cloud
Now, for our sample application. We need to configure Spring Boot to use instance profiles, which we can do in our Spring Boot configuration file:
cloud.aws.credentials.instanceProfile=true
And, thatβs it! If this Spring Boot application is deployed in an EC2 instance, then each client will automatically attempt to use instance profile credentials to connect to AWS resources.
This is because Spring Cloud uses the EC2ContainerCredentialsProviderWrapper from the AWS SDK. This will look for credentials in priority order, automatically ending with instance profile credentials if it canβt find any others in the system.
If we need to specify that Spring Cloud only use instance profiles, then we can instantiate our own AmazonS3 instance.
We can configure it with an InstanceProfileCredentialsProvider and publish it as a bean:
@Bean
public AmazonS3 amazonS3() {
InstanceProfileCredentialsProvider provider
= new InstanceProfileCredentialsProvider(true);
return AmazonS3ClientBuilder.standard()
.withCredentials(provider)
.build();
}
This will replace the default AmazonS3 instance provided by Spring Cloud.
4. Connecting to Our S3 Bucket
Now, we can connect to our S3 bucket using Spring Cloud as normal, but without needing to configure permanent credentials:
@Component
public class SpringCloudS3Service {
// other declarations
@Autowired
AmazonS3 amazonS3;
public void createBucket(String bucketName) {
// log statement
amazonS3.createBucket(bucketName);
}
}
Remember that because instance profiles are only issued to EC2 instances, this code only works when running on an EC2 instance.
Of course, we can repeat the process for any AWS service that our EC2 instance connects to, including EC2, SQS, and SNS.
5. Conclusion
In this tutorial, weβve seen how to use instance profile credentials with Spring Cloud. Also, we created a simple application that connects to an S3 bucket.
