AWS Lambda
BoxLang Runtime for AWS Lambda! Serverless for the win!
Last updated
BoxLang Runtime for AWS Lambda! Serverless for the win!
Last updated
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/).
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.
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:
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 |
---|---|
| Absolute path to the lambda to execute. The default path is: Which is your lambda deployed within your zip file. |
| Turn runtime debug mode or not. |
| Absolute path to a custom |
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
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:
The Lambda function is a BoxLang class with a single function called run()
.
It accepts three arguments:
Argument | Type | Description |
---|---|---|
|
| All the incoming JSON as a BoxLang struct |
|
| |
|
| A BoxLang struct convention for a response. |
You can find more information about the AWS Context object here: https://docs.aws.amazon.com/lambda/latest/dg/java-context.html
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.
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.
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
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:
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.
The AWS context object. You can find much