Skip to content

Latest commit

 

History

History

native-aws-fargate

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

GraalVM on AWS Fargate Demo

This demo will walk you through the process of containerizing a Native Image application and then launching the image on the AWS platform by using the Amazon Elastic Container Registry and AWS Fargate. In this demo, you will deploy a simple "Hello World" HTTP application and have the ability to see details about its performance.

Prerequisites

Ensure that you have the following installed and follow the linked instructions for any that you are missing:

COMPATIBILITY: Please note that this demo must be performed on an x86-based platform in order to properly function. Working through this demo on an ARM-based platform will result in the generation of a native executable that is not compatible with the platform.

Download or clone the GraalVM demos repository:

git clone https://github.com/graalvm/graalvm-demos

Micronaut "Hello World" Application

The code provided in this demo is a simple "Hello World" REST application created using the Micronaut ® framework. To understand what the code is doing, take a look at the Application.java and HelloController.java files:

Application.java

package example.micronaut;

import io.micronaut.runtime.Micronaut;

public class Application {

    public static void main(String[] args) {
        Micronaut.run(Application.class, args);
    }
}

This is the location of the main() function and entry point for the application.

HelloController.java

package example.micronaut;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;

@Controller("/hello") 
public class HelloController {
    @Get 
    @Produces(MediaType.TEXT_PLAIN) 
    public String index() {
        return "Hello World"; 
    }
}

This code implements the actual RESTful "Hello World" functionality. It produces the "Hello World" string when a GET request is made to the /hello URL.

Deploy a Native Image Container on Amazon ECR

  1. Navigate to the directory for this demo:
cd graalvm-demos/native-aws-fargate
  1. Create a new ECR repository to store the image:
aws ecr create-repository --repository-name native-fargate-repo

Create Repo

  1. A successful repository creation will return an output similar to above; note the "repositoryUri" that is outputted as this will be the location you will use to store your image
  2. Login to the Amazon Web Service dashboard
  3. In the “Services” drop-down menu on the top-left of the page, select “All services” and navigate to the “Elastic Container Registry” page
  4. Locate the newly created repository under Private (ensure that the correct region is selected on the top right of your screen) and click on the box beside the repository

Repositories

  1. Click View push commands and copy the command under step 1

Push command

  1. Authenticate the Uri for the repository with your credentials by executing the copied command in your CLI

OPTIONAL: In the next step you will use a single command to build the application into a container image and deploy it to the repository you have created; if you would like to first view the Docker file that will be used to create the image, run the following command:

./mvnw mn:dockerfile -Dpackaging=docker-native

The newly created Dockerfile will be automatically stored in the "target" directory

  1. Use the Uri once again to push the image to the Amazon ECR:
./mvnw deploy -Dpackaging=docker-native -Djib.to.image=REPOSITORYURI

Deploy the Service on AWS Fargate

  1. Login to the Amazon Web Service dashboard
  2. In the “Services” drop-down menu on the top-left of the page, select “All services” and navigate to the “Elastic Container Service” page
  3. Click on "Clusters" on the left-side pane
  4. Create a new cluster using the button on the top-right of the page

Clusters

  1. Choose a name for the cluster and leave the remaining settings as their default
    • If you would like to be able to view insights about the container image performance, activate Container Insights under "Monitoring"

New cluster

Infrastructure

  1. Once you have created the new cluster, navigate to "Task definitions" by selecting it on the left-side pane
  2. Create a new task definition by clicking the button on the top-right corner

Task definitions

  1. Choose names for the Task Definition Family and Container

Create task definition

  1. Paste the REPOSITORYURI used in the prior section into the "Image URI" text box
    • To find the URI again click the "Amazon ECR" link on the left-side pane and select "Repositories". Your repository will appear along with the corresponding URI
  2. Leave the remaining options as their default and create the new Task Definition
  3. Return to the list of Clusters and select the Cluster that you created
  4. Under "Tasks" click the "Run new task" button

New Task

  1. Choose "Task" as the Application Type
  2. Choose the newly created Task Definition under the "Family" drop-down menu and choose a name for the service

Create task

  1. Under "Networking" -> "Security Group" select "Create a new security group"
  2. Choose a name and description for the new group
  3. For "Type" select "All traffic" and for "Source" select "Anywhere"

Security group

  1. Click "Create" to create and deploy the task
  2. Select the task currently running and copy the public IP address displayed on the right side of the page

Public IP

  1. In a new browser tab, type the IP address in the format: http://PUBLICIP:8080/hello
  2. You should see a "Hello World" message displayed!

Hello world

Clean-Up

To prevent incurring additional charges after you are finished with the demo you first need to stop the service that you created. To do so, follow these instructions:

  1. Visit the Task Definitions page and select the task currently running
  2. Under Actions select "Deregister"
  3. Change the drop-down view menu to show "Inactive task definitions"
  4. Select the task definition and under Actions select "Delete"

Delete task

  1. Return to the cluster that you created and delete the task currently running
  2. Use the button on the top-right of the screen to delete the cluster itself

Delete cluster