icon | description |
---|---|
aws |
BoxLang Runtime for AWS Lambda! Serverless for the win! |
AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS) that lets you run code without provisioning or managing servers. It automatically scales applications by running code in response to events and allocates compute resources as needed, allowing developers to focus on writing code rather than managing infrastructure (https://docs.aws.amazon.com/lambda/).
{% embed url="https://docs.aws.amazon.com/lambda/" %}
The BoxLang AWS Runtime allows you to run a Lambda.bx
function in this ecosystem. We provide you a nice template so you can work with serverless: https://github.com/ortus-boxlang/bx-aws-lambda-template.
{% @github-files/github-code-block url="https://github.com/ortus-boxlang/bx-aws-lambda-template" %}
The runtime provides a handler for lambda already configured to accept JSON in as a BoxLang Struct and then output either by returning a simple or complex object or using our response
convention struct. The default handler is:
ortus.boxlang.runtime.aws.LambdaRunner::handleRequest
You can see the code for the handler here: https://github.com/ortus-boxlang/boxlang-aws-lambda/blob/development/src/main/java/ortus/boxlang/runtime/aws/LambdaRunner.java#L161
The handler is Java, but you can easily build it with BoxLang. However, we have done the hard parts for you so you can focus on your lambda function.
The following are the env variables available in the runtime:
Environmeent | Description |
---|---|
BOXLANG_LAMBDA_CLASS | Absolute path to the lambda to execute. The default path is:
Which is your lambda deployed within your zip file. |
BOXLANG_LAMBDA_DEBUGMODE | Turn runtime debug mode or not. |
BOXLANG_LAMBDA_CONFIG | Absolute path to a custom boxlang.json configuration for the runtime. |
The BoxLang default template for AWS lambda can be found here: https://github.com/ortus-boxlang/bx-aws-lambda-template. It is a Gradle project for now, it will be migrated to CommandBox soon. The structure of the project is the following
/.vscode - Some useful vscode tasks and settings
/build - Not in source control, where your build and distributions happen
/gradle - The gradle runtime, keep in source control
/src
+ main
+ bx
+ Lambda.bx (Your BoxLang Lambda function)
+ test
+ java
+ com
+ myproject
+ LambdaRunnerTest.java (An integration test for your Lambda)
+ TestContext.java (A testing context for lambda)
+ TestLogger.java (A testing logger for lambda)
/workbench - Tons of AWS lambda utilities
/box.json - Your project's dependency descriptor for CommandBox
/build.gradle - The gradle build
/gradle.properties - Where you store your version and metadata
/gradlew - The gradle shell executor, keep in source control
/gradlew.bat - The gradle shell executor, keep in source control
/settings.gradle - The project settings
The BoxLang AWS Lambda runtime will look for a Lambda.bx
in your project and execute it by default. Run the following commands to prep for coding:
# Download the runtime, later it will be automatic via maven
./gradlew downloadBoxLang
# Run the tests
./gradlew test
# Build the project, create the lambda zip
# The location is /build/distributions/boxlang-aws-project-1.0.0.zip
./gradlew build
# Mess up? No problem, remove the /build folder or run
./gradlew clean
The Lambda function is a BoxLang class with a single function called run()
.
/**
* My BoxLang Lambda
*/
class{
function run( event, context, response ){
response.body = {
"error": false,
"messages": [],
"data": "====> Incoming event " & event.toString()
};
response.statusCode = 200;
}
}
It accepts three arguments:
Argument | Type | Description |
---|---|---|
event | Struct | All the incoming JSON as a BoxLang struct |
context | com.amazonaws.services.lambda.runtime.Context | The AWS context object. You can find much more information here. |
response | Struct | A BoxLang struct convention for a response. |
{% hint style="success" %} You can find more information about the AWS Context object here: https://docs.aws.amazon.com/lambda/latest/dg/java-context.html {% endhint %}
The event
structure is a snapshot of the input to your lambda. We deserialize the incoming JSON for you and give you a nice struct.
This is an Amazon Java class that provides extensive information about the request. For more information, check out the API in the Amazon Docs (https://docs.aws.amazon.com/lambda/latest/dg/java-context.html)
getRemainingTimeInMillis()
– Returns the number of milliseconds left before the execution times out.getFunctionName()
– Returns the name of the Lambda function.getFunctionVersion()
– Returns the version of the function.getInvokedFunctionArn()
– Returns the Amazon Resource Name (ARN) that's used to invoke the function. Indicates if the invoker specified a version number or alias.getMemoryLimitInMB()
– Returns the amount of memory that's allocated for the function.getAwsRequestId()
– Returns the identifier of the invocation request.getLogGroupName()
– Returns the log group for the function.getLogStreamName()
– Returns the log stream for the function instance.getIdentity()
– (mobile apps) Returns information about the Amazon Cognito identity that authorized the request.getClientContext()
– (mobile apps) Returns the client context that's provided to Lambda by the client application.getLogger()
– Returns the logger object for the function.
The response
argument is our convention to help you build a nice return structure. However, it is completely optional. You can easily return a simple or complex object from your lambda, and we will convert it to JSON.
response : {
statusCode : 200,
headers : {
content-type : "application/json",
access-control-allow-origin : "*",
},
body : YourLambda.run() results
}
{% code lineNumbers="true" %}
/**
* My BoxLang Lambda Simple Return
*/
class{
function run( event, context, response ){
return "Hello World!"
}
}
/**
* My BoxLang Lambda Complex Return
*/
class{
function run( event, context, response ){
return {
age : 1,
when : now(),
data : [ 12,3234,23423 ]
};
}
}
{% endcode %}
Now you can go ahead and build your function. You can use TestBox to unit test your Lambda.bx
or we even include a src/test
folder in Java, that simulates the full life-cycle of the runtime. Just run gradle test
or use VSCode BoxLang IDE to run the tests. Now we go to production!
Log in to the Lambda Console and click on Create function
button.
AWS Lambda Console
Now let's add the basic information about our deployment:
- Add a function name
- Choose
Java 21
as your runtime - Choose
x86_64
as your architecture
Create a function
Now, let's upload our test code. You can choose a zip/jar or s3 location. We will do a simple zip from our template:
Upload Test Code
Code Uploaded
Now important, let's choose the AWS Lambda Runtime Handler in the Edit runtime settings
And make sure you use the following handler address: ortus.boxlang.runtime.aws.LambdaRunner::handleRequest
This is inside the runtime to execute our Lambda.bx
function.
Now click on the Test
tab and an event name MyTestEvent
. You can also add anything you like in the Event JSON
to test the input of your lambda. Then click on Test
and watch it do its magic. Now go have some good fun!
The AWS Runtime source code can be found here: https://github.com/ortus-boxlang/boxlang-aws-lambda
We welcome any pull requests, testing, docs, etc.