Creating service clients - AWS SDK for Java
AWS services or capabilities described in AWS documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with AWS services in China.

At Amazon Web Services (AWS), we’re focused on finding ways to improve our products and provide a better customer experience. To do that, we need your feedback. Please take 5 minutes of your time to share insights regarding your experience with Java Spring and your need for Spring integration with AWS.

Click here to take a quick survey

This survey is hosted by an external company (Qualtrics), so the link above does not lead to our website. Please note that AWS will own the data gathered via this survey, and will not share the information/results collected with survey respondents. AWS handles your information as described in the AWS Privacy Notice.

Creating service clients

To make requests to Amazon Web Services, you first create a service client object. In version 2.x of the SDK, you can create clients only by using service client builders.

Each AWS service has a service interface with methods for each action in the service API. For example, the service interface for Amazon DynamoDB is named DynamoDbClient. Each service interface has a static factory builder method you can use to construct an implementation of the service interface.

Obtaining a client builder

To obtain an instance of the client, use the static factory method builder. Then customize it by using the setters in the builder, as shown in the following example.

In the AWS SDK for Java 2.0, the setters are named without the with prefix.

DynamoDbClient client = DynamoDbClient.builder() .region(Region.US_WEST_2) .credentialsProvider(ProfileCredentialsProvider.builder() .profileName("myProfile") .build()) .build();

The fluent setter methods return the builder object, so that you can chain the method calls for convenience and for more readable code. After you configure the properties you want, you can call the build method to create the client. Once a client is created, it’s immutable. The only way to create a client with different settings is to build a new client.

Using the default client

The client builders have another factory method named create. This method creates a service client with the default configuration. It uses the default provider chain to load credentials and the AWS Region. If credentials or the region can’t be determined from the environment that the application is running in, the call to create fails. See Using credentials and java-dg-region-selection for more information about how credentials and region are determined.

To create a default client

DynamoDbClient client = DynamoDbClient.create();

Client lifecycle

Service clients in the SDK are thread-safe. For best performance, treat them as long-lived objects. Each client has its own connection pool resource that is released when the client is garbage collected. The clients in the AWS SDK for Java 2.0 now extend the AutoClosable interface. For best practices, explicitly close a client by calling the close method.

To close a client

DynamoDbClient client = DynamoDbClient.create(); client.close();