Skip to content

Commit e9e94d3

Browse files
authored
Merge pull request #2847 from indranil-banerjee-aws/main
PR to include a new pattern documentdb-lambda-java-sam
2 parents 8619362 + 190ef8f commit e9e94d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+5050
-0
lines changed

documentdb-lambda-java-sam/DocumentDBAndMongoClientEC2.yaml

Lines changed: 946 additions & 0 deletions
Large diffs are not rendered by default.

documentdb-lambda-java-sam/README.md

Lines changed: 195 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
# Connect to Mongo Shell
4+
mongosh --tls --tlsCAFile /home/ec2-user/mongoshell/global-bundle.pem --username DOCDB_CLUSTER_ADMIN_USER --password DOCDB_CLUSTER_PASSWORD --host DOCDB_CLUSTER_ENDPOINT --port 27017
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
# DocumentDB Connection Details
4+
DOCDB_ENDPOINT="DOCDB_CLUSTER_ENDPOINT"
5+
DOCDB_PORT="27017"
6+
DOCDB_USERNAME="DOCDB_CLUSTER_ADMIN_USER"
7+
DOCDB_PASSWORD="DOCDB_CLUSTER_PASSWORD"
8+
TLS_CERT_FILE="/home/ec2-user/mongoshell/global-bundle.pem" # Path to your TLS certificate file if TLS is enabled
9+
10+
# Database and Collection Names
11+
DATABASE_NAME="DOCDB_DATABASE"
12+
COLLECTION_NAME="DOCDB_COLLECTION"
13+
14+
# Connect using Mongo Shell and create database and collection
15+
mongosh --tls --tlsCAFile ${TLS_CERT_FILE} --username ${DOCDB_USERNAME} --password ${DOCDB_PASSWORD} --host ${DOCDB_ENDPOINT} --port ${DOCDB_PORT} --file mongodbcolcreate.js
16+
17+
18+
# Connect to Mongo Shell
19+
# mongosh --tls --tlsCAFile /home/ec2-user/mongoshell/global-bundle.pem --username DOCDB_CLUSTER_ADMIN_USER --password DOCDB_CLUSTER_PASSWORD --host DOCDB_CLUSTER_ENDPOINT --port 27017
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
{
2+
"title": "Amazon DocumentDB streams consumer",
3+
"description": "Consume Amazon DocumentDB stream records with AWS Lambda.",
4+
"language": "Java",
5+
"level": "200",
6+
"framework": "AWS SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern demonstrates consuming Amazon DocumentDB stream records with an AWS Lambda function.",
11+
"The CloudFormation template provided in this pattern creates an Amazon DocumentDB Cluster and database with a collection. It also enables change streams on the database and collection, and creates an Amazon EC2 instances for accessing the Amazon DocumentDB cluster.",
12+
"For detailed deployment instructions instructions see the README.md"
13+
]
14+
},
15+
"gitHub": {
16+
"template": {
17+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/documentdb-lambda-java-sam",
18+
"templateURL": "serverless-patterns/documentdb-lambda-java-sam/documentdb_streams_consumer_dynamo_sam",
19+
"projectFolder": "documentdb_streams_consumer_dynamo_sam",
20+
"templateFile": "template_original.yaml"
21+
}
22+
},
23+
"resources": {
24+
"bullets": [
25+
{
26+
"text": "Process Amazon DocumentDB events with Lambda",
27+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/with-documentdb.html"
28+
},
29+
{
30+
"text": "Using AWS Lambda with change streams",
31+
"link": "https://docs.aws.amazon.com/documentdb/latest/developerguide/using-lambda.html"
32+
},
33+
{
34+
"text": "Amazon DocumentDB quick start using AWS CloudFormation",
35+
"link": "https://docs.aws.amazon.com/documentdb/latest/developerguide/quick_start_cfn.html"
36+
},
37+
{
38+
"text": "AWS CloudFormation DocumentDB cluster reference",
39+
"link": "https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-docdb-dbcluster.html"
40+
}
41+
]
42+
},
43+
"deploy": {
44+
"text": [
45+
"sam deploy --guided"
46+
]
47+
},
48+
"testing": {
49+
"text": [
50+
"See the GitHub repo for detailed testing instructions."
51+
]
52+
},
53+
"cleanup": {
54+
"text": [
55+
"Delete the template: <code>sam delete</code>."
56+
]
57+
},
58+
"authors": [
59+
{
60+
"name": "Indranil Banerjee",
61+
"bio": "AWS - Senior Solutions Architect",
62+
"image": "https://media.licdn.com/dms/image/v2/C5603AQEL3BG6JZca6A/profile-displayphoto-shrink_400_400/profile-displayphoto-shrink_400_400/0/1653972622784?e=1762992000&v=beta&t=a9gnmtxyWBhfEfqDF3HiPHWDoc4KZjG2sdNsIakcJXw",
63+
"linkedin": "indranil-banerjee-b00a261"
64+
},
65+
{
66+
"name": "Arghya Banerjee",
67+
"bio": "AWS - Senior Solutions Architect",
68+
"image": "https://media.licdn.com/dms/image/v2/C5603AQHFFKivT-1iKA/profile-displayphoto-shrink_800_800/profile-displayphoto-shrink_800_800/0/1516305772138?e=1762992000&v=beta&t=dKgoxEfgZd3M5XW-GEJ9Ss4I5ka8-C7iEIy8Sb_PTOQ",
69+
"linkedin": "arghya-b-6130b57"
70+
},
71+
{
72+
"name": "Kunal Ghosh",
73+
"bio": "AWS - Sr SA, Strategic Accounts",
74+
"image": "https://media.licdn.com/dms/image/v2/C5603AQHrj7mHd7Z1hg/profile-displayphoto-shrink_800_800/profile-displayphoto-shrink_800_800/0/1516355158121?e=1762992000&v=beta&t=8h0cpHLR6v4-e3BJ7n9Wd_OHL2rRDa8F_74rTUnu8Js",
75+
"linkedin": "kunal-ghosh-6583058"
76+
},
77+
{
78+
"name": "Angelo Spagnolo",
79+
"bio": "AWS - Sr Technical Account Manager",
80+
"image": "https://media.licdn.com/dms/image/v2/D5603AQFQj2a90KFFZQ/profile-displayphoto-shrink_400_400/profile-displayphoto-shrink_400_400/0/1692659294509?e=1762992000&v=beta&t=gEfdRLVzNPXGZ9-5EZKA358NBf4F7VBcVthyWSrzjrs",
81+
"linkedin": "aspagnolo"
82+
},
83+
{
84+
"name": "Vinayak Datar",
85+
"bio": "AWS - Sr. Customer Solutions Manager",
86+
"image": "https://media.licdn.com/dms/image/v2/C5103AQFAa6JcLNknkA/profile-displayphoto-shrink_400_400/profile-displayphoto-shrink_400_400/0/1548189034877?e=1762992000&v=beta&t=o2lDqU3V4ivFjt00rEDYd2yjGr55TszcG0J9hGuRM_w",
87+
"linkedin": "vinayakdatar"
88+
},
89+
{
90+
"name": "Greg Medard",
91+
"bio": "AWS - Solutions Architect",
92+
"image": "https://media.licdn.com/dms/image/v2/C4E03AQGveSDnRH9aCg/profile-displayphoto-shrink_400_400/profile-displayphoto-shrink_400_400/0/1565226836063?e=1762992000&v=beta&t=Jkir-cq-T1EU8MTqY7PPPxreVo2_zI4FXaXXrdTdpfQ",
93+
"linkedin": "gregorymedard"
94+
}
95+
],
96+
"patternArch": {
97+
"icon1": {
98+
"x": 20,
99+
"y": 50,
100+
"service": "documentdb",
101+
"label": "Amazon DocumentDB"
102+
},
103+
"icon2": {
104+
"x": 50,
105+
"y": 50,
106+
"service": "lambda",
107+
"label": "AWS Lambda"
108+
},
109+
"line1": {
110+
"from": "icon1",
111+
"to": "icon2",
112+
"label": "Stream event"
113+
},
114+
"icon3": {
115+
"x": 80,
116+
"y": 50,
117+
"service": "dynamodb",
118+
"label": "Amazon DynamoDB"
119+
},
120+
"line2": {
121+
"from": "icon2",
122+
"to": "icon3"
123+
}
124+
}
125+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
node_modules
2+
npm-debug.log
3+
package-lock.json
4+
package
5+
*out.yml
6+
out.json
7+
bucket-name.txt
8+
target
9+
build
10+
.gradle
11+
*.zip
12+
bin
13+
obj
14+
Gemfile.lock
15+
lib
16+
__pycache__
17+
*.pyc
18+
.classpath
19+
.factorypath
20+
.project
21+
.settings/*
22+
.aws
23+
.sam
24+
.aws-sam
25+
*.toml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/target/
2+
.classpath
3+
.project
4+
org.eclipse.jdt.core.prefs
5+
org.eclipse.core.resources.prefs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
3+
org.eclipse.jdt.core.compiler.compliance=11
4+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
5+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
6+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
7+
org.eclipse.jdt.core.compiler.release=disabled
8+
org.eclipse.jdt.core.compiler.source=11
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.amazonaws.services.lambda.samples.events.documentdb.streams</groupId>
4+
<artifactId>DocumentDBStreamsConsumer</artifactId>
5+
<version>1.0</version>
6+
<packaging>jar</packaging>
7+
<name>A sample Lambda DocumentDB Streams consumer</name>
8+
<properties>
9+
<maven.compiler.source>11</maven.compiler.source>
10+
<maven.compiler.target>11</maven.compiler.target>
11+
</properties>
12+
<dependencies>
13+
<dependency>
14+
<groupId>com.amazonaws</groupId>
15+
<artifactId>aws-lambda-java-core</artifactId>
16+
<version>1.2.1</version>
17+
</dependency>
18+
<dependency>
19+
<groupId>com.amazonaws</groupId>
20+
<artifactId>aws-lambda-java-events</artifactId>
21+
<version>3.11.0</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.google.code.gson</groupId>
25+
<artifactId>gson</artifactId>
26+
<version>2.10.1</version>
27+
</dependency>
28+
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-dynamodb -->
29+
<dependency>
30+
<groupId>com.amazonaws</groupId>
31+
<artifactId>aws-java-sdk-dynamodb</artifactId>
32+
<version>1.12.445</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.junit.jupiter</groupId>
36+
<artifactId>junit-jupiter-api</artifactId>
37+
<version>5.6.0</version>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.junit.jupiter</groupId>
42+
<artifactId>junit-jupiter-engine</artifactId>
43+
<version>5.6.0</version>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.mockito</groupId>
48+
<artifactId>mockito-core</artifactId>
49+
<version>3.7.7</version>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.mockito</groupId>
54+
<artifactId>mockito-junit-jupiter</artifactId>
55+
<version>3.8.0</version>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>software.amazon.awssdk</groupId>
60+
<artifactId>sqs</artifactId>
61+
<version>2.20.62</version>
62+
</dependency>
63+
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
64+
</dependencies>
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-shade-plugin</artifactId>
70+
<version>3.2.4</version>
71+
<configuration></configuration>
72+
<executions>
73+
<execution>
74+
<phase>package</phase>
75+
<goals>
76+
<goal>shade</goal>
77+
</goals>
78+
</execution>
79+
</executions>
80+
</plugin>
81+
</plugins>
82+
</build>
83+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.amazonaws.services.lambda.samples.events.documentdbstreams;
2+
3+
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
4+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
5+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
6+
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
7+
import com.amazonaws.services.dynamodbv2.document.Item;
8+
import com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
9+
import com.amazonaws.services.dynamodbv2.document.Table;
10+
import com.amazonaws.services.lambda.runtime.LambdaLogger;
11+
import com.amazonaws.services.lambda.samples.events.documentdbstreams.models.EventEvent;
12+
13+
public class DynamoDBUpdater {
14+
15+
String dynamoDBTableName;
16+
AmazonDynamoDB client;
17+
DynamoDB dynamoDB;
18+
Table dynamoTable;
19+
20+
21+
public DynamoDBUpdater(String dynamoDBTableName) {
22+
super();
23+
if (null == dynamoDBTableName) {
24+
this.dynamoDBTableName = "SQS_LAMBDA_DYNAMO_TABLE";
25+
} else {
26+
this.dynamoDBTableName = dynamoDBTableName;
27+
}
28+
String AWS_SAM_LOCAL = System.getenv("AWS_SAM_LOCAL");
29+
if (null == AWS_SAM_LOCAL) {
30+
this.client = AmazonDynamoDBClientBuilder.standard().build();
31+
} else {
32+
this.client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new EndpointConfiguration("http://127.0.0.1:8000", "")).build();
33+
this.dynamoDBTableName = "SQS_LAMBDA_DYNAMO_TABLE";
34+
}
35+
this.dynamoDB = new DynamoDB(client);
36+
this.dynamoTable = dynamoDB.getTable(this.dynamoDBTableName);
37+
}
38+
39+
public PutItemOutcome insertIntoDynamoDB(EventEvent eventEvent, String EventSource, String EventSourceARN, LambdaLogger logger) {
40+
logger.log("Now inserting a row in DynamoDB for messageID = " + eventEvent.getFullDocument().get_id());
41+
Item item = new Item();
42+
item.withPrimaryKey("MessageID", eventEvent.getFullDocument().get_id());
43+
item.withString("EventSource", EventSource);
44+
item.withString("EventSourceARN", EventSourceARN);
45+
item.withString("EventIDData", eventEvent.get_id().get_data());
46+
item.withString("OperationType", eventEvent.getOperationType());
47+
item.withString("DocumentDBDatabase", eventEvent.getNs().getDb());
48+
item.withString("DocumentDBCollection", eventEvent.getNs().getColl());
49+
item.withString("DocumentKeyID", eventEvent.getDocumentKey().get_id());
50+
item.withLong("ClusterTimeTimeStampT = ", eventEvent.getClusterTime().get$timestamp().getT());
51+
item.withLong("ClusterTimeTimeStampI = ", eventEvent.getClusterTime().get$timestamp().getI());
52+
item.withString("CustomerID = ", eventEvent.getFullDocument().get_id());
53+
item.withString("CustomerFirstname = ", eventEvent.getFullDocument().getFirstname());
54+
item.withString("CustomerLastname = ", eventEvent.getFullDocument().getLastname());
55+
item.withString("CustomerStreet = ", eventEvent.getFullDocument().getStreet());
56+
item.withString("CustomerCity = ", eventEvent.getFullDocument().getCity());
57+
item.withString("CustomerCounty = ", eventEvent.getFullDocument().getCounty());
58+
item.withString("CustomerState = ", eventEvent.getFullDocument().getState());
59+
item.withString("CustomerZip = ", eventEvent.getFullDocument().getZip());
60+
item.withString("CustomerHomePhone = ", eventEvent.getFullDocument().getHomePhone());
61+
item.withString("CustomerCellPhone = ", eventEvent.getFullDocument().getCellPhone());
62+
item.withString("CustomerEmail = ", eventEvent.getFullDocument().getEmail());
63+
item.withString("CustomerCompany = ", eventEvent.getFullDocument().getCompany());
64+
item.withString("CustomerWebsite = ", eventEvent.getFullDocument().getWebsite());
65+
logger.log("Now done inserting a row in DynamoDB for messageID = " + eventEvent.getFullDocument().get_id());
66+
return dynamoTable.putItem(item);
67+
}
68+
}

0 commit comments

Comments
 (0)