Background

I am in charge to rewrite viuGraph’s backend API from Ruby on Rails to serverless version with AWS Lambda. We will use JavaScript NodeJS for the new viuGraph’s backend API, a languange that supported by AWS Lambda natively. Another service provided by Amazon Web Service that will used is Amazon Simple Notification Service (SNS). We will use it for decoupling photo & video upload event by user and photo & video processing.

Problem Definition

  1. What task will be required to rewrite viugGraph’s backend API from Ruby on Rails to AWS Lambda?
  2. What source code sample to allow our Ruby on Rails app communicate with AWS Lambda function?

Migration Tasks to Amazon SNS

Current condition of viuGraph’s backend API is:

  1. using Ruby on Rails as API app,
  2. using Redis to decouple API from long-running background worker and
  3. using Sidekiq to perform asynchronous background task.

Required tasks to rewrite background worker to AWS Lambda are:

  1. setup Amazon SNS topic,
  2. rewrite background worker code from Ruby on Rails to AWS Lambda (in case NodeJS for viuGraph),
  3. setup AWS Lambda to be triggered by Amazon SNS message and
  4. write code using Ruby AWS SDK to publish new Amazon SNS message.

Publishing Amazon SNS Message with Ruby

Here the sample code:

Aws::SNS::Client.new(
    access_key_id: creds['access_key_id'],
    secret_access_key: creds['secret_access_key']
)

resp = client.publish({
    topic_arn: "a-viugraph-amazon-sns-topic",
    message: '{ "content_id": "1234" }'
})

Triggering Lambda with Amazon SNS

To allow Amazon SNS triggering an AWS Lambda function, you must configure it first. We can start from opening AWS Lambda Dashboard page at AWS Console as you can see at Figure 1.

AWS Lambda Dashboard
Figure 1. AWS Lambda Dashboard


Choose a function and then click Trigger tab. You can see the result at Figure 2.

Trigger tab for an AWS Lambda function
Figure 2. Trigger tab for an AWS Lambda function


Then click Add Trigger button, you will see a pop-up such as at Figure 3. Please choose SNS at the bottom of options list.

AWS Lambda trigger options
Figure 3. AWS Lambda trigger options


After that, choose an Amazon SNS topic to subscribe. If you publish an Amazon SNS message to the topic, you AWS Lambda function will triggered. You can look at Figure 4 for reference.

Amazon SNS topic selector to trigger AWS Lambda
Figure 4. Amazon SNS topic selector to trigger AWS Lambda


Finally, we have setup an Amazon SNS topic to trigger an AWS Lambda function if a message pushed to the notification topic. You can look at Figure 5 that trigger has been added

New trigger successfully added for AWS Lambda function acknowledgement
Figure 5. New trigger successfully added for AWS Lambda function acknowledgement

Get Amazon SNS Message from AWS Lambda

You can get Amazon SNS messssage from an AWS Lambda Function on this way:

exports.handler = (event, context, callback) => {
    const messagePlain = event.Records[0].Sns.Message

    const messageJson = JSON.parse(event.Records[0].Sns.Message)
}

Amazon Web Services listed examples of event for AWS Lambda, you can look all at Sample Events Published by Event Sources page. Here example event for AWS Lambda from Amazon SNS:

{
  "Records": [
    {
      "EventVersion": "1.0",
      "EventSubscriptionArn": "eventsubscriptionarn",
      "EventSource": "aws:sns",
      "Sns": {
        "SignatureVersion": "1",
        "Timestamp": "1970-01-01T00:00:00.000Z",
        "Signature": "EXAMPLE",
        "SigningCertUrl": "EXAMPLE",
        "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
        "Message": "Hello from SNS!",
        "MessageAttributes": {
          "Test": {
            "Type": "String",
            "Value": "TestString"
          },
          "TestBinary": {
            "Type": "Binary",
            "Value": "TestBinary"
          }
        },
        "Type": "Notification",
        "UnsubscribeUrl": "EXAMPLE",
        "TopicArn": topicarn,
        "Subject": "TestInvoke"
      }
    }
  ]
}

Conclusion

Required tasks to rewrite background worker to AWS Lambda are:

  1. setup Amazon SNS topic,
  2. rewrite background worker code from Ruby on Rails to AWS Lambda (in case NodeJS for viuGraph),
  3. setup AWS Lambda to be triggered by Amazon SNS message and
  4. write code using Ruby AWS SDK to publish new Amazon SNS message.

Source code sample to allow viuGraph’s Ruby on Rails app communicate with AWS Lambda function can viewed at:

  1. Publishing Amazon SNS Message with Ruby and
  2. Get Amazon SNS Message from AWS Lambda.