Skip to content

Commit

Permalink
fix: Upgrade to Node.js 18 (#83)
Browse files Browse the repository at this point in the history
The Lambda were already upgraded by a previous automatic projen upgrade. But this makes the bundled size small again by using AWS SDK v3. Node.js 18 no longer comes with v2, but only with v3.

BREAKING CHANGE: CDK 2.85.0 and above is required
  • Loading branch information
kichik authored Oct 31, 2023
1 parent bae6862 commit 14835b2
Show file tree
Hide file tree
Showing 19 changed files with 9,772 additions and 8,172 deletions.
16 changes: 10 additions & 6 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ const project = new awscdk.AwsCdkConstructLibrary({
author: 'Amir Szekely',
authorAddress: 'amir@cloudsnorkel.com',
stability: Stability.EXPERIMENTAL,
cdkVersion: '2.0.0',
cdkVersion: '2.85.0', // for no more deprecated nodejs 14 in integration test
defaultReleaseBranch: 'main',
name: '@cloudsnorkel/cdk-rds-sanitized-snapshots',
repositoryUrl: 'https://github.com/CloudSnorkel/cdk-rds-sanitized-snapshots.git',
license: 'Apache-2.0',
description: 'CDK construct to periodically take snapshots of RDS databases, sanitize them, and share with selected accounts.',
devDeps: [
'esbuild', // for faster NodejsFunction bundling
'aws-sdk',
'@aws-sdk/types',
'@aws-sdk/client-resource-groups-tagging-api',
'@aws-sdk/client-rds',
'@aws-sdk/client-sfn',
'@types/aws-lambda',
],
deps: [
Expand Down
11 changes: 6 additions & 5 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions src/delete-old.lambda.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable import/no-extraneous-dependencies */
import * as AWS from 'aws-sdk';
import { DeleteDBClusterSnapshotCommand, RDSClient } from '@aws-sdk/client-rds';
import { GetResourcesCommand, ResourceGroupsTaggingAPIClient } from '@aws-sdk/client-resource-groups-tagging-api';

const tagging = new AWS.ResourceGroupsTaggingAPI();
const rds = new AWS.RDS();
const tagging = new ResourceGroupsTaggingAPIClient();
const rds = new RDSClient();

interface Input {
tags: { Key: string; Value: string }[];
Expand All @@ -11,12 +12,12 @@ interface Input {
}

exports.handler = async function (input: Input) {
const snapshotsResponse = await tagging.getResources({
const snapshotsResponse = await tagging.send(new GetResourcesCommand({
TagFilters: input.tags.map(f => {
return { Key: f.Key, Values: [f.Value] };
}),
ResourceTypeFilters: [input.resourceType],
}).promise();
}));
if (!snapshotsResponse.ResourceTagMappingList) {
console.error('No snapshots found');
}
Expand All @@ -37,8 +38,8 @@ exports.handler = async function (input: Input) {

for (const snapshot of toDelete) {
console.log(`Deleting old snapshot: ${snapshot}`);
await rds.deleteDBClusterSnapshot({
await rds.send(new DeleteDBClusterSnapshotCommand({
DBClusterSnapshotIdentifier: snapshot,
}).promise();
}));
}
};
};
10 changes: 5 additions & 5 deletions src/parameters.lambda.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable import/no-extraneous-dependencies */
import * as crypto from 'crypto';
import * as AWS from 'aws-sdk';
import { DescribeDBClustersCommand, DescribeDBInstancesCommand, RDSClient } from '@aws-sdk/client-rds';

const rds = new AWS.RDS();
const rds = new RDSClient();

interface Input {
executionId: string;
Expand Down Expand Up @@ -70,7 +70,7 @@ exports.handler = async function (input: Input): Promise<Parameters> {
let instanceClass: string;

if (input.isCluster) {
const origDb = await rds.describeDBClusters({ DBClusterIdentifier: input.databaseIdentifier }).promise();
const origDb = await rds.send(new DescribeDBClustersCommand({ DBClusterIdentifier: input.databaseIdentifier }));
if (!origDb.DBClusters || origDb.DBClusters.length != 1) {
throw new Error(`Unable to find ${input.databaseIdentifier}`);
}
Expand All @@ -80,7 +80,7 @@ exports.handler = async function (input: Input): Promise<Parameters> {
throw new Error(`Database missing some required parameters: ${JSON.stringify(cluster)}`);
}

const origInstances = await rds.describeDBInstances({ DBInstanceIdentifier: cluster.DBClusterMembers[0].DBInstanceIdentifier }).promise();
const origInstances = await rds.send(new DescribeDBInstancesCommand({ DBInstanceIdentifier: cluster.DBClusterMembers[0].DBInstanceIdentifier }));
if (!origInstances.DBInstances || origInstances.DBInstances.length < 1) {
throw new Error(`Unable to find instances for ${input.databaseIdentifier}`);
}
Expand All @@ -96,7 +96,7 @@ exports.handler = async function (input: Input): Promise<Parameters> {
kmsKeyId = cluster.KmsKeyId;
instanceClass = instance.DBInstanceClass;
} else {
const origDb = await rds.describeDBInstances({ DBInstanceIdentifier: input.databaseIdentifier }).promise();
const origDb = await rds.send(new DescribeDBInstancesCommand({ DBInstanceIdentifier: input.databaseIdentifier }));
if (!origDb.DBInstances || origDb.DBInstances.length != 1) {
throw new Error(`Unable to find ${input.databaseIdentifier}`);
}
Expand Down
25 changes: 16 additions & 9 deletions src/test-wait.lambda.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/* eslint-disable import/no-extraneous-dependencies */
import * as AWS from 'aws-sdk';

const sfn = new AWS.StepFunctions();
const rds = new AWS.RDS();
import {
DeleteDBClusterSnapshotCommand,
DeleteDBSnapshotCommand,
DescribeDBClusterSnapshotsCommand,
DescribeDBSnapshotsCommand,
RDSClient,
} from '@aws-sdk/client-rds';
import { DescribeExecutionCommand, SFNClient } from '@aws-sdk/client-sfn';

const sfn = new SFNClient();
const rds = new RDSClient();

interface Input {
RequestType: 'Create' | 'Update' | 'Delete';
Expand All @@ -17,7 +24,7 @@ exports.handler = async function (input: Input): Promise<Result> {
console.log(input.RequestType, input.PhysicalResourceId);

if (input.RequestType == 'Create' || input.RequestType == 'Update') {
const exec = await sfn.describeExecution({ executionArn: input.PhysicalResourceId }).promise();
const exec = await sfn.send(new DescribeExecutionCommand({ executionArn: input.PhysicalResourceId }));
if (exec.status == 'ABORTED' || exec.status == 'FAILED' || exec.status == 'TIMED_OUT') {
throw new Error(`Step function failed with: ${exec.status}`);
}
Expand All @@ -31,17 +38,17 @@ exports.handler = async function (input: Input): Promise<Result> {
const output = JSON.parse(exec.output);

if (output.isCluster) {
const snapshots = await rds.describeDBClusterSnapshots({ DBClusterSnapshotIdentifier: output.targetSnapshotId }).promise();
const snapshots = await rds.send(new DescribeDBClusterSnapshotsCommand({ DBClusterSnapshotIdentifier: output.targetSnapshotId }));
if (!snapshots.DBClusterSnapshots || snapshots.DBClusterSnapshots.length != 1) {
throw new Error(`Target cluster snapshot ${output.targetSnapshotId} does not exist`);
}
await rds.deleteDBClusterSnapshot({ DBClusterSnapshotIdentifier: output.targetSnapshotId }).promise();
await rds.send(new DeleteDBClusterSnapshotCommand({ DBClusterSnapshotIdentifier: output.targetSnapshotId }));
} else {
const snapshots = await rds.describeDBSnapshots({ DBSnapshotIdentifier: output.targetSnapshotId }).promise();
const snapshots = await rds.send(new DescribeDBSnapshotsCommand({ DBSnapshotIdentifier: output.targetSnapshotId }));
if (!snapshots.DBSnapshots || snapshots.DBSnapshots.length != 1) {
throw new Error(`Target instance snapshot ${output.targetSnapshotId} does not exist`);
}
await rds.deleteDBSnapshot({ DBSnapshotIdentifier: output.targetSnapshotId }).promise();
await rds.send(new DeleteDBSnapshotCommand({ DBSnapshotIdentifier: output.targetSnapshotId }));
}

return { IsComplete: true };
Expand Down
10 changes: 5 additions & 5 deletions src/test.lambda.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable import/no-extraneous-dependencies */
import * as AWS from 'aws-sdk';
import { SFNClient, StartExecutionCommand } from '@aws-sdk/client-sfn';

const sfn = new AWS.StepFunctions();
const sfn = new SFNClient();

interface Input {
RequestType: 'Create' | 'Update' | 'Delete';
Expand All @@ -17,9 +17,9 @@ interface Result {

exports.handler = async function (input: Input): Promise<Result> {
if (input.RequestType == 'Create' || input.RequestType == 'Update') {
const exec = await sfn.startExecution({ stateMachineArn: input.ResourceProperties.StepFunctionArn }).promise();
return { PhysicalResourceId: exec.executionArn };
const exec = await sfn.send(new StartExecutionCommand({ stateMachineArn: input.ResourceProperties.StepFunctionArn }));
return { PhysicalResourceId: exec.executionArn! };
}

return { PhysicalResourceId: input.PhysicalResourceId };
};
};
26 changes: 16 additions & 10 deletions src/wait.lambda.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* eslint-disable import/no-extraneous-dependencies */
import * as AWS from 'aws-sdk';
import {
DescribeDBClustersCommand,
DescribeDBClusterSnapshotsCommand,
DescribeDBInstancesCommand,
DescribeDBSnapshotsCommand,
RDSClient,
} from '@aws-sdk/client-rds';

const rds = new AWS.RDS();
const rds = new RDSClient();

interface Input {
resourceType: 'snapshot' | 'cluster' | 'instance';
Expand Down Expand Up @@ -39,10 +45,10 @@ exports.handler = async function (input: Input) {
let status: string;
if (input.isCluster) {
// wait for cluster snapshot
const snapshots = await rds.describeDBClusterSnapshots({
const snapshots = await rds.send(new DescribeDBClusterSnapshotsCommand({
DBClusterIdentifier: input.databaseIdentifier,
DBClusterSnapshotIdentifier: input.snapshotIdentifier,
}).promise();
}));

console.log(snapshots);

Expand All @@ -53,10 +59,10 @@ exports.handler = async function (input: Input) {
status = snapshots.DBClusterSnapshots[0].Status ?? '';
} else {
// wait for instance snapshot
const snapshots = await rds.describeDBSnapshots({
const snapshots = await rds.send(new DescribeDBSnapshotsCommand({
DBInstanceIdentifier: input.databaseIdentifier,
DBSnapshotIdentifier: input.snapshotIdentifier,
}).promise();
}));

console.log(snapshots);

Expand All @@ -74,9 +80,9 @@ exports.handler = async function (input: Input) {
checkStatus(status, input.snapshotIdentifier);
} else if (input.resourceType == 'cluster') {
// wait for db
const dbs = await rds.describeDBClusters({
const dbs = await rds.send(new DescribeDBClustersCommand({
DBClusterIdentifier: input.databaseIdentifier,
}).promise();
}));

console.log(dbs);

Expand All @@ -92,9 +98,9 @@ exports.handler = async function (input: Input) {
checkStatus(status, input.databaseIdentifier);
} else if (input.resourceType == 'instance') {
// wait for db
const instances = await rds.describeDBInstances({
const instances = await rds.send(new DescribeDBInstancesCommand({
DBInstanceIdentifier: input.databaseIdentifier,
}).promise();
}));

console.log(instances);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "15.0.0",
"version": "32.0.0",
"files": {
"1033c183df53fe80a9c43605c9e648af74bf88fd094db5b39bff286023de0be1": {
"af64ec05b3dfb7846a43d7aebf55e6ab83ac754192e5c5e8341de0d3e5f4314c": {
"source": {
"path": "RDS-Sanitized-Snapshotter-RDS.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "1033c183df53fe80a9c43605c9e648af74bf88fd094db5b39bff286023de0be1.json",
"objectKey": "af64ec05b3dfb7846a43d7aebf55e6ab83ac754192e5c5e8341de0d3e5f4314c.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Loading

0 comments on commit 14835b2

Please sign in to comment.