As we discussed in the introduction, AWS provides developer and command-line tools, toolkits, and SDKs to develop and manage AWS applications. Currently, AWS provides nine SDKs for different programming languages, six SDKs for IoT devices, and five SDKs for mobile devices. Let's take a brief look at this:
- Developer tools: Developer tools are used to store source code securely and version-control it. They also help with build automation and testing and deploying applications to AWS or on-premise. They include the AWS CodeCommit, AWS CodePipeline, AWS CodeBuild, and AWS CodeDeploy services. We will cover these in Chapter 4, CI/CD in AWS Part 1 – CodeCommit, CodeBuild, and Testing and Chapter 5, CI/CD in AWS Part 2 – CodeDeploy, CodePipeline, and CodeStar.
- SDKs: They provide APIs for programming languages, IoT, and mobile devices.
- IDE toolkits: Cloud tools which can integrate to your integrated development environment to speed up your AWS development.
- Command line: This is used to control AWS services from the command line and create scripts for automated service management.
- Serverless development: Serverless applications built on AWS Lambda can test and deploy using AWS Serverless Application Model (SAM) and SAM Local. We will cover Amazon Lambda in Chapter 10, Amazon Lambda – AWS Serverless Architecture.
AWS provides SDKs for the different languages and hardware devices to connect AWS IoT and mobile devices.
The following are the different kinds of SDK. In this chapter, we will cover two programming language SDKs, Java and Node.js:
Let's start with the Java SDK. This SDK helps to minimize the complexity and provision to coding using Java APIs for AWS Services such as Amazon EC2, Amazon DynamoDB, Amazon S3, and many more. You can download a single package from the AWS website which includes the AWS Java library and code samples with documentation.
Currently, you can download the AWS SDK for Java v1.11.x code base and AWS has recently launched an AWS SDK for Java v2.0, which is major code change. This version is built on Java 8. It has added features such as non-blocking I/O and a pluggable API layer (by default, it will use Apache but you can change this as per your project needs). In this version, you can see some API changes:
Note
This AWS SDK for Java v2.0 is a developer preview version and not recommended for production use.
Let's explore how to install, set up, and use AWS SDK for Java.
You need to set up AWS SDK for Java on your machine to use in your project. Please perform the following steps to set up the environment and run the sample code in Java using the AWS SDK:
- AWS account setup and IAM user creation: You have to set up an AWS account and credentials to use AWS SDK. To increase the level of security for your AWS account, it is always preferable to create an IAM user. Use the created IAM user instead of the root user. Once you create an IAM user, you have to create an access key. You can download or view the access key ID and secret access key in the resulting dialog box. It's always best practice to download and store them in your local environment.
- AWS credentials and region setup: For your local application development, you need to set up credentials and regions.
The AWS credentials profile file is located in your filesystem. It should be at the following path:
- For Linux, macOS, or Unix:
~/.aws/credentials
- For Windows:
C:\Users\USERNAME\.aws\credentials
- For Linux, macOS, or Unix:
The file format should be as follows:
[default] aws_access_key_id = downloaded_access_key_id aws_secret_access_key = downloaded_secret_access_key
Another alternative is to set up AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
environment variables. Now, replace your AWS access key ID and secret access key for the values of downloaded_access_key_id
and downloaded_secret_access_key
.
The AWS region configuration file is located in your filesystem. It should be at the following path:
- For Linux, macOS, or Unix:
~/.aws/config
- For Windows:
C:\Users\USERNAME\.aws\config
- For Linux, macOS, or Unix:
This file format should be as follows:
[default] region = your_region_name
Now, replace your AWS region for the value or region. Another alternative is you can set up AWS_REGION
as an environment variable.
- Java Development Environment: JDK 6.0 or later versions are required for the AWS SDK. The latest JDK versions are available for download from the Oracle website. J2SE 6.0 does not support SHA 256-signed SSL certificates, which are required for all HTTP connections with AWS after September 2015. You can use J2SE7.0 or newer versions, which are not affected by the certificate issue.
You can use different methods to include the AWS SDK for your Java project. We will explore all methods in this chapter:
- Apache Maven: You can use specific SDK components or the full SDK with the help of Apache Maven.
- Gradle: Maven Bill of Materials (BOM) in a Gradle project can be used to automatically manage the dependency for your project.
- Eclipse IDE: The AWS toolkit can be integrated into an existing Eclipse IDE. It will automatically download, install, and update the Java SDK with a few settings.
Please perform the following steps to include AWS SDK for Java using Apache Maven.
- Assuming that you have already installed Maven in your machine, create a new folder called
AWS SDK Example
or any name. Go to this folder and execute the following command to set up the environment:
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart
- After it has successfully executed, you will see the following folder structure under the
AWS SDK Example
folder:
You will see the pom.xml
file generated under the ..\AWS SDK Example\java-maven-demo
folder.
Please perform the following steps to configure AWS SDK as Maven dependency.
- To add AWS SDK for Java in your project, you need to add the dependency to the
pom.xml
file. From SDK version 1.9.*, you can import single or individual components. If you want to add the entire SDK as a dependency, add the following code in the<dependency>
tag in thepom.xml
file:
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk</artifactId> <version>1.11.106</version> </dependency>
- If you are using SDK Version 1.9.* or above, you can import many individual components, such as EC2, S3, CodeCommit, or CodeDeploy:
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3</artifactId> </dependency>
<build> <resources> <resource> <directory>${env.HOME}/.aws/</directory> </resource> </resources> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>com.packt.example.S3MavenExample</mainClass> </configuration> </plugin> </plugins> </build>
- Let's create the
S3MavenExample.java
file in thecom/packt/example
package. We are going to create an S3 bucket as per the specific region with a random number generator, prefix it withs3-maven-bucket-
, and then delete the bucket:
import java.util.UUID; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client;
- You have imported UUID to generate the pseudo random number. You are importing
Region
andRegions
to create the bucket in a specific region, andAmazonS3
andAmazonS3Client
to access the AWS S3 services:
AmazonS3 s3 = new AmazonS3Client(); Region s3Region = Region.getRegion(Regions.AP_SOUTHEAST_1); s3.setRegion(s3Region);
- Here you are creating an S3 client and specifying the specific region where it will create the bucket:
Note
If you do not specify any region, it will create it at US East (N. Virginia)
. This means the default region is US East (N. Virginia)
.
String bucketName = "s3-maven-bucket-" + UUID.randomUUID();
- Here you are creating the
s3-maven-bucket-
prefix with some random UUID:
s3.createBucket(bucketName);
- To create the bucket, you can use
createBucket()
method. You have to pass the bucket name as a parameter in this method:
s3.deleteBucket(bucketName);
mvn clean compile exec:java
It will create and delete the bucket as per the specified regions:
If you have completed this step and you can see the creation and deletion of the bucket, it means you have successfully completed AWS SDK for Java using Maven in your project.
Please perform the following steps to include AWS SDK for Java using Gradle:
- Assuming that you have already installed Gradle in your machine, create a new folder called
java-gradle-demo
or any other name. Go to this folder and copy the following files:- The
gradle
folder: Contains necessary files for the wrapper build.gradle
: Gradle build filegradlew
: Gradle startup script for Unixgradlew.bat
: Gradle startup script for Windows:
- The
- Now execute the following command:
gradlew.bat
After completing this execution, you can see the .gradle
folder.
- Now you need to update your
build.gradlew
file to connect with AWS:
apply plugin: 'java' apply plugin: 'application' mainClassName="com.packt.example.S3GradleExample" repositories { mavenCentral() } dependencies { compile 'com.amazonaws:aws-java-sdk:1.9.6' }
- Let's create a
S3GradleExample.java
file under thecom.packt.example
folder. This is the same file asS3MavenExample.java
:
package com.packt.example; import java.util.UUID; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; public class S3GradleExample { public static void main(String[] args) { AmazonS3 s3 = new AmazonS3Client(); Region s3Region = Region.getRegion(Regions.AP_SOUTHEAST_1); s3.setRegion(s3Region); String bucketName = "s3-gradle-bucket-" + UUID.randomUUID(); System.out.println("Amazon S3 will create/delete bucket"); // Create a new bucket System.out.println("Creating bucket " + bucketName + "\n"); s3.createBucket(bucketName); // Delete a bucket. System.out.println("Deleting bucket " + bucketName + "\n"); s3.deleteBucket(bucketName); } }
gradlew clean build run
It will create and delete the bucket as per the specified regions:
I am assuming that you have already installed Eclipse 4.4 (Luna) or a higher version on your machine as the AWS toolkit supports that.
There are two ways to install an AWS toolkit in your IDE:
- Click on
Help
|Install New Software…
and install - Click on
Help
|Eclipse Marketplace
, search forAWS
, and install
We will install using the first method. Now please perform the following steps and refer to the following screenshot:
- Once you click on
Install New Software
, it will open theAvailable Software
dialog box. - In this dialog box, you have to click on
Add
to add the AWS toolkit. - It will open the
Add Repository
dialog box. - In this dialog box, add the
Name
andLocation
ashttps://aws.amazon.com/eclipse
. - Click on
OK
.
- After successful installation, your IDE will restart. After restarting, you will see the AWS toolkit icon in the toolbar:
- Now let's create a sample AWS Java project. When you click
New AWS Java Project….
, you will see the following screen. You need to add the necessary details for the project. Here I have usedS3Demo
as myProject name
,com.packt
as myGroup ID
, andexamples
as myArtifact ID
. I have selectedAmazon S3 Sample
from the Java samples:
- If you want to add new AWS accounts, click on the
Configure AWS accounts…
link. You can add the credentials in two ways:- Add
Profile Name
,Access Key ID
, andSecret Access Key
under theProfile Details
screen. - You can specify your credentials file path or browse to your credentials file. Once you have added that, you can select
Apply and Close
:
- Add
- Now it will generate the projects and create the necessary files. You can see the following screen with generated files. It will generate a
S3Sample.java
file. You can right-click on this file and selectRun As
|Java Application
. It will create the bucket, list the bucket, upload a new object to S3, download an object, list an object, delete an object, and delete the bucket:
So far, you have learned how to add the AWS Java toolkit into your project using Maven, Gradle, and Eclipse IDE. Now we will see how to add the AWS SDK for Node.js into your project.
Node.js is a free, open source, cross-platform framework. It is used to execute JavaScript code on the server side. In Node.js, you can use AWS SDK for JavaScript. This SDK will help to remove the complexity of coding by providing JavaScript objects to use the AWS services. A single downloaded package includes the AWS JavaScript library as well documentation.
You can install AWS SDK for Node.js in two ways:
- From GitHub: You can get the source code from https://github.com/aws/aws-sdk-js
- From Node.js Package Manager (npm): You can install AWS SDK from the Node.js package manager
Let's install the AWS SDK package and create a sample application to create and delete a bucket on S3 using the following steps:
- You can download (https://nodejs.org/en/download/) and install Node.js If you haven't already installed it. Once you have installed Node.js, you can open the Node.js command prompt from (
Run
|Node.js
c
ommand prompt
in Windows. - You need to create a
package.json
file to mention the required dependency to install AWS SDK and UUID. We needaws-sdk
to install the required Node modules for AWS services and a UUID to create a pseudo random number:
package.json { "dependencies": { "aws-sdk": ">= 2.0.9", "uuid": ">= 1.4.1" } }
- Now open the command prompt and execute the following command:
npm install aws-sdk
- It will create a
node_modules
folder and install AWS SDK for Node.js. Now you need to set the credentials in the AWS credentials profile file on your local system, located at the following:- For Linux, macOS, or Unix:
~/.aws/credentials
- For Windows:
C:\Users\USERNAME\.aws\credentials
- For Linux, macOS, or Unix:
- The file format should be as follows:
[default] aws_access_key_id = downloaded_access_key_id aws_secret_access_key = downloaded_secret_access_key
- Now, replace your AWS credentials values with the values
downloaded_access_key_id
anddownloaded_secret_access_key
. - Now let's create a
S3Example.js
file which will connect to AWS and create and delete the bucket on S3:
var AWS = require('aws-sdk'); var uuid = require('uuid');
var s3 = new AWS.S3(); var bucketName = 'node-sdk-sample-' + uuid.v4(); var params={Bucket: bucketName}
Here you are creating s3 as an S3 service object, bucketname
with node-sdk-sample
as the prefix with a random number, and params
as the parameters to call the bucket:
s3.createBucket(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log("Successfully Created Bucket: "+bucketName); // successful response });
The preceding method is used to create the bucket with parameters and callback functions:
s3.waitFor('bucketExists', params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else { s3.deleteBucket(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log("Successfully Deleted Bucket:"+bucketName); // successful response }); } });
Here it will check whether the bucket is exists or not. If it exists then it will delete the bucket. If it is not exist than it is trying to delete the bucket which is not created yet. In that case, you will get an error.
- You can execute the file with
node S3Example.js
and you can see that it will create and delete the bucket:
Previously, we discussed AWS SDKs for different programming languages and we have covered Java and Node.js with setup and examples. Now we will see how you can set up SDKs on IoT devices.