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 environmentbundler 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
- Make sure you run
ruby-2.5
on your development environment. This is important, asbundler
gem will prepare a package appropriate for the ruby version you are using. If you are usingrvm
to manage multiple Ruby binaries, make sure you switch toruby-2.5
. - 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 thebucketname
you are using, as we will use it again in step 8:
aws s3 mb s3://<bucketname>
- Create your application directory:
mkdir rollbar-lambda && cd rollbar-lambda
- Add the following code to
Gemfile
:
# Gemfile
source 'https://rubygems.org'
gem 'rollbar'
- Add the following code to
rollbar-lambda.rb
. Make sure to replacePOST_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
- 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
- Package gem dependencies for deployment:
bundle install && bundle install --deployment
- 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>
- 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.
- In the Lambda console, you will find a
Test
button up the top. Click it. - You will be presented with a test configuration popup. Make sure
Create new test event
radio box is selected. In theEvent name
field putrollbarLambdaTest
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. - Click
Create
at the bottom of the popup. - Now, you should have
rollbarLambdaTest
preselected as the preferred test to execute, just left of theTest
button. ClickTest
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.
Updated 10 months ago