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/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 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" } ]