Book Image

Expert AWS Development

By : Atul Mistry
Book Image

Expert AWS Development

By: Atul Mistry

Overview of this book

Expert AWS Development begins with the installation of the AWS SDK and you will go on to get hands-on experience of creating an application using the AWS Management Console and the AWS Command Line Interface (CLI). Then, you will integrate applications with AWS services such as DynamoDB, Amazon Kinesis, AWS Lambda, Amazon SQS, and Amazon SWF. Following this, you will get well versed with CI/CD workflow and work with four major phases in the release process – Source, Build, Test, and Production. Then, you will learn to apply AWS Developer tools to your Continuous Integration (CI) and Continuous Deployment (CD) workflow. Later, you will learn about user authentication using Amazon Cognito, and also how you can evaluate the best architecture as per your infrastructure costs. You will learn about Amazon EC2 service and will deploy an app using it. You will also deploy a practical real-world example of a CI/CD application with the Serverless Application Framework, which is known as AWS Lambda. Finally, you will learn how to build, develop, and deploy the Application using AWS Developer tools such as AWS CodeCommit, AWS CodeBuild, AWS CodeDeploy, and AWS CodePipeline, as per your project requirements.
Table of Contents (16 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Brief introduction to AWS tools and SDKs


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:

AWS SDK for Java

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:

  • Client builders are the only way to create the client services, which means the clients are immutable after creation
  • All Plain Old Java Objects (POJOs) are immutable and must be created from the builder
  • Many region classes such as Region, Regions, and RegionUtils are merged into a single Region class

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:

  1. 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.
  2. 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

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

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.

AWS SDK for Java using Apache Maven

Please perform the following steps to include AWS SDK for Java using Apache Maven.

  1. 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
  1. 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.

Configuring an SDK as a Maven dependency

Please perform the following steps to configure AWS SDK as Maven dependency.

  1. 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 the pom.xml file:
  <dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.11.106</version>
  </dependency>
  1. 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>
  1. After setting up the pom.xml file, you can build your project with the mvn package command. It will generate a Java Archive (JAR) file in the target directory after successful execution:
  1. Now you need to add the following code in the pom.xml file to connect with the AWS SDK. You have to mention your main Java class under the Configuration | mainClass tag. We will create the S3MavenExample.java file in the next step:
<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>
  1. Let's create the S3MavenExample.java file in the com/packt/example package. We are going to create an S3 bucket as per the specific region with a random number generator, prefix it with s3-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;
  1. You have imported UUID to generate the pseudo random number. You are importing Region and Regions to create the bucket in a specific region, and AmazonS3 and AmazonS3Client to access the AWS S3 services:
AmazonS3 s3 = new AmazonS3Client();
Region s3Region = Region.getRegion(Regions.AP_SOUTHEAST_1);
s3.setRegion(s3Region);
  1. 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();
  1. Here you are creating the s3-maven-bucket- prefix with some random UUID:
s3.createBucket(bucketName);
  1. 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);
  1. To delete the bucket, you can use the deleteBucket() method. You have to pass the bucket name as a parameter in this method. After creating the Java file, execute the following command:
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.

AWS SDK for Java using Gradle

Please perform the following steps to include AWS SDK for Java using Gradle:

  1. 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 file
    • gradlew: Gradle startup script for Unix
    • gradlew.bat: Gradle startup script for Windows:
  1. Now execute the following command:
gradlew.bat

After completing this execution, you can see the .gradle folder.

  1. 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'
}
  1. Let's create a S3GradleExample.java file under the com.packt.example folder. This is the same file as S3MavenExample.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);
    }
}
  1. After creating the Java file, execute the following command:
gradlew clean build run

It will create and delete the bucket as per the specified regions:

AWS SDK for Java using Eclipse IDE

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:

  1. Once you click on Install New Software, it will open the Available Software dialog box.
  2. In this dialog box, you have to click on Add to add the AWS toolkit.
  3.  It will open the Add Repository dialog box.
  4.  In this dialog box, add the Name and Location as https://aws.amazon.com/eclipse.
  5.  Click on OK.
  1. On the next page, you will see all available AWS tools. You can select AWS Core Management Tools and other tools as per your project requirements:
  1. A preview page will display to confirm the installation details. Click on Next and you will see the Review License page, where you click Accept and Finish to complete the AWS installation:
  1. After successful installation, your IDE will restart. After restarting, you will see the AWS toolkit icon in the toolbar:
  1. 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 used S3Demo as my Project name, com.packt as my Group ID, and examples as my Artifact ID. I have selected Amazon S3 Sample from the Java samples:
  1. 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, and Secret Access Key under the Profile 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:
  1. 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 select Run 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.

AWS SDK for Node.js

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:

  1. 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 command prompt in Windows.
  2. You need to create a package.json file to mention the required dependency to install AWS SDK and UUID. We need aws-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"
    }
}
  1. Now open the command prompt and execute the following command:
npm install aws-sdk
  1. 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
  2. The file format should be as follows:
[default]
aws_access_key_id = downloaded_access_key_id
aws_secret_access_key = downloaded_secret_access_key
  1. Now, replace your AWS credentials values with the values downloaded_access_key_id and downloaded_secret_access_key.
  2. 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');
  1. First, you have to create variable such as AWS and UUID to load SDK for JavaScript:
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.

  1. 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.