AWS Lambda

The AWS Lambda extension provides the functionality to write AWS Lambda-compatible packages by exposing a Ballerina function as an AWS Lambda function.

Info: Ballerina functions can be deployed in AWS Lambda by annotating a Ballerina function with @awslambda:Function adhering to the following function signature:
function (awslambda:Context, json|EventType) returns json|error

Supported triggers

An AWS Lambda function can be triggered by various AWS services. You can find the list of supported notification types below.

Set up the prerequisites

Follow the instructions in the sections below to set up the prerequisites.

Set up the AWS account

Follow the steps below to set up an AWS account and the AWS CLI.

  1. Install the AWS CLI.

  2. Create an AWS account and sign in to it.

Create a user

Follow the steps below to create a new user in your AWS account.

  1. Create a new AWS user.

    Note: Enter a username and enable access to the AWS Management Console.

  2. Set the permissions to the created user.

    Note: Enable AWSLambda_FullAccess or higher permissions.

  3. Obtain the access keys.

  4. Configure the AWS CLI for the user by providing the access key and secret generated in the user creation.

Create a role

Follow the steps below to create a new role in your AWS account.

  1. Create a new role.

  2. Set the permissions to the created role.

    Info: Enable AWSLambda_FullAccess or higher permissions.

  3. Access the newly created role in the AWS Console, and copy the role ARN to use when the Lambda function is being deployed.

Create the function

You can write AWS Lambda functions that use different triggers based on your use case.

Functions annotated as @awslambda:Function should always have the first parameter with the awslambda:Contextobject, which contains the information and operations related to the current function execution in AWS Lambda such as the request ID and the remaining execution time.

The second parameter with the json value contains the input request data. This input value format will vary depending on the source, which invoked the function (e.g., an AWS S3 bucket update event). The return type of the function is json. When the function is triggered by the event, the function body executes and it simply logs the input JSON and returns the JSON.

Info: For examples of creating AWS Lambda functions, see Examples.

Build the function

The AWS Lambda functionality is implemented as a compiler extension. Therefore, artifact generation happens automatically when you build a Ballerina module by executing the command below.

Copy
$ bal build --cloud="aws_lambda"

*Tip You can append the --graalvm flag to the above build command to build the native executable. This executable will have a much smaller memory footprint and faster startup time. For more information, see Build the executable in a container.

Deploy the function

The AWS Lambda functionality in Ballerina is implemented as a custom AWS Lambda layer. This information is provided when the function is created. The compiler generates the aws-ballerina-lambda-functions.zip file, which encapsulates all the AWS Lambda functions that are generated. This ZIP file can be used with the AWS web console or the AWS CLI to deploy the functions.

To deploy the function, execute the command, which you get in the CLI output logs after you build the function. For examples, see Examples.

Note: When you are deploying, make sure to replace the $FUNCTION_NAME, $LAMBDA_ROLE_ARN, and $REGION_ID placeholders with the corresponding values you obtained when setting up the prerequisites.

Info: For the supported parameters, go to the create-function documentation. You might need to change parameters such as the MemorySize and Timeout depending on your application and connection speed.

Examples

In a more practical scenario, the AWS Lambda functions will be used by associating them with an external event source such as Amazon DynamoDB or Amazon SQS. For more information on this, go to AWS Lambda event source mapping documentation.

For examples of the usage of AWS Lambda functions, see AWS Lambda.

Previous