3

I need to configure lambda function to access RDS db(mysql) using Ruby code. I found one blog which is doing same but it's in python. Can anyone help me how we can do it using Ruby code?

1
  • 1
    Show your work plz sir Commented Apr 15, 2021 at 14:31

1 Answer 1

3

Assuming you've been doing your diligence work of creating a ruby project according to the lambda guidelines on Amazon (https://docs.aws.amazon.com/lambda/latest/dg/lambda-ruby.html) it's a matter of creating a Gemfile and in here add the gem mysql2.

An example of what you're looking for might be like this.

require 'json'
require 'mysql2'

$client = Mysql2::Client.new(
  host: ENV["DB_HOST"],
  username: ENV["DB_USER"],
  password: ENV["DB_PASSWORD"],
  database: ENV["DB_NAME"],
  port: ENV["DB_PORT"]
)

def lambda_handler(event:, context:)
  # Add your query here
  results = $client.query("SELECT * FROM items;").to_a

  {
    statusCode: 200,
    body: results.to_json
  }
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.