AWS Lambda

Simple step by step tutorial on how to use rollbar-gem with AWS Lambda

In November 2018, Amazon added support for Ruby to their popular cloud computation solution AWS Lambda. We are very happy to say that rollbar-gem can be added to your Lambda functions.

In this tutorial, we will build a simple AWS Lambda function to send a log message to Rollbar. We will also package it and deploy as an Amazon CloudFormation application.

If you don't want to go through this whole tutorial, just know that rollbar-gem will work perfectly fine in your Lambda functions as long as you include it as a bundler dependency in your deployed application. If you're not sure how to do this, continue reading.

Requirements for a sample application

  • Amazon Web Services account
  • ruby-2.5 on your development environment
  • bundler gem on your development environment
  • AWS CLI on your development environment (read more here) or a prepared AWS S3 bucket
  • AWS SAM CLI on your development environment (read more here)

Building the application

  1. Make sure you run ruby-2.5 on your development environment. This is important, as bundler gem will prepare a package appropriate for the ruby version you are using. If you are using rvm to manage multiple Ruby binaries, make sure you switch to ruby-2.5.
  2. Create an S3 bucket to store your application code (through the aws CLI or S3 Management Console). Note: if you have never used the AWS CLI, you will need to provide your AWS credentials first. Make sure to note the bucketname you are using, as we will use it again in step 8:
aws s3 mb s3://<bucketname>
  1. Create your application directory:
mkdir rollbar-lambda && cd rollbar-lambda
  1. Add the following code to Gemfile:
# Gemfile
source 'https://rubygems.org'
gem 'rollbar'
  1. Add the following code to rollbar-lambda.rb. Make sure to replace POST_SERVER_ITEM_ACCESS_TOKEN with your Rollbar project's access token:
# rollbar-lambda.rb
require 'rollbar'

def report(event:,context:)
  Rollbar.configure do |config|
    config.access_token = "POST_SERVER_ITEM_ACCESS_TOKEN"
    config.environment = "rollbar-lambda"
    # Other Configuration Settings
  end

  Rollbar.debug("Rollbar message from rollbar-lambda app")
end
  1. Add the following code to template.yaml:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'rollbar-lambda application'

Resources:
    RollbarLambdaFunction:
       Type: AWS::Serverless::Function
       Properties:
           Handler: rollbar-lambda.report
           Runtime: ruby2.5

Outputs:
    RollbarLambdaFunction:
        Description: Rollbar Lambda Function
        Value:
            Fn::GetAtt:
            - RollbarLambdaFunction
            - Arn
  1. Package gem dependencies for deployment:
bundle install && bundle install --deployment
  1. In step 2 we created an S3 bucket to store the application code. We will now use it to deploy our code. First, let's package the function:
sam package \
--template-file template.yaml \
--output-template-file packaged-template.yaml \
--s3-bucket <bucketname>
  1. And now, let's deploy to Amazon:
sam deploy \
--template-file packaged-template.yaml \
--stack-name rollbarLambda \
--capabilities CAPABILITY_IAM

Testing the application

rollbarLambda should now be available in your AWS Lambda console as an application stack. Navigate into the function overview to execute a test run.

  1. In the Lambda console, you will find a Test button up the top. Click it.
  2. You will be presented with a test configuration popup. Make sure Create new test event radio box is selected. In the Event name field put rollbarLambdaTest and leave the test input content as it is or make it an empty JSON object: {}. The test input content really doesn't matter in our case.
  3. Click Create at the bottom of the popup.
  4. Now, you should have rollbarLambdaTest preselected as the preferred test to execute, just left of the Test button. Click Test to run the function.

If everything went well, you should receive Execution result: succeeded message in AWS Lambda console and a new item reported in your Rollbar project.