Saturday, October 1, 2022

Deploying a Java web application to Amazon ECS

 You can deploy a Java web application that uses the AWS SDK for Java (v2) to Amazon Elastic Container Service (Amazon ECS). Amazon ECS is a highly scalable and fast container management service. You can use it to run, stop, and manage containers on a cluster. With Amazon ECS, your containers are defined in a task definition that you use to run an individual task or task within a service.

This AWS tutorial walks you through deploying a Java web application to an AWS Fargate container. With AWS Fargate, you don't need to manage servers, handle capacity planning, or isolate container workloads for security. Fargate handles the infrastructure management aspects of your workload for you. For more information, see What is Amazon Elastic Container Service.

To deploy a Java web application to AWS Fargate, create a Docker image from your Spring Boot project. Then, you can deploy your Docker image to Docker Hub, which is a hosted repository service. Finally, configure AWS Fargate to import the Docker image located on Docker Hub into a container, as shown in the following illustration.


After you complete this tutorial, you will have a Java web application running in a container.



Topics

  • Prerequisites
  • Deploy your Java web application to Docker Hub
  • Deploy the Docker image to Amazon ECS
  • Run the application

Prerequisites

To complete the tutorial, you need the following:
  • An AWS account.
  • A Java IDE. (This example uses IntelliJ.)
  • Java 1.8 SDK and Maven.
  • Docker Desktop. (For information about installing, see Docker Desktop overview.)

If you are using a Windows operating system, install Docker Desktop for Windows. For more information, see Install Docker Desktop on Windows. We recommend that you verify that Docker is installed on your system by running the following command.

Docker --version

If this command is successful, the version is displayed.



Important

  • The AWS services used in this document are included in the AWS Free Tier.
  • This code has not been tested in all AWS Regions. Some AWS services are available only in specific Regions. For more information, see AWS Regional Services.
  • Running this code might result in charges to your AWS account.
  • Be sure to delete all of the resources that you create while going through this tutorial so that you won't be charged.

Creating the resources

To follow along with this tutorial, complete Creating your first AWS Java web application. This is the Java web application that is deployed to a container.

Modify the Java code in the project to use a StaticCredentialsProvider. This way, your credentials can be used within the container. To use this provider, create a service client object using the following Java code.

  AwsBasicCredentials credentials = AwsBasicCredentials.create("<Key value>", "<Secret Key value>");
  StaticCredentialsProvider staticCredentials = StaticCredentialsProvider.create(credentials);
  Region region = Region.US_EAST_1;
  DynamoDbClient ddb = DynamoDbClient.builder()
            .region(region)
            .credentialsProvider(staticCredentials)
            .build();


Deploy your Java web application to Docker Hub

First, create a Docker image from your Spring Boot application.

Note: As stated at the start of this document, make sure to complete the steps in Creating your first AWS Java web application. All of the Java logic can be found in that document.

Add the Docker file to your project

Add the Docker file to your project. This file is used to assemble a Docker image. The following illustration shows the location of this file.


The following code shows the content of this file.

FROM openjdk:8-jdk-alpine
EXPOSE 8080
ARG JAR_FILE=target/my-application.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Build your Docker image

You can build a Docker image from your Spring Boot project by using the following command.

mvn spring-boot:build-image

Note: Make sure that your project name in the POM.xml file is lowercase. Otherwise, an error occurs.

Verify that the Docker image was created by using the following command.

docker image ls

All the Docker images are displayed, as shown in the following illustration.



Next, execute the run command and specify 8080 as the port.

docker run --tty --publish 8080:8080 firstappdocker:1.0

Run the Docker tag command.

 docker tag firstappdocker:1.0 scmacdon/firstappdocker:1.0

In this example, scmacdon refers to the repo name in Docker Hub. Change this value to reflect your repository name.

Verify that the Docker image with a tag was created by using the following command.

docker image ls



Deploy the Docker image to Docker Hub

You can push the Docker image to Docker Hub by using the following command. You must use the tag value and the Docker image name.

docker push scmacdon/firstappdocker:1.0

After deployment, you will see the Docker image in Docker Hub at:

https://hub.docker.com/

The following illustration shows the Docker image deployed to Docker Hub.



After the Docker image is located on Docker Hub, you can import it into Amazon ECS.

Deploy the Docker image to Amazon ECS

You can deploy the Docker image located on Docker Hub to Amazon ECS. After you complete this task, you can run the application.

To deploy the Docker image to Amazon ECS, perform these steps.

1, Sign in to Amazon ECS at the following URL:

https://us-west-2.console.aws.amazon.com/ecs/home?region=us-west-2#/clusters

2. Choose Task Definitions.

3. Choose Create new Task Definition.



4. Choose Fargate.

5. Choose Next step.

6. In the Task definition name field, enter a name. For example, springboot-docker.

7. For Task role, select None.



8. For Task memory (GB), specify 1 GB.

9. For Task CPU (vCPU), specify 0.5.

10. Choose Add container.

11. Specify a container name. For example, first-java-container.

12. For Image, specify a URL of docker.io/[Repo Name]/[Image Name]:[Tag]. For example, docker.io/scmacdon/firstappdocker:1.0.

13. For Memory limits, specify a Hard limit of 1024.



14. Specify two Port Mappings values, 8080 and 80.

15. Choose Add.

16. Choose Create. A message informs you that the task definition was created.





17. Choose View Definition.

18. Choose Clusters, Create Cluster.

19. Choose Networking Only.

20. Click Next step.

21. Specify a cluster name. For example, my-aws-cluster.


22. Choose Create. You will see a confirmation message.


23. Choose View Cluster.

24. Choose Tasks.

25. Choose Run New Task.


26. Choose Fargate as Launch Type.

27. Choose Task Definition (select the one that you created).


28. Select Cluster VPC and Subnet values.

29. Configure a security group for this cluster. For information about setting up security group inbound rules, see Controlling Access with Security Groups.

30. Choose Run Task. You will see a message that shows it's running. (You might have to wait a few seconds and then refresh.)


31. Choose the link under Task. Then, scroll under Network. You see the public IP address that you can use to access the application. Remember to specify the port number, also.



Saturday, January 8, 2022

Explore different ways to code AWS database solutions with different AWS SDKs

 When working with AWS database solutions, there are different options and different SDKs available. This post contains a collection of different technologies and different SDKs. The example app that shows usage of AWS SDKs is a decoupled React app.  

Item Tracker

The following illustration shows the item tracker application. 







1. Build the Item tracker with AWS RDS MySQL

You can build this app by using the Java Database Connectivity (JDBC) API that provides data access from the Java programming language to an AWS RDS MySQL instance. 


2. Build the Item tracker app with the Amazon DynamoDB Java API

You can build this app by using the Amazon DynamoDB Java V2 API and the Enhanced Client. 



3. Build the Serverless Amazon Aurora item tracker application using the Java RdsDataClient API

You can build this app by using the RdsDataClient object to perform CRUD operations on the Serverless Amazon Aurora database.



4. Create a React and Spring REST application that queries Amazon Redshift data

Discusses how to develop a Spring REST API that queries Amazon Redshift data. The Spring REST API uses the AWS SDK for Java (v2) to invoke AWS services and is used by a React application that displays the data..









Friday, December 3, 2021

How to play an Audio file located in an Amazon S3 bucket in your web page

 You can play an Audio file (MP3) that is located in your Amazon S3 bucket in your web page. 



You can follow these steps:

1. Install the JQuery plug-in by following the instructions in the jPlayer documentation.

2. Add the plug-in to your page, along with the required the CSS.

<!DOCTYPE HTML>

<html xmlns:th="https://www.thymeleaf.org">

<head>

    <meta charset="utf-8" />

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

    <meta name="viewport" content="width=device-width, initial-scale=1" />



    <link rel="stylesheet" th:href="|https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css|"/>

    <link rel="stylesheet" th:href="|https://cdnjs.cloudflare.com/ajax/libs/jplayer/2.9.2/skin/pink.flag/css/jplayer.pink.flag.css|"/>

    <script th:src="|https://code.jquery.com/jquery-1.12.4.min.js|"></script>

    <script th:src="|https://cdnjs.cloudflare.com/ajax/libs/jplayer/2.9.2/jplayer/jquery.jplayer.min.js|"></script>

    <script src="../public/js/items.js" th:src="@{/js/items.js}"></script>

    <link rel="stylesheet" href="../public/css/styles.css" th:href="@{/css/styles.css}" />

    <link rel="icon" href="../public/img/favicon.ico" th:href="@{/img/favicon.ico}" />


    <title>Audio Stream Sample App</title>

</head>


<body>

<div class="container">


    <h2>Audio Stream Sample App</h2>


    <p>This sample application plays Amazon S3 audio content.<p>


    <div id="jquery_jplayer_1" class="jp-jplayer"></div>

    <div id="jp_container_1" class="jp-audio" role="application" aria-label="media player">

        <div class="jp-type-single">

            <div class="jp-gui jp-interface">

                <div class="jp-volume-controls">

                    <button class="jp-mute" role="button" tabindex="0">mute</button>

                    <button class="jp-volume-max" role="button" tabindex="0">max volume</button>

                    <div class="jp-volume-bar">

                        <div class="jp-volume-bar-value"></div>

                    </div>

                </div>

                <div class="jp-controls-holder">

                    <div class="jp-controls">

                        <button class="jp-play" role="button" tabindex="0">play</button>

                        <button class="jp-stop" role="button" tabindex="0">stop</button>

                    </div>

                    <div class="jp-progress">

                        <div class="jp-seek-bar">

                            <div class="jp-play-bar"></div>

                        </div>

                    </div>

                    <div class="jp-current-time" role="timer" aria-label="time">&nbsp;</div>

                    <div class="jp-duration" role="timer" aria-label="duration">&nbsp;</div>

                    <div class="jp-toggles">

                        <button class="jp-repeat" role="button" tabindex="0">repeat</button>

                    </div>

                </div>

            </div>

            <div class="jp-details">

                <div class="jp-title" aria-label="title">&nbsp;</div>

            </div>

            <div class="jp-no-solution">

                <span>Update Required</span>

                To play the media you will need to either update your browser to a recent version or update your <a href="https://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>.

            </div>

        </div>

    </div>

    </div>

  </body>

</html>



Here is the JS file: 

$(document).ready(function(){

    $("#jquery_jplayer_1").jPlayer({

        ready: function () {

            $(this).jPlayer("setMedia", {

                title: "Bubble",

                mp3: "https://<BucketName>.s3.amazonaws.com/<MP3 File Name>"

            });

        },

        cssSelectorAncestor: "#jp_container_1",

        swfPath: "/js",

        supplied: "mp3",

        useStateClassSkin: true,

        autoBlur: false,

        smoothPlayBar: true,

        keyEnabled: true,

        remainingDuration: true,

        toggleDuration: true

    });

});

3. Place your MP3 file into a bucket and make the MP3 file public. Copy the URL here:




4. Add to the URL to this line of code in the JS file:


$(this).jPlayer("setMedia", {

        title: "Bubble",

        mp3: "https://<BUCKETNAME>.s3.amazonaws.com/<MP3 FILE NAME>"

   });

Now your app will play an MP3 file located in an Amazon S3 bucket. Enjoy!!!!


Thursday, December 2, 2021

Creating your first Native Android application using the AWS SDK for Kotlin

 You can develop a native Android application that lets application users submit mobile data to an Amazon DynamoDB table by using the AWS SDK for Kotlin. In addition to using Amazon DynamoDB Kotlin API, this Android application also uses the Amazon Simple Notification Service (Amazon SNS) Kotlin API. For example, this Android application uses the aws.sdk.kotlin.services.dynamodb.DynamoDbClient to store data within a DynamoDB table. After the DynamoDB table is updated, the application uses the Kotlin Amazon SNS API to send a text message to notify a user. This application also uses the Android API to build the application logic. For more information, see Android API reference.

The Amazon DynamoDB Kotlin API lets you perform the following tasks from within an Android project:

  • Access your tables
  • Perform various create, read, update, and delete (CRUD) operations
  • Execute queries

The following shows the application you'll create.


Click https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/usecases/first_android_app

Tuesday, November 9, 2021

Create a Lambda Function with a custom JAR

 

To add the local jar to local maven,  you  can use this command:

mvn install:install-file -Dfile="C:/Users/NROLL97/Main.jar" -DgroupId=keydoxWeb -DartifactId=keydoxWebCodec -Dversion=1.0 -Dpackaging=jar

 

Then added it as a dependency like this:

 <dependency>

    <groupId>keydoxWeb</groupId>

    <artifactId>keydoxWebCodec</artifactId>

    <version>1.0</version>

  </dependency>

And then it worked!

Monday, May 17, 2021

Creating a Publish/Subscription Spring Boot Application

 You can create a web application that has subscription and publish functionality by using the Amazon Simple Notification Service (Amazon SNS). The application created in this AWS tutorial is a Spring Boot web application that lets a user subscribe to an Amazon SNS topic by entering a valid email address. A user can enter many emails and all of them are subscribed to the given SNS topic (once the email recipients confirm the subscription). The user can publish a message that results in all subscribed emails receiving the message.

Note: Amazon SNS is a managed service that provides message delivery from publishers to subscribers (also known as producers and consumers). For more information, see What is Amazon SNS?

To subscribe to an Amazon SNS topic, the user enters a valid email address into the web application.


The specified email address recieves an email message that lets the recipient confirm the subscription.



Once the email recipient accepts the confirmation, that email is subscribed to the specific SNS topic and receives published messages. To publish a message, a user enters the message into the web application and then chooses the Publish button.




I work on AWS Java code that helps AWS developers. Included in this role is working with AWS Services such as S3, DynamoDB, EC2, Lambda functions, and so on. 

https://www.linkedin.com/in/scottmacdonald2010/




Tuesday, May 4, 2021

Creating a Job Posting Site using Amazon Redshift and Amazon Translate Services

 You can create a web application that stores and queries data by using the Amazon Redshift service and the Amazon Redshift Java API V2. To interact with an Amazon Redshift table, you can use a software.amazon.awssdk.services.redshiftdata.RedshiftDataClient. The application created in this AWS tutorial is a job posting web application that lets an employer, an administrator, or human resources staff alert employees or the public about a job opening within a company.

Note: Amazon Redshift is a fully managed, petabyte-scale data warehouse service in the cloud. You can start with just a few hundred gigabytes of data and scale to a petabyte or more. This enables you to use your data to acquire new insights for your business and customers. For more information, see What is Amazon Redshift?.

The data is stored in a Redshift table named blog, as shown in this illustration.


The application you create uses Spring Boot APIs to build a model, different views, and a controller. This web application also lets a user submit a new job posting that is then stored into the blog table, as shown in this illustration.



This example application lets you view the posts by choosing the Get Posts menu item and choosing one of the available buttons. For example, you can view five recent posts by choosing the Five Posts button, as shown in the following illustration.




This application also supports viewing the result set in different languages. For example, if a user wants to view the result set in Spanish, they can choose Spanish from the dropdown field and the data is translated to the given language by using Amazon Translate, as shown in this illustration.




To read this AWS tutorial, click https://github.com/scmacdon/aws-doc-sdk-examples/tree/master/javav2/usecases/creating_redshift_application.


I work on AWS Java code that helps AWS developers. Included in this role is working with AWS Services such as S3, DynamoDB, EC2, Lambda functions, and so on. 

https://www.linkedin.com/in/scottmacdonald2010/


Deploying a Java web application to Amazon ECS

 You can deploy a Java web application that uses the AWS SDK for Java (v2) to Amazon Elastic Container Service (Amazon ECS). Amazon ECS is a...