{{announcement.body}}
{{announcement.title}}

Practical Serverless: Integrating Amazon S3 and Rekognition with Ballerina

DZone 's Guide to

Practical Serverless: Integrating Amazon S3 and Rekognition with Ballerina

Take a look at Ballerina’s AWS Lambda functionality, used to implement an integration scenario between AWS Simple Storage Service (S3) and AWS Rekognition.

· Microservices Zone ·
Free Resource

Introduction

In this use case, a user will upload images to an S3 bucket where the Lambda function will listen in. 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. 

Figure 1: Image Analysis Scenario Deployment

Figure 1: Image Analysis Scenario Deployment


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

Listing 1: Ballerina Lambda Function Implementation

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 S3 object information to perform 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 source code for the above implementation can be found here as well. In the next section, we will see how to build our code and deploy it. 

Build and Deploy

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. 

Shell
 




x
14


1
$ ballerina build functions.bal
2
Compiling source
3
    functions.bal
4
 
          
5
Generating executables
6
    functions.jar
7
    @awslambda:Function: processImage
8
 
          
9
    Run the following command to deploy each Ballerina AWS Lambda function:
10
    aws lambda create-function --function-name <FUNCTION_NAME> --zip-file fileb://aws-ballerina-lambda-functions.zip --handler functions.<FUNCTION_NAME> --runtime provided --role <LAMBDA_ROLE_ARN> --layers arn:aws:lambda:<REGION_ID>:141896495686:layer:ballerina:2
11
 
          
12
    Run the following command to re-deploy an updated Ballerina AWS Lambda function:
13
    aws lambda update-function-code --function-name <FUNCTION_NAME> --zip-file fileb://aws-ballerina-lambda-functions.zip
14
 
          


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. It also prints out the AWS CLI commands that can be used in the deployment. 

To deploy the generated Lambda function in AWS. The following prerequisites must be met first. 

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.

Shell
 




xxxxxxxxxx
1
26


 
1
$ aws lambda create-function --function-name processImage --zip-file fileb://aws-ballerina-lambda-functions.zip --handler functions.processImage --runtime provided --role arn:aws:iam::908363916138:role/lambda-role --layers arn:aws:lambda:us-west-1:141896495686:layer:ballerina:2 --memory-size 512 --timeout 10
2
{
3
    "FunctionName": "processImage",
4
    "FunctionArn": "arn:aws:lambda:us-west-1:908363916138:function:processImage",
5
    "Runtime": "provided",
6
    "Role": "arn:aws:iam::908363916138:role/lambda-role",
7
    "Handler": "functions.processImage",
8
    "CodeSize": 22745880,
9
    "Description": "",
10
    "Timeout": 10,
11
    "MemorySize": 512,
12
    "LastModified": "2020-08-28T14:35:38.558+0000",
13
    "CodeSha256": "YzePibKR4BPJzfHyPQkORKU6uqe/FahYrnGkxFsn87I=",
14
    "Version": "$LATEST",
15
    "TracingConfig": {
16
        "Mode": "PassThrough"
17
    },
18
    "RevisionId": "b1f0bee4-76c0-41ca-998b-24ee5deaa0d8",
19
    "Layers": [
20
        {
21
            "Arn": "arn:aws:lambda:us-west-1:141896495686:layer:ballerina:2",
22
            "CodeSize": 697
23
        }
24
    ]
25
}
26
 
          


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

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

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

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

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

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. 

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. 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:

Topics:
aws lambda, ballerina, image processing, microservices, s3, serverless

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}