Skip to content

Commit

Permalink
Merge pull request #783 from PRX/dtr-scaler-node-upgrade
Browse files Browse the repository at this point in the history
Upgrade Router scaler function to Node.js 20 and AWS SDK v3
  • Loading branch information
farski authored Sep 17, 2024
2 parents 9333f6c + b2162f6 commit 33c2891
Show file tree
Hide file tree
Showing 3 changed files with 908 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@aws-sdk/client-cloudwatch": "3.x",
"@aws-sdk/client-codebuild": "3.x",
"@aws-sdk/client-codepipeline": "3.x",
"@aws-sdk/client-dynamodb": "3.x",
"@aws-sdk/client-ec2": "3.x",
"@aws-sdk/client-ecs": "3.x",
"@aws-sdk/client-eventbridge": "3.x",
Expand Down
38 changes: 27 additions & 11 deletions spire/templates/apps/dovetail-router.yml
Original file line number Diff line number Diff line change
Expand Up @@ -925,10 +925,23 @@ Resources:
- aws.s3
Handler: index.handler
InlineCode: !Sub |
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const ddb = new AWS.DynamoDB({ region: '${AWS::Region}' });
const ecs = new AWS.ECS({ region: '${AWS::Region}' });
// As of 2024-09, there's no way to use `import` as part of inline
// Lambda function code, so we have to use `require`.
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');
const {
DynamoDBClient,
BatchGetItemCommand,
PutItemCommand,
} = require('@aws-sdk/client-dynamodb');
const {
ECSClient,
DescribeServicesCommand,
UpdateServiceCommand,
} = require('@aws-sdk/client-ecs');
const s3 = new S3Client();
const ddb = new DynamoDBClient({ region: '${AWS::Region}' });
const ecs = new ECSClient({ region: '${AWS::Region}' });
const TABLE_NAME = process.env.DYNAMODB_TABLE_NAME;
const CLUSTER_NAME = process.env.ECS_CLUSTER_NAME;
Expand All @@ -940,15 +953,18 @@ Resources:
};
exports.handler = async (event) => {
console.log(JSON.stringify(event));
const Bucket = event.detail.bucket.name;
const Key = event.detail.object.key;
if (!SCALE_TO[Key]) {
return;
}
// read rss guids
const result = await s3.getObject({ Bucket, Key }).promise();
const matches = result.Body.toString('utf-8').matchAll(/>([^<]+)<\/guid>/g);
const result = await s3.send(new GetObjectCommand({ Bucket, Key }));
const resultBody = await result.Body.transformToString();
const matches = resultBody.matchAll(/>([^<]+)<\/guid>/g);
const guids = [];
for (const match of matches) {
guids.push(match[1]);
Expand All @@ -966,7 +982,7 @@ Resources:
const found = {};
for (const Keys of chunkedKeys) {
const params = { RequestItems: { [TABLE_NAME]: { Keys } } };
const data = await ddb.batchGetItem(params).promise();
const data = await ddb.send(new BatchGetItemCommand(params));
data.Responses[TABLE_NAME].forEach((r) => (found[r.guid.S] = true));
}
const newGuids = guids.filter((g) => !found[g]);
Expand All @@ -975,7 +991,7 @@ Resources:
// scale ECS service
if (newGuids.length > 0) {
const params = { cluster: CLUSTER_NAME, services: [SERVICE_NAME] };
const res = await ecs.describeServices(params).promise();
const res = await ecs.send(new DescribeServicesCommand(params));
const count = res.services[0].desiredCount;
const desiredCount = SCALE_TO[Key];
if (count < desiredCount) {
Expand All @@ -985,7 +1001,7 @@ Resources:
service: SERVICE_NAME,
desiredCount,
};
await ecs.updateService(updateParams).promise();
await ecs.send(new UpdateServiceCommand(updateParams));
} else {
console.info(`Already at ${!count}`);
}
Expand All @@ -994,7 +1010,7 @@ Resources:
// set new guids in DDB
for (const guid of newGuids) {
const params = { TableName: TABLE_NAME, Item: { guid: { S: guid } } };
await ddb.putItem(params).promise();
await ddb.send(new PutItemCommand(params));
}
};
MemorySize: 512
Expand Down Expand Up @@ -1022,7 +1038,7 @@ Resources:
Effect: Allow
Resource: !Sub arn:aws:ecs:${AWS::Region}:${AWS::AccountId}:service/${EcsClusterName}/${EcsService.Name}
Version: "2012-10-17"
Runtime: nodejs16.x
Runtime: nodejs20.x
Tags:
prx:meta:tagging-version: "2021-04-07"
prx:cloudformation:stack-name: !Ref AWS::StackName
Expand Down
Loading

0 comments on commit 33c2891

Please sign in to comment.