Getting Started with AWS Development using Java



There are many languages supported by AWS for building applications including Java, net and Python. For this blog we are going to focus on Java and will provide the basic guidelines to quickly launch a development environment using your favourite IDE and Build tools.

Amazon recently released new versions AWS Java SDK 2.0 that enables developers to programmatically interact with AWS Services like EC2, S3, RDS, SNS, DynamoDB, SQS and more. 

This article provides a set of guidelines which will enable the reader to quickly get started with AWS development using AWS Java SDK on Windows platform.

Install Java

Download and install the latest version of Java from the official website.

Create AWS IAM User

For AWS development it is recommended to create a new IAM user from the AWS console. Here are the steps:

  1. Login to AWS management console and go to the IAM console.

  2. Click Users in the sidebar.

  3. Click Create New Users.

  4. After creating the user select the newly created IAM user from the list.

  5. Open the Security Credentials tab, and click Create Access Key. A dialog box will appear from where you can download the security keys to your desktop.

Note: Security keys will be downloaded in the form of a CSV file and will contain a Secret key and an Access key generated for your IAM user.

Install Eclipse

Eclipse is the most commonly used IDE for developing Java projects. We recommend Eclipse IDE for this exercise. You can download the latest version from the official website

After the installation is completed, launch the IDE and provide a path to an empty directory to be used as your workspace.

Install and Configure AWS Toolkit for Eclipse

AWS Toolkit for Eclipse is a fully integrated and open source plugin available on Eclipse marketplace. The Toolkit includes the AWS SDK and makes it very easy for users to develop, build, debug, run and test Java projects using Maven.

Here are the installation instruction for AWS Toolkit:

1.   Launch Eclipse, go to Help from the tab menu and select Eclipse Marketplace.

2.   In the pop-up window go to the Search section and type in AWS. See the snapshot

 
Eclipse marketplace aws toolkit.png

3.   Look for AWS Toolkit for Eclipse 2.0 and click install.

4.   Select all features and click Confirm and complete the installation process.

5.   An icon for AWS Toolkit will appear after successful installation of the plugin. Click that icon and select Preferences.

 
Eclipse marketplace aws toolkit preferences.png

6.   Provide the Secret key and Access key that you generated for your IAM user. Save the profile.

 
Eclipse-AWS-Toolkit-Profile.png

Create a HelloWorld application

To ensure that the setup is working fine, create a simple HelloWorld application which will create a new S3 bucket in the specified region.

1.   Launch Eclipse, go to File, select New and then select AWS Java Project
2.   Provide a name for your project and click Finish.
3.   From the Project explorer select and open your maven build file i.e. pom.xml
4.   Copy the below code in the dependency section. The depencies here are enough for the HelloWorld project.

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.319</version>
<scope>compile</scope>
</dependency>

5.   In src/main/java create a new package and a class. Include the public static void main(String[] args) in your class.
6.   Add following code to your main method:

 

public static void main(String[] args) {
String bucket_name = "nameofyourbucket";
String secretKey = "XXXXXXXXXXXXXXX";
String accessKey = "XxXXxXxxxxxxxXXXXXxxXXXXXXXXxXXxx";
String AWSRegion = "eu-west-1";

BasicAWSCredentials basicCredentials = new BasicAWSCredentials(secretKey,accessKey);
AmazonS3 s3 = AmazonS3Client.builder()
.withCredentials(new StaticCredentialsProvider(basicCredentials))
.withRegion(AWSRegion)
.build();

Bucket bucket = null;
try {
bucket = s3.createBucket(bucket_name);
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
}

if (bucket == null)
System.out.println("Error creating bucket!\n");
else
System.out.println("Bucket Created!\n");

}

7.   Add following lines in your class:

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.internal.StaticCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.Bucket;

8.   Run your application and select the class which contains the main method.
9.   To validate the outcome of program please go to your AWS management console and launch the S3 service. Here you should be able to see a new bucket created by the name that you specified in the code.

Conclusion

Now, we have a fully functional development environment ready. This environment can be used to develop applications for a number of use cases including automate launching EC2 instances, creating Auto Scaling groups, Load balancers, RDS instances, backups and many more.


Complete documentation for AWS SDK for Java can be found here

Muhammad Ali

Bluestack IT Solutions