-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
101 lines (86 loc) · 2.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import "dotenv/config";
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";
import { DynamoDBClient, PutCommand } from "@aws-sdk/client-dynamodb";
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";
import { SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs";
// Wrapper
import S3Client from "./services/s3";
// Create service clients
const lambda = new LambdaClient({ region: process.env.AWS_REGION });
const dynamodb = new DynamoDBClient({ region: process.env.AWS_REGION });
const sns = new SNSClient({ region: process.env.AWS_REGION });
const sqs = new SQSClient({ region: process.env.AWS_REGION });
const s3 = new S3Client();
// 1. Upload a file to S3
const uploadParams = {
Bucket: process.env.S3_BUCKET,
Key: process.env.S3_KEY,
Body: "Hello, world!",
};
const uploadParams2 = {
Bucket: process.env.S3_BUCKET_1,
Key: process.env.S3_KEY,
Body: "Hello, world!",
};
const uploadParams3 = {
Bucket: "my_bucket_2",
Key: process.env.S3_KEY,
Body: "Hello, world!",
};
s3.uploadFile(uploadParams)
.then((data) => {
console.log("Upload Success");
// 2. Invoke a Lambda function
const lambdaParams = {
FunctionName: process.env.LAMBDA_FUNCTION,
Payload: JSON.stringify({
key: process.env.S3_KEY,
bucket: process.env.S3_BUCKET,
}),
};
lambda
.send(new InvokeCommand(lambdaParams))
.then((data) => {
console.log("Lambda Success");
// 3. Write to DynamoDB
const dynamoParams = {
TableName: process.env.DYNAMODB_TABLE,
Item: {
id: { S: "123" },
data: { S: "Hello, world!" },
},
};
dynamodb
.send(new PutCommand(dynamoParams))
.then((data) => {
console.log("DynamoDB Write Success");
// 4. Publish to SNS
const snsParams = {
Message: "Hello, world!",
TopicArn: process.env.SNS_TOPIC_ARN,
};
sns
.send(new PublishCommand(snsParams))
.then((data) => {
console.log("SNS Publish Success");
// 5. Send a message to SQS
const sqsParams = {
MessageBody: "Hello, world!",
QueueUrl: process.env.SQS_QUEUE_URL,
};
sqs
.send(new SendMessageCommand(sqsParams))
.then((data) => {
console.log("SQS Send Message Success");
})
.catch((err) => console.log("SQS Error", err));
})
.catch((err) => console.log("SNS Error", err));
})
.catch((err) => console.log("DynamoDB Error", err));
})
.catch((err) => console.log("Lambda Error", err));
})
.catch((err) => console.log("S3 Upload Error", err));
s3.uploadFile(uploadParams2);
s3.uploadFile(uploadParams3);