import ballerinax/awslambda;
import ballerina/system;
@awslambda:Function
public function echo(awslambda:Context ctx, json input) returns json|error {
   return input;
}@awslambda:Function
public function uuid(awslambda:Context ctx, json input) returns json|error {
   return system:uuid();
}@awslambda:Function
public function ctxinfo(awslambda:Context ctx, json input) returns json|error {
   json result = { RequestID: ctx.getRequestId(),
                   DeadlineMS: ctx.getDeadlineMs(),
                   InvokedFunctionArn: ctx.getInvokedFunctionArn(),
                   TraceID: ctx.getTraceId(),
                   RemainingExecTime: ctx.getRemainingExecutionTime() };
   return result;
}# Prerequisites: AWS CLI tools installation and configuration

AWS Lambda Deployment

AWS Lambda is an event driven, serverless computing platform. Ballerina functions can be deployed in AWS Lambda by annotating a a Ballerina function with “@awslambda:Function”, which should have the function signature function (awslambda:Context, json) returns json|error.

import ballerinax/awslambda;
import ballerina/system;
@awslambda:Function
public function echo(awslambda:Context ctx, json input) returns json|error {
   return input;
}

The @awslambda:Function annotation marks a function to generate an AWS Lambda function

@awslambda:Function
public function uuid(awslambda:Context ctx, json input) returns json|error {
   return system:uuid();
}
@awslambda:Function
public function ctxinfo(awslambda:Context ctx, json input) returns json|error {
   json result = { RequestID: ctx.getRequestId(),
                   DeadlineMS: ctx.getDeadlineMs(),
                   InvokedFunctionArn: ctx.getInvokedFunctionArn(),
                   TraceID: ctx.getTraceId(),
                   RemainingExecTime: ctx.getRemainingExecutionTime() };
   return result;
}

The awslambda:Context object contains request execution context information

# Prerequisites: AWS CLI tools installation and configuration
# Build the Ballerina program to generate the AWS Lambda functions
$ ./ballerina build aws_lambda_deployment.bal
Compiling source
    aws_lambda_deployment.bal
Generating executable
    aws_lambda_deployment.balx
	@awslambda:Function: echo, uuid, ctxinfo
        The Ballerina AWS Lambda layer information can be found at https://ballerina.io/deployment/aws-lambda.
	Run the following commands to deploy each Ballerina AWS Lambda function:
	aws lambda create-function --function-name <FUNCTION_NAME> --zip-file fileb://aws-ballerina-lambda-functions.zip --handler aws_lambda_deployment.<FUNCTION_NAME> --runtime provided --role <LAMBDA_ROLE_ARN> --timeout 10 --memory-size 1024
	aws lambda update-function-configuration --function-name <FUNCTION_NAME> --layers <BALLERINA_LAYER_ARN>
	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
# Execute the AWS CLI commands to create and publish the functions, and your AWS <LAMBDA_ROLE_ARN> and the <BALLERINA_LAYER_ARN> for a specific Ballerina release should be used, following values are just examples
$ aws lambda create-function --function-name echo --zip-file fileb://aws-ballerina-lambda-functions.zip --handler aws_lambda_deployment.echo --runtime provided --role --role arn:aws:iam::908363916111:role/lambda-role
 --timeout 10 --memory-size 1024
$ aws lambda create-function --function-name uuid --zip-file fileb://aws-ballerina-lambda-functions.zip --handler aws_lambda_deployment.uuid --runtime provided --role --role arn:aws:iam::908363916111:role/lambda-role
 --timeout 10 --memory-size 1024
$ aws lambda create-function --function-name ctxinfo --zip-file fileb://aws-ballerina-lambda-functions.zip --handler aws_lambda_deployment.ctxinfo --runtime provided --role --role arn:aws:iam::908363916111:role/lambda-role
 --timeout 10 --memory-size 1024
$ aws lambda update-function-configuration --function-name echo --layers arn:aws:lambda:us-west-2:908363916138:layer:ballerina-x_xxx_x-runtime:1
$ aws lambda update-function-configuration --function-name uuid --layers arn:aws:lambda:us-west-2:908363916138:layer:ballerina-x_xxx_x-runtime:1
$ aws lambda update-function-configuration --function-name ctxinfo --layers arn:aws:lambda:us-west-2:908363916138:layer:ballerina-x_xxx_x-runtime:1
# Invoke the functions
$ aws lambda invoke --function-name echo --payload '{"MESSAGE":"HELLO"}' response.txt
{
    "ExecutedVersion": "$LATEST", 
    "StatusCode": 200
}
$ cat echo-response.txt 
{"MESSAGE":"HELLO"}
$ aws lambda invoke --function-name uuid uuid-response.txt
{
    "ExecutedVersion": "$LATEST", 
    "StatusCode": 200
}
$ cat uuid-response.txt 
"711cd328-1937-40cc-9078-c3628c6edb02"
$ aws lambda invoke --function-name ctxinfo ctxinfo-response.txt
{
    "ExecutedVersion": "$LATEST", 
    "StatusCode": 200
}
$ cat ctxinfo-response.txt 
{"RequestID":"d55f7d06-f2ab-4b6e-8606-482607785a91", "DeadlineMS":1548069389978, "InvokedFunctionArn":"arn:aws:lambda:us-west-2:908363916138:function:ctxinfo", "TraceID":"Root=1-5c45aa03-f8aff4c9e24dc4fbf48f2990;Parent=17ad3b290def98fd;Sampled=0", "RemainingExecTime":9946}