Skip to main content
The 2024 Developer Survey results are live! See the results
888

Expose a simple Ballerina program as an AWS Lambda Function

Created
Active
Last edited
Viewed 11k times
6 min read
-1

This article was written using Ballerina 2201.7.0 (Swan Lake Update 7) and is about exposing a simple Ballerina program as an AWS Lambda Function.

AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers[1].

Ballerina is an open-source programming language for the cloud that makes it easier to use, combine, and create network services[2].

The sections below describe how to expose a simple Ballerina program as an AWS Lambda Function.

Set up the prerequisites

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

Set up Ballerina

For instructions to download and install the latest Ballerina distribution, see Get started.

Set up AWS

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

Set up the AWS account

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

  1. Download and install the AWS CLI based on your operating system.

  2. Go to the AWS Portal and create an AWS account.

For more detailed instructions, see Module 1: Create Your AWS Account.

3. Go to the AWS Management Console.

4. Sign in to the AWS account you created as the root user.

Create an AWS user

Follow the steps below to create an AWS user.

  1. Go to the AWS Management Console.

  2. Click on the IAM service.

3. Click Users in the left navigation menu, and click Add users.

4. Enter the details below in the User details screen and click Next.

  • Add a name for the user.

  • Select the Provide user access to the AWS Management Console — optional* *option.

  • Select the I want to create an IAM user option.

5. Do the following in the Set permissions screen and click Next.

  • Select Attach policies directly.

  • Enter AWSLambda_FullAccess on the search bar and select it.

6. Click Create user in the Review and create screen.

7. Click Download .csv file in the Retrieve password screen to save the credentials of the created user as you will not be able to access this information later.

8. Click Return to users list to view the created user.

9. Obtain the access keys.

10. Enter the aws configure** **command and enter the details below in the CLI to configure the AWS CLI for the created user.

Create a new role

Follow the steps below to create an AWS role.

  1. Go to the AWS Management Console, and click on the IAM service.

  2. Click Roles in the left navigation menu, and click Create role.

3. Do the following in the Select trusted entity screen, and click Next.

  • Select AWS Service.

  • Select Lambda.

4. In the Add permissions screen, enter AWSLambda_FullAccess on the search bar, select it, and click Next.

5. Add the Role name in the Name, review, and create screen, and click Create role.

6. Click on the created role.

7. Copy the role ARN to use when the Lambda function is being deployed.

Create the function

Follow the steps below to create the Ballerina program, which will be exposed as the AWS Lambda function.

  1. In the CLI, execute the command below to create a new Ballerina project.

    bal new aws_lambda_hello_world
    

    You view the output below.

    Created new package 'aws_lambda_hello_world' at /Users/praneesha/Desktop/aws-lambda-hello-world.
    
    
  2. Replace the content of the generated Ballerina file (`main.bal` file) with the content below.

    import ballerina/io;
    import ballerinax/aws.lambda;
    
    // The `@lambda:Function` annotation marks a function to generate an AWS Lambda function.
    @lambda:Function
    public function echo(lambda:Context ctx, json input) returns json {
        io:println(input.toJsonString());
        return input;
    }
    

Build the function

Navigate to the created project root directory named aws-lambda-hello-world, and execute the command below to build the program.

bal build

You view the output below.

Compiling source
 praneesha/aws_lambda_hello_world:0.1.0

Generating executable
 @aws.lambda:Function: echo

 Run the following command to deploy each Ballerina AWS Lambda function:
 aws lambda create-function --function-name $FUNCTION_NAME --zip-file fileb:///Users/praneesha/Desktop/aws-lambda-hello-world/target/bin/aws-ballerina-lambda-functions.zip --handler aws_lambda_hello_world.$FUNCTION_NAME --runtime provided --role $LAMBDA_ROLE_ARN --layers arn:aws:lambda:$REGION_ID:134633749276:layer:ballerina-jre11:6 --memory-size 512 --timeout 10

 Run the following command to re-deploy an updated Ballerina AWS Lambda function:
 aws lambda update-function-code --function-name $FUNCTION_NAME --zip-file fileb://aws-ballerina-lambda-functions.zip


 target/bin/aws_lambda_hello_world.jar

Deploy the function

The above output you get when building the function will include the commands you need to deploy the function.

You need to replace the $FUNCTION_NAME, $LAMBDA_ROLE_ARN, and $REGION_ID you obtained above in these commands.

For example, execute the command below to deploy the function.

aws lambda create-function --function-name echo --zip-file fileb:///Users/praneesha/Desktop/aws-lambda-hello-world/target/bin/aws-ballerina-lambda-functions.zip --handler aws_lambda_hello_world.echo --runtime provided --role arn:aws:iam::758454776668:role/testAWSLambda --layers arn:aws:lambda:us-east-1:134633749276:layer:ballerina-jre11:6 --memory-size 512 --timeout 10

Follow the steps below to view the deployed function.

  1. Go to the AWS Management Console.

  2. Click on the Lambda service.

You view the deployed function added to the list.

Invoke the function

Follow the steps below to invoke the function.

  1. Execute the command below to bind an input JSON file with the response.

    echo '{"MESSAGE":"HELLO"}' > input.json
    
  2. Execute the command below to invoke the function by passing the payload.

    aws lambda invoke --function-name echo --payload fileb://input.json echo-response.txt
    

    You view the output below.

    {
    "StatusCode": 200
    "ExecutedVersion": "$LATEST",
    }
    
  3. Execute the commands below to view the response.

    You view the response below.

    {"MESSAGE":"HELLO"}
    

References

[1] https://aws.amazon.com/lambda/

[2] https://ballerina.io/