Practical Serverless: Integrating Amazon S3 and Rekognition with Ballerina
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.
Implementation
Figure 1 shows the high level deployment architecture of our solution.
Listing 1 contains the Ballerina implementation for the processImage Lambda function.
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.
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.
- AWS account and generated security credentials (with permissions for Lambda deployment and S3 object access)
- AWS CLI
- GMail API keys — instructions found here
Now that we have the AWS Lambda zip artifact (aws-ballerina-lambda-functions.zip) 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.
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.
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.
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.
After a successful upload, we will receive an email similar to the one shown in Figure 5.
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.
Summary
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: