Back to Examples

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.

Set up the prerequisites

For instructions, see 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.
$ bal new aws_lambda_s3_triggerCreated new package 'aws_lambda_s3_trigger' at /Users/wso2/aws_lambda_s3_trigger.
  • 2.Replace the content of the generated Ballerina file with the content below.
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;
}

Build the function

Execute the command below to generate the AWS Lambda artifacts.

$ bal buildCompiling source        wso2/aws_lambda_s3_trigger:0.1.0
Generating executable        @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-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

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.

$ 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

Invoke the function

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

  • 1.Go to the AWS 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.
PreviousExecution context
NextDynamoDB trigger