There are more AWS SDK examples available in the AWS Doc SDK Examples
Lambda examples using SDK for Ruby
The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Ruby with Lambda.
Basics are code examples that show you how to perform the essential operations within a service.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Scenarios are code examples that show you how to accomplish specific tasks by calling multiple functions within a service or combined with other AWS services.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Get started
The following code examples show how to get started using Lambda.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. require 'aws-sdk-lambda' # Creates an AWS Lambda client using the default credentials and configuration def lambda_client Aws::Lambda::Client.new end # Lists the Lambda functions in your AWS account, paginating the results if necessary def list_lambda_functions lambda = lambda_client # Use a pagination iterator to list all functions functions = [] lambda.list_functions.each_page do |page| functions.concat(page.functions) end # Print the name and ARN of each function functions.each do |function| puts "Function name: #{function.function_name}" puts "Function ARN: #{function.function_arn}" puts end puts "Total functions: #{functions.count}" end list_lambda_functions if __FILE__ == $PROGRAM_NAME
-
For API details, see ListFunctions in AWS SDK for Ruby API Reference.
-
Basics
The following code example shows how to:
Create an IAM role and Lambda function, then upload handler code.
Invoke the function with a single parameter and get results.
Update the function code and configure with an environment variable.
Invoke the function with new parameters and get results. Display the returned execution log.
List the functions for your account, then clean up resources.
For more information, see Create a Lambda function with the console.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Set up pre-requisite IAM permissions for a Lambda function capable of writing logs.
# Get an AWS Identity and Access Management (IAM) role. # # @param iam_role_name: The name of the role to retrieve. # @param action: Whether to create or destroy the IAM apparatus. # @return: The IAM role. def manage_iam(iam_role_name, action) case action when 'create' create_iam_role(iam_role_name) when 'destroy' destroy_iam_role(iam_role_name) else raise "Incorrect action provided. Must provide 'create' or 'destroy'" end end private def create_iam_role(iam_role_name) role_policy = { 'Version': '2012-10-17', 'Statement': [ { 'Effect': 'Allow', 'Principal': { 'Service': 'lambda.amazonaws.com' }, 'Action': 'sts:AssumeRole' } ] } role = @iam_client.create_role( role_name: iam_role_name, assume_role_policy_document: role_policy.to_json ) @iam_client.attach_role_policy( { policy_arn: 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole', role_name: iam_role_name } ) wait_for_role_to_exist(iam_role_name) @logger.debug("Successfully created IAM role: #{role['role']['arn']}") sleep(10) [role, role_policy.to_json] end def destroy_iam_role(iam_role_name) @iam_client.detach_role_policy( { policy_arn: 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole', role_name: iam_role_name } ) @iam_client.delete_role(role_name: iam_role_name) @logger.debug("Detached policy & deleted IAM role: #{iam_role_name}") end def wait_for_role_to_exist(iam_role_name) @iam_client.wait_until(:role_exists, { role_name: iam_role_name }) do |w| w.max_attempts = 5 w.delay = 5 end end
Define a Lambda handler that increments a number provided as an invocation parameter.
require 'logger' # A function that increments a whole number by one (1) and logs the result. # Requires a manually-provided runtime parameter, 'number', which must be Int # # @param event [Hash] Parameters sent when the function is invoked # @param context [Hash] Methods and properties that provide information # about the invocation, function, and execution environment. # @return incremented_number [String] The incremented number. def lambda_handler(event:, context:) logger = Logger.new($stdout) log_level = ENV['LOG_LEVEL'] logger.level = case log_level when 'debug' Logger::DEBUG when 'info' Logger::INFO else Logger::ERROR end logger.debug('This is a debug log message.') logger.info('This is an info log message. Code executed successfully!') number = event['number'].to_i incremented_number = number + 1 logger.info("You provided #{number.round} and it was incremented to #{incremented_number.round}") incremented_number.round.to_s end
Zip your Lambda function into a deployment package.
# Creates a Lambda deployment package in .zip format. # # @param source_file: The name of the object, without suffix, for the Lambda file and zip. # @return: The deployment package. def create_deployment_package(source_file) Dir.chdir(File.dirname(__FILE__)) if File.exist?('lambda_function.zip') File.delete('lambda_function.zip') @logger.debug('Deleting old zip: lambda_function.zip') end Zip::File.open('lambda_function.zip', create: true) do |zipfile| zipfile.add('lambda_function.rb', "#{source_file}.rb") end @logger.debug("Zipping #{source_file}.rb into: lambda_function.zip.") File.read('lambda_function.zip').to_s rescue StandardError => e @logger.error("There was an error creating deployment package:\n #{e.message}") end
Create a new Lambda function.
# Deploys a Lambda function. # # @param function_name: The name of the Lambda function. # @param handler_name: The fully qualified name of the handler function. # @param role_arn: The IAM role to use for the function. # @param deployment_package: The deployment package that contains the function code in .zip format. # @return: The Amazon Resource Name (ARN) of the newly created function. def create_function(function_name, handler_name, role_arn, deployment_package) response = @lambda_client.create_function({ role: role_arn.to_s, function_name: function_name, handler: handler_name, runtime: 'ruby2.7', code: { zip_file: deployment_package }, environment: { variables: { 'LOG_LEVEL' => 'info' } } }) @lambda_client.wait_until(:function_active_v2, { function_name: function_name }) do |w| w.max_attempts = 5 w.delay = 5 end response rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error creating #{function_name}:\n #{e.message}") rescue Aws::Waiters::Errors::WaiterFailed => e @logger.error("Failed waiting for #{function_name} to activate:\n #{e.message}") end
Invoke your Lambda function with optional runtime parameters.
# Invokes a Lambda function. # @param function_name [String] The name of the function to invoke. # @param payload [nil] Payload containing runtime parameters. # @return [Object] The response from the function invocation. def invoke_function(function_name, payload = nil) params = { function_name: function_name } params[:payload] = payload unless payload.nil? @lambda_client.invoke(params) rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error executing #{function_name}:\n #{e.message}") end
Update your Lambda function's configuration to inject a new environment variable.
# Updates the environment variables for a Lambda function. # @param function_name: The name of the function to update. # @param log_level: The log level of the function. # @return: Data about the update, including the status. def update_function_configuration(function_name, log_level) @lambda_client.update_function_configuration({ function_name: function_name, environment: { variables: { 'LOG_LEVEL' => log_level } } }) @lambda_client.wait_until(:function_updated_v2, { function_name: function_name }) do |w| w.max_attempts = 5 w.delay = 5 end rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error updating configurations for #{function_name}:\n #{e.message}") rescue Aws::Waiters::Errors::WaiterFailed => e @logger.error("Failed waiting for #{function_name} to activate:\n #{e.message}") end
Update your Lambda function's code with a different deployment package containing different code.
# Updates the code for a Lambda function by submitting a .zip archive that contains # the code for the function. # # @param function_name: The name of the function to update. # @param deployment_package: The function code to update, packaged as bytes in # .zip format. # @return: Data about the update, including the status. def update_function_code(function_name, deployment_package) @lambda_client.update_function_code( function_name: function_name, zip_file: deployment_package ) @lambda_client.wait_until(:function_updated_v2, { function_name: function_name }) do |w| w.max_attempts = 5 w.delay = 5 end rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error updating function code for: #{function_name}:\n #{e.message}") nil rescue Aws::Waiters::Errors::WaiterFailed => e @logger.error("Failed waiting for #{function_name} to update:\n #{e.message}") end
List all existing Lambda functions using the built-in paginator.
# Lists the Lambda functions for the current account. def list_functions functions = [] @lambda_client.list_functions.each do |response| response['functions'].each do |function| functions.append(function['function_name']) end end functions rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error listing functions:\n #{e.message}") end
Delete a specific Lambda function.
# Deletes a Lambda function. # @param function_name: The name of the function to delete. def delete_function(function_name) print "Deleting function: #{function_name}..." @lambda_client.delete_function( function_name: function_name ) print 'Done!'.green rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error deleting #{function_name}:\n #{e.message}") end
-
For API details, see the following topics in AWS SDK for Ruby API Reference.
-
Actions
The following code example shows how to use CreateFunction
.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. class LambdaWrapper attr_accessor :lambda_client, :cloudwatch_client, :iam_client def initialize @lambda_client = Aws::Lambda::Client.new @cloudwatch_client = Aws::CloudWatchLogs::Client.new(region: 'us-east-1') @iam_client = Aws::IAM::Client.new(region: 'us-east-1') @logger = Logger.new($stdout) @logger.level = Logger::WARN end # Deploys a Lambda function. # # @param function_name: The name of the Lambda function. # @param handler_name: The fully qualified name of the handler function. # @param role_arn: The IAM role to use for the function. # @param deployment_package: The deployment package that contains the function code in .zip format. # @return: The Amazon Resource Name (ARN) of the newly created function. def create_function(function_name, handler_name, role_arn, deployment_package) response = @lambda_client.create_function({ role: role_arn.to_s, function_name: function_name, handler: handler_name, runtime: 'ruby2.7', code: { zip_file: deployment_package }, environment: { variables: { 'LOG_LEVEL' => 'info' } } }) @lambda_client.wait_until(:function_active_v2, { function_name: function_name }) do |w| w.max_attempts = 5 w.delay = 5 end response rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error creating #{function_name}:\n #{e.message}") rescue Aws::Waiters::Errors::WaiterFailed => e @logger.error("Failed waiting for #{function_name} to activate:\n #{e.message}") end
-
For API details, see CreateFunction in AWS SDK for Ruby API Reference.
-
The following code example shows how to use DeleteFunction
.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. class LambdaWrapper attr_accessor :lambda_client, :cloudwatch_client, :iam_client def initialize @lambda_client = Aws::Lambda::Client.new @cloudwatch_client = Aws::CloudWatchLogs::Client.new(region: 'us-east-1') @iam_client = Aws::IAM::Client.new(region: 'us-east-1') @logger = Logger.new($stdout) @logger.level = Logger::WARN end # Deletes a Lambda function. # @param function_name: The name of the function to delete. def delete_function(function_name) print "Deleting function: #{function_name}..." @lambda_client.delete_function( function_name: function_name ) print 'Done!'.green rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error deleting #{function_name}:\n #{e.message}") end
-
For API details, see DeleteFunction in AWS SDK for Ruby API Reference.
-
The following code example shows how to use GetFunction
.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. class LambdaWrapper attr_accessor :lambda_client, :cloudwatch_client, :iam_client def initialize @lambda_client = Aws::Lambda::Client.new @cloudwatch_client = Aws::CloudWatchLogs::Client.new(region: 'us-east-1') @iam_client = Aws::IAM::Client.new(region: 'us-east-1') @logger = Logger.new($stdout) @logger.level = Logger::WARN end # Gets data about a Lambda function. # # @param function_name: The name of the function. # @return response: The function data, or nil if no such function exists. def get_function(function_name) @lambda_client.get_function( { function_name: function_name } ) rescue Aws::Lambda::Errors::ResourceNotFoundException => e @logger.debug("Could not find function: #{function_name}:\n #{e.message}") nil end
-
For API details, see GetFunction in AWS SDK for Ruby API Reference.
-
The following code example shows how to use Invoke
.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. class LambdaWrapper attr_accessor :lambda_client, :cloudwatch_client, :iam_client def initialize @lambda_client = Aws::Lambda::Client.new @cloudwatch_client = Aws::CloudWatchLogs::Client.new(region: 'us-east-1') @iam_client = Aws::IAM::Client.new(region: 'us-east-1') @logger = Logger.new($stdout) @logger.level = Logger::WARN end # Invokes a Lambda function. # @param function_name [String] The name of the function to invoke. # @param payload [nil] Payload containing runtime parameters. # @return [Object] The response from the function invocation. def invoke_function(function_name, payload = nil) params = { function_name: function_name } params[:payload] = payload unless payload.nil? @lambda_client.invoke(params) rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error executing #{function_name}:\n #{e.message}") end
-
For API details, see Invoke in AWS SDK for Ruby API Reference.
-
The following code example shows how to use ListFunctions
.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. class LambdaWrapper attr_accessor :lambda_client, :cloudwatch_client, :iam_client def initialize @lambda_client = Aws::Lambda::Client.new @cloudwatch_client = Aws::CloudWatchLogs::Client.new(region: 'us-east-1') @iam_client = Aws::IAM::Client.new(region: 'us-east-1') @logger = Logger.new($stdout) @logger.level = Logger::WARN end # Lists the Lambda functions for the current account. def list_functions functions = [] @lambda_client.list_functions.each do |response| response['functions'].each do |function| functions.append(function['function_name']) end end functions rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error listing functions:\n #{e.message}") end
-
For API details, see ListFunctions in AWS SDK for Ruby API Reference.
-
The following code example shows how to use UpdateFunctionCode
.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. class LambdaWrapper attr_accessor :lambda_client, :cloudwatch_client, :iam_client def initialize @lambda_client = Aws::Lambda::Client.new @cloudwatch_client = Aws::CloudWatchLogs::Client.new(region: 'us-east-1') @iam_client = Aws::IAM::Client.new(region: 'us-east-1') @logger = Logger.new($stdout) @logger.level = Logger::WARN end # Updates the code for a Lambda function by submitting a .zip archive that contains # the code for the function. # # @param function_name: The name of the function to update. # @param deployment_package: The function code to update, packaged as bytes in # .zip format. # @return: Data about the update, including the status. def update_function_code(function_name, deployment_package) @lambda_client.update_function_code( function_name: function_name, zip_file: deployment_package ) @lambda_client.wait_until(:function_updated_v2, { function_name: function_name }) do |w| w.max_attempts = 5 w.delay = 5 end rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error updating function code for: #{function_name}:\n #{e.message}") nil rescue Aws::Waiters::Errors::WaiterFailed => e @logger.error("Failed waiting for #{function_name} to update:\n #{e.message}") end
-
For API details, see UpdateFunctionCode in AWS SDK for Ruby API Reference.
-
The following code example shows how to use UpdateFunctionConfiguration
.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. class LambdaWrapper attr_accessor :lambda_client, :cloudwatch_client, :iam_client def initialize @lambda_client = Aws::Lambda::Client.new @cloudwatch_client = Aws::CloudWatchLogs::Client.new(region: 'us-east-1') @iam_client = Aws::IAM::Client.new(region: 'us-east-1') @logger = Logger.new($stdout) @logger.level = Logger::WARN end # Updates the environment variables for a Lambda function. # @param function_name: The name of the function to update. # @param log_level: The log level of the function. # @return: Data about the update, including the status. def update_function_configuration(function_name, log_level) @lambda_client.update_function_configuration({ function_name: function_name, environment: { variables: { 'LOG_LEVEL' => log_level } } }) @lambda_client.wait_until(:function_updated_v2, { function_name: function_name }) do |w| w.max_attempts = 5 w.delay = 5 end rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error updating configurations for #{function_name}:\n #{e.message}") rescue Aws::Waiters::Errors::WaiterFailed => e @logger.error("Failed waiting for #{function_name} to activate:\n #{e.message}") end
-
For API details, see UpdateFunctionConfiguration in AWS SDK for Ruby API Reference.
-
Scenarios
The following code example shows how to create an application that analyzes customer comment cards, translates them from their original language, determines their sentiment, and generates an audio file from the translated text.
- SDK for Ruby
-
This example application analyzes and stores customer feedback cards. Specifically, it fulfills the need of a fictitious hotel in New York City. The hotel receives feedback from guests in various languages in the form of physical comment cards. That feedback is uploaded into the app through a web client. After an image of a comment card is uploaded, the following steps occur:
-
Text is extracted from the image using Amazon Textract.
-
Amazon Comprehend determines the sentiment of the extracted text and its language.
-
The extracted text is translated to English using Amazon Translate.
-
Amazon Polly synthesizes an audio file from the extracted text.
The full app can be deployed with the AWS CDK. For source code and deployment instructions, see the project in GitHub
. Services used in this example
Amazon Comprehend
Lambda
Amazon Polly
Amazon Textract
Amazon Translate
-
Serverless examples
The following code example shows how to implement a Lambda function that connects to an RDS database. The function makes a simple database request and returns the result.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Connecting to an Amazon RDS database in a Lambda function using Ruby.
# Ruby code here. require 'aws-sdk-rds' require 'json' require 'mysql2' def lambda_handler(event:, context:) endpoint = ENV['DBEndpoint'] # Add the endpoint without https" port = ENV['Port'] # 3306 user = ENV['DBUser'] region = ENV['DBRegion'] # 'us-east-1' db_name = ENV['DBName'] credentials = Aws::Credentials.new( ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'], ENV['AWS_SESSION_TOKEN'] ) rds_client = Aws::RDS::AuthTokenGenerator.new( region: region, credentials: credentials ) token = rds_client.auth_token( endpoint: endpoint+ ':' + port, user_name: user, region: region ) begin conn = Mysql2::Client.new( host: endpoint, username: user, password: token, port: port, database: db_name, sslca: '/var/task/global-bundle.pem', sslverify: true, enable_cleartext_plugin: true ) a = 3 b = 2 result = conn.query("SELECT #{a} + #{b} AS sum").first['sum'] puts result conn.close { statusCode: 200, body: result.to_json } rescue => e puts "Database connection failed due to #{e}" end end
The following code example shows how to implement a Lambda function that receives an event triggered by receiving records from a Kinesis stream. The function retrieves the Kinesis payload, decodes from Base64, and logs the record contents.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming an Kinesis event with Lambda using Ruby.
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 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
The following code example shows how to implement a Lambda function that receives an event triggered by receiving records from a DynamoDB stream. The function retrieves the DynamoDB payload and logs the record contents.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming a DynamoDB event with Lambda using Ruby.
def lambda_handler(event:, context:) return 'received empty event' if event['Records'].empty? event['Records'].each do |record| log_dynamodb_record(record) end "Records processed: #{event['Records'].length}" end def log_dynamodb_record(record) puts record['eventID'] puts record['eventName'] puts "DynamoDB Record: #{JSON.generate(record['dynamodb'])}" end
The following code example shows how to implement a Lambda function that receives an event triggered by receiving records from a DocumentDB change stream. The function retrieves the DocumentDB payload and logs the record contents.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming a Amazon DocumentDB event with Lambda using Ruby.
require 'json' def lambda_handler(event:, context:) event['events'].each do |record| log_document_db_event(record) end 'OK' end def log_document_db_event(record) event_data = record['event'] || {} operation_type = event_data['operationType'] || 'Unknown' db = event_data.dig('ns', 'db') || 'Unknown' collection = event_data.dig('ns', 'coll') || 'Unknown' full_document = event_data['fullDocument'] || {} puts "Operation type: #{operation_type}" puts "db: #{db}" puts "collection: #{collection}" puts "Full document: #{JSON.pretty_generate(full_document)}" end
The following code example shows how to implement a Lambda function that receives an event triggered by receiving records from an Amazon MSK cluster. The function retrieves the MSK payload and logs the record contents.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming an Amazon MSK event with Lambda using Ruby.
require 'base64' def lambda_handler(event:, context:) # Iterate through keys event['records'].each do |key, records| puts "Key: #{key}" # Iterate through records records.each do |record| puts "Record: #{record}" # Decode base64 msg = Base64.decode64(record['value']) puts "Message: #{msg}" end end end
The following code example shows how to implement a Lambda function that receives an event triggered by uploading an object to an S3 bucket. The function retrieves the S3 bucket name and object key from the event parameter and calls the Amazon S3 API to retrieve and log the content type of the object.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming an S3 event with Lambda using Ruby.
require 'json' require 'uri' require 'aws-sdk' puts 'Loading function' def lambda_handler(event:, context:) s3 = Aws::S3::Client.new(region: 'region') # Your AWS region # puts "Received event: #{JSON.dump(event)}" # Get the object from the event and show its content type bucket = event['Records'][0]['s3']['bucket']['name'] key = URI.decode_www_form_component(event['Records'][0]['s3']['object']['key'], Encoding::UTF_8) begin response = s3.get_object(bucket: bucket, key: key) puts "CONTENT TYPE: #{response.content_type}" return response.content_type rescue StandardError => e puts e.message puts "Error getting object #{key} from bucket #{bucket}. Make sure they exist and your bucket is in the same region as this function." raise e end end
The following code example shows how to implement a Lambda function that receives an event triggered by receiving messages from an SNS topic. The function retrieves the messages from the event parameter and logs the content of each message.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming an SNS event with Lambda using Ruby.
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 def lambda_handler(event:, context:) event['Records'].map { |record| process_message(record) } end def process_message(record) message = record['Sns']['Message'] puts("Processing message: #{message}") rescue StandardError => e puts("Error processing message: #{e}") raise end
The following code example shows how to implement a Lambda function that receives an event triggered by receiving messages from an SQS queue. The function retrieves the messages from the event parameter and logs the content of each message.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming an SQS event with Lambda using Ruby.
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 def lambda_handler(event:, context:) event['Records'].each do |message| process_message(message) end puts "done" end def process_message(message) begin puts "Processed message #{message['body']}" # TODO: Do interesting work based on the new message rescue StandardError => err puts "An error occurred" raise err end end
The following code example shows how to implement partial batch response for Lambda functions that receive events from a Kinesis stream. The function reports the batch item failures in the response, signaling to Lambda to retry those messages later.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Reporting Kinesis batch item failures with Lambda using Ruby.
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 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
The following code example shows how to implement partial batch response for Lambda functions that receive events from a DynamoDB stream. The function reports the batch item failures in the response, signaling to Lambda to retry those messages later.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Reporting DynamoDB batch item failures with Lambda using Ruby.
def lambda_handler(event:, context:) records = event["Records"] cur_record_sequence_number = "" records.each do |record| begin # Process your record cur_record_sequence_number = record["dynamodb"]["SequenceNumber"] rescue StandardError => e # Return failed record's sequence number return {"batchItemFailures" => [{"itemIdentifier" => cur_record_sequence_number}]} end end {"batchItemFailures" => []} end
The following code example shows how to implement partial batch response for Lambda functions that receive events from an SQS queue. The function reports the batch item failures in the response, signaling to Lambda to retry those messages later.
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Reporting SQS batch item failures with Lambda using Ruby.
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 require 'json' def lambda_handler(event:, context:) if event batch_item_failures = [] sqs_batch_response = {} event["Records"].each do |record| begin # process message rescue StandardError => e batch_item_failures << {"itemIdentifier" => record['messageId']} end end sqs_batch_response["batchItemFailures"] = batch_item_failures return sqs_batch_response end end