Micronaut 1.0.3
Gradle 4.8
Java 1.8
AWS Credentials stored at /.aws/credentials in the home directory which can be created from the "My security credentials" section from the AWS dashboard. The credentials file has a form like
[default]
region=ap-south-1
aws_access_key_id=##################
aws_secret_access_key=##################
The default is profile name which can be changed if we are using multiple profiles based on the region. default
is used in the build.gradle at root of this project as
aws {
profileName = "default"
region = 'ap-south-1'
}
To set the region of lambda function, add the following block in the build.gradle file and this can be changed according to the region in use.
lambda {
region = 'ap-south-1'
}
In this app, the main function is defined in the MicronautFnAwsFunction
class and should be declared in the config as
mainClassName = "micronaut.fn.aws.MicronautFnAwsFunction"
It has a method named executeMnAwsFunction
which is invoked when we hit the lambda function.
For creating a JAR of the function:
micronaut-fn-aws git:(master) ✗ ./gradlew assemble
To deploy the micronaut function on the AWS Lambda or testing locally, we have setup and tested this function with the following steps:
micronaut-fn-aws git:(master) ✗ ./gradlew deploy
To make a post request, add your request body in the payload
as when you test from the console using invoke
gradle task.
payload = '{"customMessage": "This is custom message from user.", "customerId": "1"}'
The arguments can be customized based on the need in the method defined executeMnAwsFunction
by changing its parameters.
For an example:
The defined micronaut function is
CustomResponse executeMnAwsFunction(CustomRequest request) {
String customerId = request.customerId
CustomResponse customResponse = new CustomResponse()
if (customerId) {
customResponse.customer = customerService.findCustomerById(customerId)
}
customResponse.fromInterestService = interestService.methodName
customResponse.fromMathService = mathematicsService.getEvenNumbers()
customResponse.message = request.customMessage ?: 'No message from console'
return customResponse
}
It accepts a request, a custom type, argument only.
To make a GET request, you do not need a payload
and micronaut function should be no args.
Create a test event using the request body:
{
"customMessage": "This is custom message from user.",
"customerId": "1"
}
and hit the test to see the results and summay details.
Use the following commands, where option in {} denotes the input to the lambda function created.
libs git:(master) ✗ java -jar micronaut-fn-aws-0.1-all.jar '{"customMessage": "This is custom message from user.", "customerId": 1}'
micronaut-fn-aws git:(master) ✗ ./gradlew invoke
> Task :invoke
Lambda function result:
{
"message":"This is custom message from user.",
"customer":{
"id":1,
"name":"John",
"address":{
"line":"Pune",
"zipCode":"411007",
"country":{
"name":"India"
}
}
},
"fromMathService":"[2, 4, 6, 8]",
"fromInterestService":" This is getMethodName from InterestService."
}
and see what happens when we pass the wrong details in the request params:
In the invoke
task, if we change the customerId to 2 which doesn't exists in the records,
Now our payload is
payload = '{"customMessage": "This is custom message from user.", "customerId": "2"}' // 2 doesn't exists
then response will become
➜ micronaut-fn-aws git:(master) ✗ ./gradlew invoke
> Task :invoke
Lambda function result:
{
"errorMessage":"No customer found with id: 2. Please input a valid id",
"errorType":"com.micronaut.exception.InvalidIdException"
}
The deployed micronaut function can be consumed by any service by giving its detail in the application.yml file but to allow direct access the micronaut function, you need to deploy the API on AWS.
Follow these steps to deploy the function on AWS API Gateway:
- Select the API Gateway from the amazon services.
- Click on the
Create New API
where you need to provide the API name and description.In my case, we have added nameMicronautFnAPI
.
- In the created API, from the Action drop down, select action create method then add a method
POST
where you need to provide the name of deployed lambda function and click on save.
- Go to the
Models
, create a model with the required properties that we may need to use for the function as
{
"type": "object",
"properties": {
"customerId": {
"type": "string"
},
"customMessage": {
"type": "string"
}
}
}
then click on create model.
- Now go to
Resources
, selectPOST
method and addmodel
which is created in the Models section in 4th step. you can also add various metrics here.
- Ready to test now. go to the
test
onResources
and add a request body and click onTest
. You will see the results as
{
"message": "Test",
"customer": {
"id": 1,
"name": "John",
"address": {
"line": "Pune",
"zipCode": "411007",
"country": {
"name": "India"
}
}
},
"fromMathService": "[2, 4, 6, 8]",
"fromInterestService": " This is getMethodName from InterestService."
}
- Now API is tested and ready to deploy the API to access outside the AWS. Go to the
Actions
and click onDeploy API
where you will be required to selectDeployment stage
. Select [New stage] and name it beta or alpha. After deploying it, you will get an URL to invoke the Lambda function on the serverless.
- Now you have the
Invoke URL
for the use withPOST
request.