AWS News Blog

New Resources APIs for the AWS SDK for Java

We are launching a preview of a new, resource-style API model for the AWS SDK for Java. I will summarize the preview here, and refer you to the AWS Java Blog for full information!

The new resource-oriented APIs are designed to be easier to understand and simpler to use. It obviates much of the request-response verbosity present in the existing model and presents a view of AWS that is decidedly object-oriented. Instead of exposing all of the methods of the service as part of a single class, the resource-style API includes multiple classes, each of which represents a particular type of resource for the service. Each class includes the methods needed to interact with the resource and with related resources of other types. Code written to the new API will generally be shorter, cleaner, and easier to comprehend.

Here is the old-school way to retrieve an AWS Identity and Access Management (IAM) group using the GetGroup function:


AmazonIdentityManagement iam = new AmazonIdentityManagementClient();
iam.setRegion(Region.getRegion(Regions.US_WEST_2));

GetGroupRequest getGroupRequest = new GetGroupRequest("NeedNewKeys");
GetGroupResult getGroupResult = iam.getGroup(getGroupRequest);

And here is the new way:


IdentityManagement iam = ServiceBuilder.forService(IdentityManagement.class)
  .withRegion(Region.getRegion(Regions.US_WEST_2))
  .build();

Group needNewKeys = iam.getGroup("NeedNewKeys");

The difference between the old and the new APIs becomes even more pronounced when more complex operations are used. Compare the old-school code for marking an outdated access key (oldKey) for an IAM user as inactive:


UpdateAccessKeyRequest updateAccessKeyRequest = new UpdateAccessKeyRequest()
  .withAccessKeyId(oldKey)
  .withUserName(user.getUserName())
  .withStatus(StatusType.Inactive);
iam.updateAccessKey(updateAccessKeyRequest);

With the new, streamlined code, the intent is a lot more obvious. There’s a lot less in the way of setup code and the method is invoked on the object of interest instead of on the service:


oldKey.deactivate();

The new API is being launched in preview mode with support for Amazon Elastic Compute Cloud (EC2), AWS Identity and Access Management (IAM), and Amazon Glacier. We plan to introduce resource APIs for other services and other AWS SDKs in the future.

Jeff;

PS – To learn more about Resource APIs, read the full post on the AWS Java Development Blog .