Skip to content

Commit 63ab7df

Browse files
Self-review on stage 5
1 parent 03e9b9d commit 63ab7df

9 files changed

Lines changed: 294 additions & 35 deletions

File tree

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@
2727
"@aws-sdk/client-cloudwatch": "^3.588.0",
2828
"@aws-sdk/client-cloudwatch-logs": "^3.588.0",
2929
"@aws-sdk/client-cognito-identity-provider": "^3.588.0",
30+
"@aws-sdk/client-dynamodb": "^3.588.0",
3031
"@aws-sdk/client-ecr": "^3.588.0",
3132
"@aws-sdk/client-eventbridge": "^3.588.0",
3233
"@aws-sdk/client-iam": "^3.588.0",
34+
"@aws-sdk/client-iot": "^3.588.0",
35+
"@aws-sdk/client-iot-data-plane": "^3.588.0",
36+
"@aws-sdk/client-kinesis": "^3.588.0",
3337
"@aws-sdk/client-lambda": "^3.588.0",
3438
"@aws-sdk/client-s3": "^3.588.0",
39+
"@aws-sdk/client-sns": "^3.588.0",
40+
"@aws-sdk/client-sqs": "^3.588.0",
3541
"@aws-sdk/client-ssm": "^3.588.0",
3642
"@aws-sdk/client-sts": "^3.588.0",
43+
"@aws-sdk/lib-dynamodb": "^3.588.0",
3744
"@aws-sdk/credential-providers": "^3.588.0",
3845
"@serverless/utils": "^6.13.1",
3946
"ajv": "^8.12.0",

test/utils/dynamodb.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
'use strict';
22

33
const awsRequest = require('@serverless/test/aws-request');
4-
const DDBDocumentClient = require('aws-sdk').DynamoDB.DocumentClient;
4+
5+
// Support for both AWS SDK v2 and v3
6+
const getDynamoDBClient = () => {
7+
if (process.env.SLS_AWS_SDK_V3 === 'true') {
8+
// AWS SDK v3 - using DynamoDBDocumentClient from lib-dynamodb
9+
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb');
10+
const { DynamoDBDocumentClient, PutCommand } = require('@aws-sdk/lib-dynamodb');
11+
12+
const client = new DynamoDBClient({ region: 'us-east-1' });
13+
const docClient = DynamoDBDocumentClient.from(client);
14+
15+
return {
16+
put: (params) => docClient.send(new PutCommand(params)),
17+
};
18+
} else {
19+
// AWS SDK v2
20+
const DDBDocumentClient = require('aws-sdk').DynamoDB.DocumentClient;
21+
return {
22+
put: (params) => awsRequest(DDBDocumentClient, 'put', params),
23+
};
24+
}
25+
};
26+
27+
const dynamodb = getDynamoDBClient();
528

629
async function putDynamoDbItem(tableName, item) {
730
const params = {
831
TableName: tableName,
932
Item: item,
1033
};
1134

12-
return awsRequest(DDBDocumentClient, 'put', params);
35+
return dynamodb.put(params);
1336
}
1437

1538
module.exports = {

test/utils/event-bridge.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,59 @@
11
'use strict';
22

33
const awsRequest = require('@serverless/test/aws-request');
4-
const EventBridgeService = require('aws-sdk').EventBridge;
4+
5+
// Support for both AWS SDK v2 and v3
6+
const getEventBridgeClient = () => {
7+
if (process.env.SLS_AWS_SDK_V3 === 'true') {
8+
// AWS SDK v3
9+
const { EventBridgeClient } = require('@aws-sdk/client-eventbridge');
10+
const {
11+
CreateEventBusCommand,
12+
DeleteEventBusCommand,
13+
DescribeEventBusCommand,
14+
PutEventsCommand
15+
} = require('@aws-sdk/client-eventbridge');
16+
17+
const client = new EventBridgeClient({ region: 'us-east-1' });
18+
19+
return {
20+
createEventBus: (params) => client.send(new CreateEventBusCommand(params)),
21+
deleteEventBus: (params) => client.send(new DeleteEventBusCommand(params)),
22+
describeEventBus: (params) => client.send(new DescribeEventBusCommand(params)),
23+
putEvents: (params) => client.send(new PutEventsCommand(params)),
24+
};
25+
} else {
26+
// AWS SDK v2
27+
const EventBridgeService = require('aws-sdk').EventBridge;
28+
return {
29+
createEventBus: (params) => awsRequest(EventBridgeService, 'createEventBus', params),
30+
deleteEventBus: (params) => awsRequest(EventBridgeService, 'deleteEventBus', params),
31+
describeEventBus: (params) => awsRequest(EventBridgeService, 'describeEventBus', params),
32+
putEvents: (params) => awsRequest(EventBridgeService, 'putEvents', params),
33+
};
34+
}
35+
};
36+
37+
const eventBridge = getEventBridgeClient();
538

639
async function createEventBus(name) {
7-
return awsRequest(EventBridgeService, 'createEventBus', { Name: name });
40+
return eventBridge.createEventBus({ Name: name });
841
}
942

1043
async function deleteEventBus(name) {
11-
return awsRequest(EventBridgeService, 'deleteEventBus', { Name: name });
44+
return eventBridge.deleteEventBus({ Name: name });
1245
}
1346

1447
async function describeEventBus(name) {
15-
return awsRequest(EventBridgeService, 'describeEventBus', { Name: name });
48+
return eventBridge.describeEventBus({ Name: name });
1649
}
1750

1851
async function putEvents(EventBusName, Entries) {
1952
Entries.map((entry) => (entry.EventBusName = EventBusName));
2053
const params = {
2154
Entries,
2255
};
23-
return awsRequest(EventBridgeService, 'putEvents', params);
56+
return eventBridge.putEvents(params);
2457
}
2558

2659
module.exports = {

test/utils/iot.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
11
'use strict';
22

33
const awsRequest = require('@serverless/test/aws-request');
4-
const IotService = require('aws-sdk').Iot;
5-
const IotDataService = require('aws-sdk').IotData;
4+
5+
// Support for both AWS SDK v2 and v3
6+
const getIoTClients = () => {
7+
if (process.env.SLS_AWS_SDK_V3 === 'true') {
8+
// AWS SDK v3 - dual service pattern (IoT + IoTDataPlane)
9+
const { IoTClient, DescribeEndpointCommand } = require('@aws-sdk/client-iot');
10+
const { IoTDataPlaneClient, PublishCommand } = require('@aws-sdk/client-iot-data-plane');
11+
12+
const iotClient = new IoTClient({ region: 'us-east-1' });
13+
14+
return {
15+
iot: {
16+
describeEndpoint: (params) => iotClient.send(new DescribeEndpointCommand(params)),
17+
},
18+
createIoTDataClient: (endpoint) => {
19+
const iotDataClient = new IoTDataPlaneClient({
20+
region: 'us-east-1',
21+
endpoint: `https://${endpoint}`
22+
});
23+
return {
24+
publish: (params) => iotDataClient.send(new PublishCommand(params)),
25+
};
26+
}
27+
};
28+
} else {
29+
// AWS SDK v2
30+
const IotService = require('aws-sdk').Iot;
31+
const IotDataService = require('aws-sdk').IotData;
32+
return {
33+
iot: {
34+
describeEndpoint: (params) => awsRequest(IotService, 'describeEndpoint', params),
35+
},
36+
createIoTDataClient: (endpoint) => ({
37+
publish: (params) => awsRequest({ client: IotDataService, params: { endpoint } }, 'publish', params),
38+
})
39+
};
40+
}
41+
};
42+
43+
const { iot, createIoTDataClient } = getIoTClients();
644

745
async function resolveIotEndpoint() {
8-
return awsRequest(IotService, 'describeEndpoint', { endpointType: 'iot:Data-ATS' }).then(
46+
return iot.describeEndpoint({ endpointType: 'iot:Data-ATS' }).then(
947
(data) => {
1048
return data.endpointAddress;
1149
}
@@ -19,7 +57,8 @@ async function publishIotData(topic, message) {
1957
payload: Buffer.from(message),
2058
};
2159

22-
return awsRequest({ client: IotDataService, params: { endpoint } }, 'publish', params);
60+
const iotDataClient = createIoTDataClient(endpoint);
61+
return iotDataClient.publish(params);
2362
});
2463
}
2564

test/utils/kinesis.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,48 @@
11
'use strict';
22

33
const awsRequest = require('@serverless/test/aws-request');
4-
const KinesisService = require('aws-sdk').Kinesis;
4+
5+
// Support for both AWS SDK v2 and v3
6+
const getKinesisClient = () => {
7+
if (process.env.SLS_AWS_SDK_V3 === 'true') {
8+
// AWS SDK v3
9+
const { KinesisClient } = require('@aws-sdk/client-kinesis');
10+
const {
11+
CreateStreamCommand,
12+
DeleteStreamCommand,
13+
DescribeStreamCommand,
14+
PutRecordCommand
15+
} = require('@aws-sdk/client-kinesis');
16+
17+
const client = new KinesisClient({ region: 'us-east-1' });
18+
19+
return {
20+
createStream: (params) => client.send(new CreateStreamCommand(params)),
21+
deleteStream: (params) => client.send(new DeleteStreamCommand(params)),
22+
describeStream: (params) => client.send(new DescribeStreamCommand(params)),
23+
putRecord: (params) => client.send(new PutRecordCommand(params)),
24+
};
25+
} else {
26+
// AWS SDK v2
27+
const KinesisService = require('aws-sdk').Kinesis;
28+
return {
29+
createStream: (params) => awsRequest(KinesisService, 'createStream', params),
30+
deleteStream: (params) => awsRequest(KinesisService, 'deleteStream', params),
31+
describeStream: (params) => awsRequest(KinesisService, 'describeStream', params),
32+
putRecord: (params) => awsRequest(KinesisService, 'putRecord', params),
33+
};
34+
}
35+
};
36+
37+
const kinesis = getKinesisClient();
538

639
async function waitForKinesisStream(streamName) {
740
const params = {
841
StreamName: streamName,
942
};
1043
return new Promise((resolve) => {
1144
const interval = setInterval(() => {
12-
awsRequest(KinesisService, 'describeStream', params).then((data) => {
45+
kinesis.describeStream(params).then((data) => {
1346
const status = data.StreamDescription.StreamStatus;
1447
if (status === 'ACTIVE') {
1548
clearInterval(interval);
@@ -27,7 +60,7 @@ async function createKinesisStream(streamName) {
2760
StreamName: streamName,
2861
};
2962

30-
return awsRequest(KinesisService, 'createStream', params).then(() =>
63+
return kinesis.createStream(params).then(() =>
3164
waitForKinesisStream(streamName)
3265
);
3366
}
@@ -37,7 +70,7 @@ async function deleteKinesisStream(streamName) {
3770
StreamName: streamName,
3871
};
3972

40-
return awsRequest(KinesisService, 'deleteStream', params);
73+
return kinesis.deleteStream(params);
4174
}
4275

4376
async function putKinesisRecord(streamName, message) {
@@ -47,7 +80,7 @@ async function putKinesisRecord(streamName, message) {
4780
PartitionKey: streamName, // test streams are single shards
4881
};
4982

50-
return awsRequest(KinesisService, 'putRecord', params);
83+
return kinesis.putRecord(params);
5184
}
5285

5386
module.exports = {

test/utils/misc.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
'use strict';
22

33
const awsRequest = require('@serverless/test/aws-request');
4-
const CloudWatchLogsService = require('aws-sdk').CloudWatchLogs;
54
const wait = require('timers-ext/promise/sleep');
65

6+
// Support for both AWS SDK v2 and v3
7+
const getCloudWatchLogsClient = () => {
8+
if (process.env.SLS_AWS_SDK_V3 === 'true') {
9+
// AWS SDK v3
10+
const { CloudWatchLogsClient } = require('@aws-sdk/client-cloudwatch-logs');
11+
const { FilterLogEventsCommand } = require('@aws-sdk/client-cloudwatch-logs');
12+
13+
const client = new CloudWatchLogsClient({ region: 'us-east-1' });
14+
15+
return {
16+
filterLogEvents: (params) => client.send(new FilterLogEventsCommand(params)),
17+
};
18+
} else {
19+
// AWS SDK v2
20+
const CloudWatchLogsService = require('aws-sdk').CloudWatchLogs;
21+
return {
22+
filterLogEvents: (params) => awsRequest(CloudWatchLogsService, 'filterLogEvents', params),
23+
};
24+
}
25+
};
26+
27+
const cloudWatchLogs = getCloudWatchLogsClient();
28+
729
const logger = console;
830

931
function replaceEnv(values) {
@@ -33,7 +55,7 @@ async function confirmCloudWatchLogs(logGroupName, trigger, options = {}) {
3355
const timeout = options.timeout || 3 * 60 * 1000;
3456
return trigger()
3557
.then(() => wait(1000))
36-
.then(() => awsRequest(CloudWatchLogsService, 'filterLogEvents', { logGroupName }))
58+
.then(() => cloudWatchLogs.filterLogEvents({ logGroupName }))
3759
.then(({ events }) => {
3860
if (events.length) {
3961
if (options.checkIsComplete) {

test/utils/sns.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,51 @@
11
'use strict';
22

33
const awsRequest = require('@serverless/test/aws-request');
4-
const SNSService = require('aws-sdk').SNS;
4+
5+
// Support for both AWS SDK v2 and v3
6+
const getSNSClient = () => {
7+
if (process.env.SLS_AWS_SDK_V3 === 'true') {
8+
// AWS SDK v3
9+
const { SNSClient } = require('@aws-sdk/client-sns');
10+
const {
11+
CreateTopicCommand,
12+
DeleteTopicCommand,
13+
ListTopicsCommand,
14+
PublishCommand
15+
} = require('@aws-sdk/client-sns');
16+
17+
const client = new SNSClient({ region: 'us-east-1' });
18+
19+
return {
20+
createTopic: (params) => client.send(new CreateTopicCommand(params)),
21+
deleteTopic: (params) => client.send(new DeleteTopicCommand(params)),
22+
listTopics: (params) => client.send(new ListTopicsCommand(params)),
23+
publish: (params) => client.send(new PublishCommand(params)),
24+
};
25+
} else {
26+
// AWS SDK v2
27+
const SNSService = require('aws-sdk').SNS;
28+
return {
29+
createTopic: (params) => awsRequest(SNSService, 'createTopic', params),
30+
deleteTopic: (params) => awsRequest(SNSService, 'deleteTopic', params),
31+
listTopics: (params) => awsRequest(SNSService, 'listTopics', params),
32+
publish: (params) => awsRequest(SNSService, 'publish', params),
33+
};
34+
}
35+
};
36+
37+
const sns = getSNSClient();
538

639
async function createSnsTopic(topicName) {
740
const params = {
841
Name: topicName,
942
};
1043

11-
return awsRequest(SNSService, 'createTopic', params);
44+
return sns.createTopic(params);
1245
}
1346

1447
async function resolveTopicArn(topicName, nextToken = null) {
15-
return awsRequest(SNSService, 'listTopics', { NextToken: nextToken }).then((data) => {
48+
return sns.listTopics({ NextToken: nextToken }).then((data) => {
1649
const targetTopic = data.Topics.find((topic) => RegExp(topicName, 'g').test(topic.TopicArn));
1750

1851
if (targetTopic) return targetTopic.TopicArn;
@@ -28,7 +61,7 @@ async function removeSnsTopic(topicName) {
2861
TopicArn: topicArn,
2962
};
3063

31-
return awsRequest(SNSService, 'deleteTopic', params);
64+
return sns.deleteTopic(params);
3265
});
3366
}
3467

@@ -42,7 +75,7 @@ async function publishSnsMessage(topicName, message, messageAttributes = null) {
4275
params.MessageAttributes = messageAttributes;
4376
}
4477

45-
return awsRequest(SNSService, 'publish', params);
78+
return sns.publish(params);
4679
});
4780
}
4881

0 commit comments

Comments
 (0)