Skip to content

Commit

Permalink
Merge pull request #4720 from ballerina-platform/revamp-faas-bbes
Browse files Browse the repository at this point in the history
[Master] - Revamp FaaS BBEs
  • Loading branch information
praneesha authored Aug 8, 2023
2 parents 6512cdd + 9c5cbd1 commit b48a4a2
Show file tree
Hide file tree
Showing 81 changed files with 921 additions and 184 deletions.
3 changes: 0 additions & 3 deletions examples/aws-lambda-deployment/aws_deploy.out

This file was deleted.

27 changes: 0 additions & 27 deletions examples/aws-lambda-deployment/aws_lambda_deployment.bal

This file was deleted.

23 changes: 0 additions & 23 deletions examples/aws-lambda-deployment/aws_lambda_deployment.md

This file was deleted.

1 change: 0 additions & 1 deletion examples/aws-lambda-deployment/bal_new.out

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ballerina/io;
import ballerinax/aws.lambda;

@lambda:Function
public function dynamoDBTrigger(lambda:Context ctx,
lambda:DynamoDBEvent event) returns json {
io:println(event.Records[0].dynamodb.Keys.toString());
return event.Records[0].dynamodb.Keys.toString();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# AWS Lambda - DynamoDB trigger

This example creates a function, which will be executed for each entry added to a database in the [DynamoDB](https://aws.amazon.com/dynamodb/).

For more information, see the [AWS Lambda learn guide](https://ballerina.io/learn/run-in-the-cloud/function-as-a-service/aws-lambda/).

## Set up the prerequisites

For instructions, see [Set up the prerequisites](https://ballerina.io/learn/run-in-the-cloud/function-as-a-service/aws-lambda/#set-up-the-prerequisites).

## Write the function

Follow the steps below to write the function.

1. Execute the command below to create a new Ballerina package.

::: out bal_new.out :::

2. Replace the content of the generated Ballerina file with the content below.

::: code aws-lambda-dynamodb-trigger.bal :::

## Build the function

Execute the command below to generate the AWS Lambda artifacts.

::: out bal_build.out :::

## Deploy the function

Execute the AWS CLI command given by the compiler to create and publish the functions by replacing the respective AWS `$LAMBDA_ROLE_ARN`, `$REGION_ID`, and `$FUNCTION_NAME` values given in the command with your values.

::: out aws_deploy.out :::

## Invoke the function

Follow the instructions below to create a DynamoDB table for invoking this function.

1. In the IAM Console, click the corresponding role in the list, and click **Add permissions**.
2. Select **attach policies** from the drop-down menu, and add the **AWSLambdaDynamoDBExecutionRole** to the role.
3. Go to [DynamoDB](https://console.aws.amazon.com/dynamodbv2), and from the drop-down menu at the top RHS of the screen, select the **AWS region** in which you created the user and role.
4. Click **Create Table**, enter a table name and a partition key, and create the table (if you already have a table created, you can skip this step).
5. Click on the DynamoDB table you created, and then click the **Exports and streams** tab.
6. Click **Turn on** under **DynamoDB stream details**, select **Key attributes only** for the event type, and click **Turn on stream**.
8. Under the **Trigger** section, click **Create trigger**, select the `dynamoDBTrigger` from the drop-down, and click **Create trigger**.
9. Click **Explore table items**, and click **Create items** under the **Items returned** section.
10. Enter a value under the **Attributes** section to add an entry to the DynamoDB table to invoke the Lambda function, and click **Create item**.
11. Click the **Monitor** tab of the Lambda function in the AWS Management Console, and click **View CloudWatch logs** to check the logs via CloudWatch.
11. Under **Log streams** in CloudWatch, click on the topmost stream in the list and verify the object name in the logs.
12. Go to the AWS Lambda function and check the logs via CloudWatch to see the object identifier in the logs.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: This example creates a function, which will be executed for each entry added to a database in the DynamoDB.
keywords: ballerina, ballerina by example, aws lambda, dynamodb, serverless, cloud, function as a service
1 change: 1 addition & 0 deletions examples/aws-lambda-dynamodb-trigger/aws_deploy.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ aws lambda create-function --function-name dynamoDBTrigger --zip-file fileb://aws-ballerina-lambda-functions.zip --handler aws-lambda-dynamodb-trigger.dynamoDBTrigger --runtime provided --role arn:aws:iam::908363916111:role/lambda-role--layers arn:aws:lambda:us-west-1:134633749276:layer:ballerina-jre11:6 --memory-size 512 --timeout 10
12 changes: 12 additions & 0 deletions examples/aws-lambda-dynamodb-trigger/bal_build.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$ bal build
Compiling source
wso2/aws_lambda_dynamodb_trigger:0.1.0

Generating executable
@awslambda:Function: dynamoDBTrigger

Run the following command to deploy each Ballerina AWS Lambda function:
aws lambda create-function --function-name $FUNCTION_NAME --zip-file fileb://<project-dir>/aws-lambda-s3-trigger/target/bin/aws-ballerina-lambda-functions.zip --handler aws-lambda-dynamodb-trigger.$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
2 changes: 2 additions & 0 deletions examples/aws-lambda-dynamodb-trigger/bal_new.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$ bal new aws_lambda_dynamodb_trigger
Created new package 'aws_lambda_dynamodb_trigger' at /Users/wso2/aws_lambda_dynamodb_trigger.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import ballerinax/aws.lambda;

// The `lambda:Context` object contains request execution context information.
@lambda:Function
public function ctxinfo(lambda:Context ctx, json input) returns json|error {
return {
RequestID: ctx.getRequestId(),
DeadlineMS: ctx.getDeadlineMs(),
InvokedFunctionArn: ctx.getInvokedFunctionArn(),
TraceID: ctx.getTraceId(),
RemainingExecTime: ctx.getRemainingExecutionTime()
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# AWS Lambda - Execution context

The example below demonstrates how the execution context information of an AWS function can be retrieved.

For more information, see the [AWS Lambda learn guide](https://ballerina.io/learn/run-in-the-cloud/function-as-a-service/aws-lambda/).

## Set up the prerequisites

For instructions, see [Set up the prerequisites](https://ballerina.io/learn/run-in-the-cloud/function-as-a-service/aws-lambda/#set-up-the-prerequisites).

## Write the function

Follow the steps below to write the function.

1. Execute the command below to create a new Ballerina package.

::: out bal_new.out :::

2. Replace the content of the generated Ballerina file with the content below.

::: code aws-lambda-execution-context.bal :::

## Build the function

Execute the command below to generate the AWS Lambda artifacts.

::: out bal_build.out :::

## Deploy the function

Execute the AWS CLI command given by the compiler to create and publish the functions by replacing the respective AWS `$LAMBDA_ROLE_ARN`, `$REGION_ID`, and `$FUNCTION_NAME` values given in the command with your values.

::: out aws_deploy.out :::

## Invoke the function

Execute the commands below to invoke the function.

::: out invoke_functions.out :::
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: The example below demonstrates how the execution context information of an AWS function can be retrieved.
keywords: ballerina, ballerina by example, aws lambda, execution context, serverless, cloud, function as a service
1 change: 1 addition & 0 deletions examples/aws-lambda-execution-context/aws_deploy.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ aws lambda create-function --function-name ctxinfo --zip-file fileb://aws-ballerina-lambda-functions.zip --handler aws-lambda-execution-context.ctxinfo --runtime provided --role arn:aws:iam::908363916111:role/lambda-role --layers arn:aws:lambda:us-west-1:134633749276:layer:ballerina-jre11:6 --memory-size 512 --timeout 10
12 changes: 12 additions & 0 deletions examples/aws-lambda-execution-context/bal_build.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$ bal build
Compiling source
wso2/aws_lambda_execution_context:0.1.0

Generating executable
@awslambda:Function: ctxinfo

Run the following command to deploy each Ballerina AWS Lambda function:
aws lambda create-function --function-name $FUNCTION_NAME --zip-file fileb://<project-dir>/aws-lambda-execution-context/target/bin/aws-ballerina-lambda-functions.zip --handler aws_lambda_deployment.$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
2 changes: 2 additions & 0 deletions examples/aws-lambda-execution-context/bal_new.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$ bal new aws_lambda_execution_context
Created new package 'aws_lambda_execution_context' at /Users/wso2/aws_lambda_execution_context.
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
$ echo '{"MESSAGE":"HELLO"}' > input.json
$ aws lambda invoke --function-name echo --payload fileb://input.json echo-response.txt
{
"ExecutedVersion": "$LATEST",
"StatusCode": 200
}
$ cat echo-response.txt
{"MESSAGE":"HELLO"}

$ aws lambda invoke --function-name ctxinfo ctxinfo-response.txt
{
"ExecutedVersion": "$LATEST",
Expand Down
9 changes: 9 additions & 0 deletions examples/aws-lambda-hello-world/aws-lambda-hello-world.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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;
}
39 changes: 39 additions & 0 deletions examples/aws-lambda-hello-world/aws-lambda-hello-world.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# AWS Lambda - Hello world

This example demonstrates how to write a simple echo function in AWS Lambda.

For more information, see the [AWS Lambda learn guide](https://ballerina.io/learn/run-in-the-cloud/function-as-a-service/aws-lambda/).

## Set up the prerequisites

For instructions, see [Set up the prerequisites](https://ballerina.io/learn/run-in-the-cloud/function-as-a-service/aws-lambda/#set-up-the-prerequisites).

## Write the function

Follow the steps below to write the function.

1. Execute the command below to create a new Ballerina package.

::: out bal_new.out :::

2. Replace the content of the generated Ballerina file with the content below.

::: code aws-lambda-hello-world.bal :::

## Build the function

Execute the command below to generate the AWS Lambda artifacts.

::: out bal_build.out :::

## Deploy the function

Execute the AWS CLI command given by the compiler to create and publish the functions by replacing the respective AWS `$LAMBDA_ROLE_ARN`, `$REGION_ID`, and `$FUNCTION_NAME` values given in the command with your values.

::: out aws_deploy.out :::

## Invoke the function

Execute the commands below to invoke the function.

::: out invoke_functions.out :::
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: This example demonstrates how to write a simple echo function in AWS Lambda.
keywords: ballerina, ballerina by example, serverless, aws lambda, cloud, function as a service
1 change: 1 addition & 0 deletions examples/aws-lambda-hello-world/aws_deploy.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ aws lambda create-function --function-name echo --zip-file fileb://aws-ballerina-lambda-functions.zip --handler aws-lambda-hello-world.echo --runtime provided --role arn:aws:iam::908363916111:role/lambda-role--layers arn:aws:lambda:us-west-1:134633749276:layer:ballerina-jre11:6 --memory-size 512 --timeout 10
12 changes: 12 additions & 0 deletions examples/aws-lambda-hello-world/bal_build.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$ bal build
Compiling source
wso2/aws_lambda_hello_world:0.1.0

Generating executable
@awslambda:Function: echo

Run the following command to deploy each Ballerina AWS Lambda function:
aws lambda create-function --function-name $FUNCTION_NAME --zip-file fileb://<project-dir>/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
2 changes: 2 additions & 0 deletions examples/aws-lambda-hello-world/bal_new.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$ bal new aws_lambda_hello_world
Created new package 'aws_lambda_hello_world' at /Users/wso2/aws_lambda_hello_world.
8 changes: 8 additions & 0 deletions examples/aws-lambda-hello-world/invoke_functions.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$ echo '{"MESSAGE":"HELLO"}' > input.json
$ aws lambda invoke --function-name echo --payload fileb://input.json echo-response.txt
{
"ExecutedVersion": "$LATEST",
"StatusCode": 200
}
$ cat echo-response.txt
{"MESSAGE":"HELLO"}
9 changes: 9 additions & 0 deletions examples/aws-lambda-s3-trigger/aws-lambda-s3-trigger.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ballerina/io;
import ballerinax/aws.lambda;

@lambda:Function
public function s3Trigger(lambda:Context ctx,
lambda:S3Event event) returns json {
io:println(event.Records[0].s3.'object.key);
return event.Records[0].s3.'object.key;
}
50 changes: 50 additions & 0 deletions examples/aws-lambda-s3-trigger/aws-lambda-s3-trigger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# AWS Lambda - S3 trigger

This example creates a function, which will be executed for each object creation in AWS S3.

For more information, see the [AWS Lambda learn guide](https://ballerina.io/learn/run-in-the-cloud/function-as-a-service/aws-lambda/).

## Set up the prerequisites

For instructions, see [Set up the prerequisites](https://ballerina.io/learn/run-in-the-cloud/function-as-a-service/aws-lambda/#set-up-the-prerequisites).

## Write the function

Follow the steps below to write the function.

1. Execute the command below to create a new Ballerina package.

::: out bal_new.out :::

2. Replace the content of the generated Ballerina file with the content below.

::: code aws-lambda-s3-trigger.bal :::

## Build the function

Execute the command below to generate the AWS Lambda artifacts.

::: out bal_build.out :::

## Deploy the function

Execute the AWS CLI command given by the compiler to create and publish the functions by replacing the respective AWS `$LAMBDA_ROLE_ARN`, `$REGION_ID`, and `$FUNCTION_NAME` values given in the command with your values.

::: out aws_deploy.out :::

## Invoke the function

Follow the instructions below to create an S3 bucket in AWS for invoking this function.

1. Go to the [AWS S3](https://s3.console.aws.amazon.com/s3/) portal and create a bucket.
>**Note:** Make sure to select the same **AWS region** in which you created the AWS user and role when creating the S3 bucket.
2. Click on the created bucket, go to the **Properties** tab, and click **Create event notification** under the **Event notifications** section.
3. Enable **All object create events** under event types.
4. Under the **Destination** section, select the AWS Lambda function (i.e., `s3Trigger` in this example) from the dropdown.
5. Select the created bucket under the **Buckets** list, click **Upload**, and upload an object to the S3 bucket.
6. Under the **Functions** list of the AWS Management Console, click the AWS Lambda function, and click the **Monitor** tab.
7. If you get a **Missing permissions** notice at the top, click the **Open the IAM Console** in it.
8. In the IAM Console, click the corresponding role in the list, and click **Add permissions**.
9. Select **attach policies** from the drop-down menu, and add the **AWSLambdaBasicExecutionRole** to the role.
10. Click the **Monitor** tab of the Lambda function in the AWS Management Console, and click **View CloudWatch logs** to check the logs via CloudWatch.
11. Under **Log streams** in CloudWatch, click on the topmost stream in the list and verify the object name in the logs.
2 changes: 2 additions & 0 deletions examples/aws-lambda-s3-trigger/aws-lambda-s3-trigger.metatags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: This example creates a function, which will be executed for each object creation in AWS S3.
keywords: ballerina, ballerina by example, aws lambda, s3, trigger, serverless, cloud, function as a service
1 change: 1 addition & 0 deletions examples/aws-lambda-s3-trigger/aws_deploy.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ aws lambda create-function --function-name s3Trigger --zip-file fileb://aws-ballerina-lambda-functions.zip --handler aws-lambda-s3-trigger.s3Trigger --runtime provided --role arn:aws:iam::908363916111:role/lambda-role--layers arn:aws:lambda:us-west-1:134633749276:layer:ballerina-jre11:6 --memory-size 512 --timeout 10
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
$ bal build
Compiling source
wso2/aws_lambda_deployment:0.1.0
wso2/aws_lambda_s3_trigger:0.1.0

Generating executable
@awslambda:Function: echo, ctxinfo, notifyS3
@awslambda:Function: s3Trigger

Run the following command to deploy each Ballerina AWS Lambda function:
aws lambda create-function --function-name $FUNCTION_NAME --zip-file fileb://<project-dir>/aws_lambda_deployment/target/bin/aws-ballerina-lambda-functions.zip --handler aws_lambda_deployment.$FUNCTION_NAME --runtime provided --role $LAMBDA_ROLE_ARN --layers arn:aws:lambda:$REGION_ID:134633749276:layer:ballerina-jre11:6 --memory-size 512 --timeout 10
aws lambda create-function --function-name $FUNCTION_NAME --zip-file fileb://<project-dir>/aws-lambda-s3-trigger/target/bin/aws-ballerina-lambda-functions.zip --handler aws-lambda-s3-trigger.$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
2 changes: 2 additions & 0 deletions examples/aws-lambda-s3-trigger/bal_new.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$ bal new aws_lambda_s3_trigger
Created new package 'aws_lambda_s3_trigger' at /Users/wso2/aws_lambda_s3_trigger.
Loading

0 comments on commit b48a4a2

Please sign in to comment.