Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
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