Practical Serverless: Integrating Amazon S3 and Rekognition with Ballerina

Anjana Fernando
Ballerina Swan Lake Tech Blog
4 min readAug 31, 2020

In this article, we will take a look at how Ballerina’s AWS Lambda functionality can be used in implementing an integration scenario between the AWS Simple Storage Service (S3) and the AWS Rekognition service.

In this use case, the users will upload images to an S3 bucket where the Lambda function will listen to. Once an image is uploaded, the Lambda function is triggered, and it will carry out an image analysis using the AWS Rekognition service. The result of this analysis will be emailed to the user.


Figure 1 shows the high level deployment architecture of our solution.

Figure 1: Image Analysis Scenario Deployment

Listing 1 contains the Ballerina implementation for the processImage Lambda function.

Listing 1: Ballerina Lambda Function Implementation

In the above code, we can see the processImage function annotated with the @awslambda:Function, which signals to the Ballerina compiler that this is a function to be deployed to AWS Lambda. We have mentioned the expected event type as awslambda:S3Event in the second function parameter, since we will be associating this Lambda function with an S3 trigger later on.

In the function, we have initialized the Ballerina Rekognition connector by providing the AWS access and secret keys via environment variables. Thereafter, we loop through all the S3 records, and call the detectLabels remote method in the Rekognition connector. This takes in both direct binary data, or else, S3 object information in doing the image processing. Here, we are returned an array of labels as the response from the service, which we will use as the data in creating an HTML table structure.

The above implementation source code can be found here as well. In the next section, we will see how to build our code and deploy.

Build and Deployment

The Ballerina AWS Lambda function code is built in the same way as we would build a general Ballerina program. In the compilation process, the compiler engages the required compiler extensions to process any specific annotations that are used in the code.

Listing 2: Building the Ballerina AWS Lambda Function

As shown in Listing 2, the Ballerina compiler identifies the AWS Lambda functions, and generates the required artifacts for deployment, and also prints out the AWS CLI commands that can be used in the deployment.

For deploying the generated Lambda function in AWS. The following prerequisites must be met first.

Now that we have the AWS Lambda zip artifact ( built using the Ballerina compiler, we can deploy it using either the AWS Lambda web console, or use the AWS CLI. In this instance we will be using the AWS CLI to deploy our Lambda function as it is shown below in Listing 3.

Listing 3: Deploying the Ballerina AWS Lambda Function

The function is now deployed in AWS Lambda. However, now we still have to configure the event trigger for the function and set the required environment variables to provide the API keys. For this, we will be using the AWS web console. Let’s navigate to the configuration page of the respective AWS Lambda function, and configure an S3 input trigger to notify on any new object creations.

The trigger configuration will look similar to Figure 2.

Figure 2: S3 Trigger Configuration for Lambda Function

Next, in the Lambda function’s web console, we will add the environment variable values to provide the API key information for Amazon Rekognition and GMail.

The final configured processImage Lambda function will look similar to Figure 3.

Figure 3: Fully Configured Lambda Function

Now we have the Lambda function fully deployed and configured. Let’s take a look at it in-action in the next section.

Demo Run

In order to execute the Lambda function, we simply need to upload an image to our S3 bucket. Let’s upload the image shown in Figure 4 below to the target location.

Figure 4: Input Image to the processImage Lambda Function

After a successful upload, we will receive an email similar to the one shown in Figure 5.

Figure 5: Image Analysis Result from the Lambda Function

In the same manner, we can experiment with different image uploads, where the Amazon Rekognition service contacted by the Lambda function will return their respective analysis.


In this article, we have looked at how to implement an integration scenario between Amazon S3 and Rekognition using the AWS Lambda serverless platform. In order to implement the scenario, we have used the Ballerina language, which has built-in support for deploying a Ballerina function in AWS Lambda.

For more information on writing serverless functions in Ballerina, refer to the following resources:



Anjana Fernando
Ballerina Swan Lake Tech Blog

Software Engineer @ Google; ex: WSO2 | 🇱🇰 🇺🇸 | — @lafernando