AWS SDK for Java
Developer Guide

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 DefaultClient

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 Working with AWS Credentials and AWS 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();