Skip to content
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

Added Ruby sample for RDS IAM connection #187

Merged
merged 3 commits into from
Jun 22, 2024
Merged
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
79 changes: 79 additions & 0 deletions integration-docdb-to-lambda/snippet-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"title": "Integration of Amazon DocumentDB with AWS Lambda",
"description": "This snippet demonstrates how to integrate Amazon DocumentDB with an AWS Lambda function.",
"type": "Integration",
"services": ["lambda", "DocumentDB"],
"tags": [],
"languages": ["Python", "Go", "Ruby", "Javascript"],
"introBox": {
"headline": "How it works",
"text": [
"Amazon DocumentDB (with MongoDB compatibility) is a fast, scalable, highly available, and fully managed document database service that supports MongoDB workloads. AWS Lambda can be easily integrated with Amazon DocumentDB to build serverless applications that leverage the power of a fully managed document database"
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-snippets/tree/main/integration-docdb-to-lambda"
}
},
"snippets": [
{
"title": "Runtimes",
"codeTabs": [
{
"id": "Python",
"title": "Copy the code into your Lambda function",
"description": "Follow the AWS Lambda instructions for packaging with dependencies and uploading in .zip format.",
"snippets": [
{
"snippetPath": "example.py",
"language": "py"
}
]
},
{
"id": "Go",
"title": "Copy the code into your Lambda function",
"description": "Follow the AWS Lambda instructions for packaging with dependencies and uploading in .zip format.",
"snippets": [
{
"snippetPath": "main.go",
"language": "go"
}
]
},
{
"id": "Ruby",
"title": "Copy the code into your Lambda function",
"description": "Follow the AWS Lambda instructions for packaging with ruby dependencies with Ruby gem and bundler.",
"snippets": [
{
"snippetPath": "example.rb",
"language": "ruby"
}
]
},
{
"id": "Javascript",
"title": "Copy the code into your Lambda function",
"description": "Follow the AWS Lambda instructions for packaging with dependencies and uploading in .zip format. This snippet uses the mysql2/promise library.",
"snippets": [
{
"snippetPath": "example.js",
"language": "js"
}
]
}
]
}
],
"authors": [
{
"headline": "Presented by Abhishek Agawane",
"name": "Abhishek Agawane",
"image": "https://drive.google.com/file/d/1E-5koDaKEaMUtOctX32I9TLwfh3kgpAq/view?usp=drivesdk",
"bio": "API Gateway SME & Cloud Support Engineer @ AWS",
"linkedin": "https://www.linkedin.com/in/agawabhi/"
}
]
}
55 changes: 55 additions & 0 deletions lambda-function-connect-rds-iam/lambda_function.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# Ruby code here.

require 'aws-sdk-rds'
require 'json'
require 'mysql2'

def lambda_handler(event:, context:)
endpoint = 'mysqldb.123456789012.us-east-1.rds.amazonaws.com'
port = '3306'
user = 'DatabaseUser'
region = 'us-east-1'
db_name = 'DatabaseName'

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
32 changes: 30 additions & 2 deletions lambda-function-connect-rds-iam/snippet-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "Integration",
"services": ["lambda", "rds"],
"tags": [],
"languages": ["Javascript"],
"languages": ["Go", "Ruby", "Javascript"],
"introBox": {
"headline": "How it works",
"text": [
Expand All @@ -20,6 +20,28 @@
{
"title": "Runtimes",
"codeTabs": [
{
"id": "Go",
"title": "Copy the code into your Lambda function",
"description": "Follow the AWS Lambda instructions for packaging with dependencies and uploading in .zip format. This snippet uses the go mysql driver",
"snippets": [
{
"snippetPath": "main.go",
"language": "go"
}
]
},
{
"id": "Ruby",
"title": "Copy the code into your Lambda function",
"description": "Follow the AWS Lambda instructions for packaging with ruby dependencies with Ruby gem and bundler. This snippet uses the mysql2 gem so needs to use lambda containers",
"snippets": [
{
"snippetPath": "lambda_function.rb",
"language": "ruby"
}
]
},
{
"id": "Javascript",
"title": "Copy the code into your Lambda function",
Expand All @@ -35,12 +57,18 @@
}
],
"authors": [
{
"headline": "Presented by Abhishek Agawane",
"name": "Abhishek Agawane",
"image": "https://drive.google.com/file/d/1E-5koDaKEaMUtOctX32I9TLwfh3kgpAq/view?usp=drivesdk",
"bio": "API Gateway SME & Cloud Support Engineer @ AWS",
"linkedin": "https://www.linkedin.com/in/agawabhi/"
},
{
"headline": "JavaScript (Node) example by Jon Loinaz",
"name": "Jon Loinaz",
"image": "/assets/images/resources/contributors/jloinaz.jpg",
"bio": " Solutions Architect @ AWS.",
"twitter": "",
"linkedin": "jonloinaz"
}
]
Expand Down