Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
 
 

Micronaut Native Pet store example

The Micronaut framework is compatible with Spring's annotations and makes it easy to use GraalVM to build application images into native binaries. Further, Micronaut includes builtin support for AWS Lambda.

This demo application shows how to use Micronaut to compile our standard pet store example, using Spring annotations, into a native binary with GraalVM and execute it in AWS Lambda. To run this demo, you will need to have Gradle installed as well as Docker to run the GraalVM build.

With all the pre-requisites installed including:

  • JDK 8 or above
  • Gradle 5.6.x

You should be able to build a native image of the application by running the docker-build.sh from the repository's root.

$ ./docker-build.sh

The build process will perform the following tasks:

  1. Use gradle to compile the application
  2. Create a native-image folder in the current directory
  3. Check if a Docker image called graalvm-lambda-build exists on the local machine. If not, create it using the Dockerfile in the root of the repository
  4. Run the docker image to execute the GraalVM build process

The output of the build is an executable file called server and a deployable zip called function.zip in the native-image folder:

$ ls -lh native-image/
total 75M
-rwxr-xr-x 1 user user  36 Oct  1 16:01 bootstrap
-rw-r--r-- 1 user user 18M Oct  1 16:01 function.zip
-rwxr-xr-x 1 user user 57M Oct  1 16:01 server

To run the lambda locally, you can utilize the SAM cli by running ./sam-local.sh. This should start up the listeners in the PetsController, and you can test locally with your preferred http client.

For example, to test the GET /pets endpoint via curl:

curl localhost:3000/pets

You should see JSON output of pets.

To deploy the application to AWS Lambda you can use the pre-configured sam-native.yaml file included in the repo. Using the AWS or SAM CLI, run the following commands:

$ aws cloudformation package --template-file sam-native.yaml \
                             --output-template-file packaged-sam.yaml \
                             --s3-bucket <YOUR_S3_BUCKET>
Uploading to d3e2617f3c8c2e8ca78e35a0dc856884  18553505 / 18553505.0  (100.00%)
Successfully packaged artifacts and wrote output template to file packaged-sam.yaml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file /workspace/graal-spring-demo/packaged-sam.yaml --stack-name <YOUR STACK NAME>


$ aws cloudformation deploy --template-file ./packaged-sam.yaml --stack-name MicronautGraalVmDemo --capabilities CAPABILITY_IAM

Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - MicronautGraalVmDemo

You can use the AWS CLI to get the API endpoint generated by the deployment process:

aws cloudformation describe-stacks --stack-name MicronautGraalVmDemo --query Stacks[0].Outputs[0].OutputValue
"https://xxxxxxxxxx.execute-api.xx-xxxx-1.amazonaws.com/Prod/pets"

Make a test request to the API endpoint using curl or your preferred http client.

For example, to check the GET /pets endpoint via curl:

curl https://xxxxxxxxxx.execute-api.xx-xxxx-1.amazonaws.com/Prod/pets
Morty Proxy This is a proxified and sanitized view of the page, visit original site.