Skip to content

Added Ruby code snippet for Lambda-Kinesis with and without batch item handling #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'aws-sdk'

def lambda_handler(event:, context:)
batch_item_failures = []

event['Records'].each do |record|
begin
puts "Processed Kinesis Event - EventID: #{record['eventID']}"
record_data = get_record_data_async(record['kinesis'])
puts "Record Data: #{record_data}"
# TODO: Do interesting work based on the new data
rescue StandardError => err
puts "An error occurred #{err}"
# Since we are working with streams, we can return the failed item immediately.
# Lambda will immediately begin to retry processing from this failed item onwards.
return { batchItemFailures: [{ itemIdentifier: record['kinesis']['sequenceNumber'] }] }
end
end

puts "Successfully processed #{event['Records'].length} records."
{ batchItemFailures: batch_item_failures }
end

def get_record_data_async(payload)
data = Base64.decode64(payload['data']).force_encoding('utf-8')
# Placeholder for actual async work
sleep(1)
data
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Using AWS Lambda with Amazon Kinesis with Batch Item Handling.",
"type": "Integration",
"services": ["lambda", "kinesis"],
"languages": ["Node", "TypeScript", ".NET"],
"languages": ["Node", "TypeScript", ".NET", "Ruby"],
"tags": [],
"introBox": {
"headline": "How it works",
Expand Down Expand Up @@ -76,6 +76,19 @@
]
},
{

"id": "Ruby",
"title": "Usage Example with Ruby:",
"description": "Consuming Kinesis event with Lambda using Ruby with batch item handling.",
"snippets": [
{
"snippetPath": "example.rb",
"language": "rb"
}
]
},
{

"id": "PHP",
"title": "Usage Example with PHP",
"description": "Consuming Kinesis event with Lambda using PHP with batch item handling.",
Expand All @@ -94,6 +107,7 @@
{
"snippetPath": "example.go",
"language": "go"

}
]
},
Expand Down Expand Up @@ -128,6 +142,12 @@
"linkedin": "mtomeh",
"twitter": "mtomeh84"
},
{
"headline": "Ruby Example Presented by Sanskar",
"name": "Sanskar",
"bio": "Cloud Support Engineer at AWS",
"linkedin": "Sanskar05"
},
{
"headline": "PHP Example Presented by Jan Richter",
"name": "Jan Richter",
Expand Down
23 changes: 23 additions & 0 deletions integration-kinesis-to-lambda/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'aws-sdk'

def lambda_handler(event:, context:)
event['Records'].each do |record|
begin
puts "Processed Kinesis Event - EventID: #{record['eventID']}"
record_data = get_record_data_async(record['kinesis'])
puts "Record Data: #{record_data}"
# TODO: Do interesting work based on the new data
rescue => err
$stderr.puts "An error occurred #{err}"
raise err
end
end
puts "Successfully processed #{event['Records'].length} records."
end

def get_record_data_async(payload)
data = Base64.decode64(payload['data']).force_encoding('UTF-8')
# Placeholder for actual async work
# You can use Ruby's asynchronous programming tools like async/await or fibers here.
return data
end
23 changes: 21 additions & 2 deletions integration-kinesis-to-lambda/snippet-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"description": "Using AWS Lambda with Amazon Kinesis without Batch Item Handling.",
"type": "Integration",
"services": ["lambda", "kinesis"],
"languages": ["Node", "TypeScript", ".NET", "Java", "Go", "Python", "PHP"],

"languages": ["Node", "TypeScript", ".NET", "Java", "Go", "Python", "PHP","Ruby"],

"tags": [],
"introBox": {
"headline": "How it works",
Expand Down Expand Up @@ -97,6 +99,17 @@
}
]
},
{
"id": "Ruby",
"title": "Usage Example with Ruby:",
"description": "Consuming Kinesis event with Lambda using Ruby without batch item handling.",
"snippets": [
{
"snippetPath": "example.rb",
"language": "rb"
}
]
},
{
"id": "PHP",
"title": "Usage Example with PHP",
Expand Down Expand Up @@ -142,6 +155,12 @@
"bio": "Cloud Support Engineer at AWS",
"linkedin": "umangaggarwal"
},
{
"headline": "Ruby Example Presented by Sanskar",
"name": "Sanskar",
"bio": "Cloud Support Engineer at AWS",
"linkedin": "Sanskar05"
},
{
"headline": "PHP Example Presented by Jan Richter",
"name": "Jan Richter",
Expand All @@ -150,4 +169,4 @@
"linkedin": "falnyr"
}
]
}
}