Skip to content

Commit b820eb1

Browse files
authored
Merge pull request #187 from agawanea/main
Added Ruby sample for RDS IAM connection
2 parents 1ccd329 + 178db78 commit b820eb1

File tree

3 files changed

+164
-2
lines changed

3 files changed

+164
-2
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"title": "Integration of Amazon DocumentDB with AWS Lambda",
3+
"description": "This snippet demonstrates how to integrate Amazon DocumentDB with an AWS Lambda function.",
4+
"type": "Integration",
5+
"services": ["lambda", "DocumentDB"],
6+
"tags": [],
7+
"languages": ["Python", "Go", "Ruby", "Javascript"],
8+
"introBox": {
9+
"headline": "How it works",
10+
"text": [
11+
"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"
12+
]
13+
},
14+
"gitHub": {
15+
"template": {
16+
"repoURL": "https://github.com/aws-samples/serverless-snippets/tree/main/integration-docdb-to-lambda"
17+
}
18+
},
19+
"snippets": [
20+
{
21+
"title": "Runtimes",
22+
"codeTabs": [
23+
{
24+
"id": "Python",
25+
"title": "Copy the code into your Lambda function",
26+
"description": "Follow the AWS Lambda instructions for packaging with dependencies and uploading in .zip format.",
27+
"snippets": [
28+
{
29+
"snippetPath": "example.py",
30+
"language": "py"
31+
}
32+
]
33+
},
34+
{
35+
"id": "Go",
36+
"title": "Copy the code into your Lambda function",
37+
"description": "Follow the AWS Lambda instructions for packaging with dependencies and uploading in .zip format.",
38+
"snippets": [
39+
{
40+
"snippetPath": "main.go",
41+
"language": "go"
42+
}
43+
]
44+
},
45+
{
46+
"id": "Ruby",
47+
"title": "Copy the code into your Lambda function",
48+
"description": "Follow the AWS Lambda instructions for packaging with ruby dependencies with Ruby gem and bundler.",
49+
"snippets": [
50+
{
51+
"snippetPath": "example.rb",
52+
"language": "ruby"
53+
}
54+
]
55+
},
56+
{
57+
"id": "Javascript",
58+
"title": "Copy the code into your Lambda function",
59+
"description": "Follow the AWS Lambda instructions for packaging with dependencies and uploading in .zip format. This snippet uses the mysql2/promise library.",
60+
"snippets": [
61+
{
62+
"snippetPath": "example.js",
63+
"language": "js"
64+
}
65+
]
66+
}
67+
]
68+
}
69+
],
70+
"authors": [
71+
{
72+
"headline": "Presented by Abhishek Agawane",
73+
"name": "Abhishek Agawane",
74+
"image": "https://drive.google.com/file/d/1E-5koDaKEaMUtOctX32I9TLwfh3kgpAq/view?usp=drivesdk",
75+
"bio": "API Gateway SME & Cloud Support Engineer @ AWS",
76+
"linkedin": "https://www.linkedin.com/in/agawabhi/"
77+
}
78+
]
79+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
# Ruby code here.
4+
5+
require 'aws-sdk-rds'
6+
require 'json'
7+
require 'mysql2'
8+
9+
def lambda_handler(event:, context:)
10+
endpoint = 'mysqldb.123456789012.us-east-1.rds.amazonaws.com'
11+
port = '3306'
12+
user = 'DatabaseUser'
13+
region = 'us-east-1'
14+
db_name = 'DatabaseName'
15+
16+
credentials = Aws::Credentials.new(
17+
ENV['AWS_ACCESS_KEY_ID'],
18+
ENV['AWS_SECRET_ACCESS_KEY'],
19+
ENV['AWS_SESSION_TOKEN']
20+
)
21+
rds_client = Aws::RDS::AuthTokenGenerator.new(
22+
region: region,
23+
credentials: credentials
24+
)
25+
26+
token = rds_client.auth_token(
27+
endpoint: endpoint+ ':' + port,
28+
user_name: user,
29+
region: region
30+
)
31+
32+
begin
33+
conn = Mysql2::Client.new(
34+
host: endpoint,
35+
username: user,
36+
password: token,
37+
port: port,
38+
database: db_name,
39+
sslca: '/var/task/global-bundle.pem',
40+
sslverify: true,
41+
enable_cleartext_plugin: true
42+
)
43+
a = 3
44+
b = 2
45+
result = conn.query("SELECT #{a} + #{b} AS sum").first['sum']
46+
puts result
47+
conn.close
48+
{
49+
statusCode: 200,
50+
body: result.to_json
51+
}
52+
rescue => e
53+
puts "Database connection failed due to #{e}"
54+
end
55+
end

lambda-function-connect-rds-iam/snippet-data.json

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "Integration",
55
"services": ["lambda", "rds"],
66
"tags": [],
7-
"languages": ["Javascript"],
7+
"languages": ["Go", "Ruby", "Javascript"],
88
"introBox": {
99
"headline": "How it works",
1010
"text": [
@@ -20,6 +20,28 @@
2020
{
2121
"title": "Runtimes",
2222
"codeTabs": [
23+
{
24+
"id": "Go",
25+
"title": "Copy the code into your Lambda function",
26+
"description": "Follow the AWS Lambda instructions for packaging with dependencies and uploading in .zip format. This snippet uses the go mysql driver",
27+
"snippets": [
28+
{
29+
"snippetPath": "main.go",
30+
"language": "go"
31+
}
32+
]
33+
},
34+
{
35+
"id": "Ruby",
36+
"title": "Copy the code into your Lambda function",
37+
"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",
38+
"snippets": [
39+
{
40+
"snippetPath": "lambda_function.rb",
41+
"language": "ruby"
42+
}
43+
]
44+
},
2345
{
2446
"id": "Javascript",
2547
"title": "Copy the code into your Lambda function",
@@ -35,12 +57,18 @@
3557
}
3658
],
3759
"authors": [
60+
{
61+
"headline": "Presented by Abhishek Agawane",
62+
"name": "Abhishek Agawane",
63+
"image": "https://drive.google.com/file/d/1E-5koDaKEaMUtOctX32I9TLwfh3kgpAq/view?usp=drivesdk",
64+
"bio": "API Gateway SME & Cloud Support Engineer @ AWS",
65+
"linkedin": "https://www.linkedin.com/in/agawabhi/"
66+
},
3867
{
3968
"headline": "JavaScript (Node) example by Jon Loinaz",
4069
"name": "Jon Loinaz",
4170
"image": "/assets/images/resources/contributors/jloinaz.jpg",
4271
"bio": " Solutions Architect @ AWS.",
43-
"twitter": "",
4472
"linkedin": "jonloinaz"
4573
}
4674
]

0 commit comments

Comments
 (0)