From d722f0bccf637f987452ac91338d4a11ebc22b77 Mon Sep 17 00:00:00 2001 From: Abhishek Agawane Date: Mon, 10 Jun 2024 22:58:03 +0530 Subject: [PATCH 1/3] Added Ruby sample for RDS IAM connection --- .../lambda_function.rb | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lambda-function-connect-rds-iam/lambda_function.rb diff --git a/lambda-function-connect-rds-iam/lambda_function.rb b/lambda-function-connect-rds-iam/lambda_function.rb new file mode 100644 index 0000000..57ad728 --- /dev/null +++ b/lambda-function-connect-rds-iam/lambda_function.rb @@ -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 \ No newline at end of file From 27afdcb63ebebab88813d219cef18bd904d13cd3 Mon Sep 17 00:00:00 2001 From: Abhishek Agawane Date: Tue, 11 Jun 2024 00:36:10 +0530 Subject: [PATCH 2/3] Added snippet-data.json files for publishing over Serverlessland portal --- .../snippet-data1.json | 47 +++++++++++++++++++ .../snippet-data2.json | 47 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 lambda-function-connect-rds-iam/snippet-data1.json create mode 100644 lambda-function-connect-rds-iam/snippet-data2.json diff --git a/lambda-function-connect-rds-iam/snippet-data1.json b/lambda-function-connect-rds-iam/snippet-data1.json new file mode 100644 index 0000000..695caa6 --- /dev/null +++ b/lambda-function-connect-rds-iam/snippet-data1.json @@ -0,0 +1,47 @@ +{ + "title": "Connect to Amazon RDS from AWS Lambda", + "description": "This snippet demonstrates how to connect to an Amazon RDS database from an AWS Lambda function with IAM authentication. This snippet is valid for MariaDB/MySQL or Aurora for MySQL", + "type": "Integration", + "services": ["lambda", "rds"], + "tags": [], + "languages": ["Ruby"], + "introBox": { + "headline": "How it works", + "text": [ + "Amazon Relational Database Service (Amazon RDS) is a collection of managed services that makes it simple to set up, operate, and scale databases in the cloud. Choose from seven popular engines. Connect to an RDS database using IAM authentication." + ] + }, + "gitHub": { + "template": { + "repoURL": "https://github.com/aws-samples/serverless-snippets/tree/main/lambda-function-connect-rds-iam" + } + }, + "snippets": [ + { + "title": "Runtimes", + "codeTabs": [ + { + "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" + } + ] + } + ] + } + ], + "authors": [ + { + "headline": "Presented by Abhishek Agawane", + "name": "Abhishek Agawane", + "image": "https://drive.google.com/file/d/1E-5koDaKEaMUtOctX32I9TLwfh3kgpAq/view?usp=drivesdk", + "bio": "Abhishek is a Cloud Support Engineer at AWS who focuses on serverless and is passionate about Cloud", + "linkedin": "https://www.linkedin.com/in/agawabhi/" + } + ] +} + diff --git a/lambda-function-connect-rds-iam/snippet-data2.json b/lambda-function-connect-rds-iam/snippet-data2.json new file mode 100644 index 0000000..5d35e31 --- /dev/null +++ b/lambda-function-connect-rds-iam/snippet-data2.json @@ -0,0 +1,47 @@ +{ + "title": "Connect to Amazon RDS from AWS Lambda", + "description": "This snippet demonstrates how to connect to an Amazon RDS database from an AWS Lambda function with IAM authentication. This snippet is valid for MariaDB/MySQL or Aurora for MySQL", + "type": "Integration", + "services": ["lambda", "rds"], + "tags": [], + "languages": ["Go"], + "introBox": { + "headline": "How it works", + "text": [ + "Amazon Relational Database Service (Amazon RDS) is a collection of managed services that makes it simple to set up, operate, and scale databases in the cloud. Choose from seven popular engines. Connect to an RDS database using IAM authentication." + ] + }, + "gitHub": { + "template": { + "repoURL": "https://github.com/aws-samples/serverless-snippets/tree/main/lambda-function-connect-rds-iam" + } + }, + "snippets": [ + { + "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" + } + ] + } + ] + } + ], + "authors": [ + { + "headline": "Presented by Abhishek Agawane", + "name": "Abhishek Agawane", + "image": "https://drive.google.com/file/d/1E-5koDaKEaMUtOctX32I9TLwfh3kgpAq/view?usp=drivesdk", + "bio": "Abhishek is a Cloud Support Engineer at AWS who focuses on serverless and is passionate about Cloud", + "linkedin": "https://www.linkedin.com/in/agawabhi/" + } + ] +} + From 178db785d539a312b92b678979aa8443b37a6334 Mon Sep 17 00:00:00 2001 From: Abhishek Agawane Date: Tue, 11 Jun 2024 01:05:55 +0530 Subject: [PATCH 3/3] Added snippet-data.json file for publishing over Serverlessland portal --- integration-docdb-to-lambda/snippet-data.json | 79 +++++++++++++++++++ .../snippet-data.json | 32 +++++++- .../snippet-data1.json | 47 ----------- .../snippet-data2.json | 47 ----------- 4 files changed, 109 insertions(+), 96 deletions(-) create mode 100644 integration-docdb-to-lambda/snippet-data.json delete mode 100644 lambda-function-connect-rds-iam/snippet-data1.json delete mode 100644 lambda-function-connect-rds-iam/snippet-data2.json diff --git a/integration-docdb-to-lambda/snippet-data.json b/integration-docdb-to-lambda/snippet-data.json new file mode 100644 index 0000000..5232cba --- /dev/null +++ b/integration-docdb-to-lambda/snippet-data.json @@ -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/" + } + ] +} diff --git a/lambda-function-connect-rds-iam/snippet-data.json b/lambda-function-connect-rds-iam/snippet-data.json index cf24c9a..9453aa2 100644 --- a/lambda-function-connect-rds-iam/snippet-data.json +++ b/lambda-function-connect-rds-iam/snippet-data.json @@ -4,7 +4,7 @@ "type": "Integration", "services": ["lambda", "rds"], "tags": [], - "languages": ["Javascript"], + "languages": ["Go", "Ruby", "Javascript"], "introBox": { "headline": "How it works", "text": [ @@ -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", @@ -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" } ] diff --git a/lambda-function-connect-rds-iam/snippet-data1.json b/lambda-function-connect-rds-iam/snippet-data1.json deleted file mode 100644 index 695caa6..0000000 --- a/lambda-function-connect-rds-iam/snippet-data1.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "title": "Connect to Amazon RDS from AWS Lambda", - "description": "This snippet demonstrates how to connect to an Amazon RDS database from an AWS Lambda function with IAM authentication. This snippet is valid for MariaDB/MySQL or Aurora for MySQL", - "type": "Integration", - "services": ["lambda", "rds"], - "tags": [], - "languages": ["Ruby"], - "introBox": { - "headline": "How it works", - "text": [ - "Amazon Relational Database Service (Amazon RDS) is a collection of managed services that makes it simple to set up, operate, and scale databases in the cloud. Choose from seven popular engines. Connect to an RDS database using IAM authentication." - ] - }, - "gitHub": { - "template": { - "repoURL": "https://github.com/aws-samples/serverless-snippets/tree/main/lambda-function-connect-rds-iam" - } - }, - "snippets": [ - { - "title": "Runtimes", - "codeTabs": [ - { - "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" - } - ] - } - ] - } - ], - "authors": [ - { - "headline": "Presented by Abhishek Agawane", - "name": "Abhishek Agawane", - "image": "https://drive.google.com/file/d/1E-5koDaKEaMUtOctX32I9TLwfh3kgpAq/view?usp=drivesdk", - "bio": "Abhishek is a Cloud Support Engineer at AWS who focuses on serverless and is passionate about Cloud", - "linkedin": "https://www.linkedin.com/in/agawabhi/" - } - ] -} - diff --git a/lambda-function-connect-rds-iam/snippet-data2.json b/lambda-function-connect-rds-iam/snippet-data2.json deleted file mode 100644 index 5d35e31..0000000 --- a/lambda-function-connect-rds-iam/snippet-data2.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "title": "Connect to Amazon RDS from AWS Lambda", - "description": "This snippet demonstrates how to connect to an Amazon RDS database from an AWS Lambda function with IAM authentication. This snippet is valid for MariaDB/MySQL or Aurora for MySQL", - "type": "Integration", - "services": ["lambda", "rds"], - "tags": [], - "languages": ["Go"], - "introBox": { - "headline": "How it works", - "text": [ - "Amazon Relational Database Service (Amazon RDS) is a collection of managed services that makes it simple to set up, operate, and scale databases in the cloud. Choose from seven popular engines. Connect to an RDS database using IAM authentication." - ] - }, - "gitHub": { - "template": { - "repoURL": "https://github.com/aws-samples/serverless-snippets/tree/main/lambda-function-connect-rds-iam" - } - }, - "snippets": [ - { - "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" - } - ] - } - ] - } - ], - "authors": [ - { - "headline": "Presented by Abhishek Agawane", - "name": "Abhishek Agawane", - "image": "https://drive.google.com/file/d/1E-5koDaKEaMUtOctX32I9TLwfh3kgpAq/view?usp=drivesdk", - "bio": "Abhishek is a Cloud Support Engineer at AWS who focuses on serverless and is passionate about Cloud", - "linkedin": "https://www.linkedin.com/in/agawabhi/" - } - ] -} -