Skip to content

fbourguignon/micronaut-security-serverless-sample

Repository files navigation

micronaut-security-serverless-sample

Sample serverless application using Micronaut (Security, Data, AWS), API Gateway and GraalVM

Build for JVM execution

micronaut-security-serverless-sample % make build

Build for GraalVM execution

micronaut-security-serverless-sample % make build-graalvm

Start Application

This command starts the docker containers, sam local api and build the project.

micronaut-security-serverless-sample % make run

Debug Application

1 - Configure intellij remote debugging. picture 2 - Execute this command

micronaut-security-serverless-sample % make debug

3 - Send request

4 - Start debug


SQL Commands

You can use docker exec to execute sql commands in local cockroach. After the first execution, use this command to insert role in table.

docker exec cockroach-db /cockroach/cockroach sql --insecure --database=security --execute="INSERT INTO public.tb_role (id, "type") VALUES(1, 'ROLE_USER');"

Local Execution

Create user

curl -X "POST" "http://localhost:3000/users/register" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
    "email": "user@gmail.com",
    "password": "12345678"
}'

Authenticate user

curl -X "POST" "http://localhost:3000/login" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
    "username": "user@gmail.com",
    "password": "12345678"
}'

Get user profile

curl -X "GET" "http://localhost:3000/users/profile" \
-H 'Content-Type: application/json; charset=utf-8' \
--header 'Authorization: Bearer {{access_token}}' 

Cockroach client

http://localhost:8080

picture

Deploy for GraalVM Compilation

1 - For GraalVM compilations, create an aws lambda function with Amazon Linux 2 custom runtime. picture

2 - Upload the zip file generated by the build

micronaut-security-serverless-sample/build/libs/micronaut-security-serverless-sample-0.1-lambda.zip

picture

3 - Add the environment variables with cockroach database parameters. You can create a free tier serverless cockroach database picture

4 - Change memory to 256 MB picture

Integrate with API Gateway

1 - Create a REST API Gateway picture

2 - Map each route in the API gateway and enable lambda integration proxy picture picture picture

3 - Deploy API gateway stage

Requests to api gateway

  • Create user
curl -X "POST" "https://{api-gateway-alias}.execute-api.us-east-1.amazonaws.com/prod/users/register" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
    "email": "user@gmail.com",
    "password": "12345678"
}'
  • Authenticate user
curl -X "POST" "https://{api-gateway-alias}.execute-api.us-east-1.amazonaws.com/prod/login" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
    "username": "user@gmail.com",
    "password": "12345678"
}'
  • Get user profile
curl -X "GET" "https://{api-gateway-alias}.execute-api.us-east-1.amazonaws.com/prod/users/profile" \
-H 'Content-Type: application/json; charset=utf-8' \
--header 'Authorization: Bearer {{access_token}}' 

About

Proof of concept using Micronaut, GraalVM, SAM, Cockroach and AWS Lambda

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published