Skip to content

Commit

Permalink
fix(transliterator): use sdk v3 and adaptive throttling (#1442)
Browse files Browse the repository at this point in the history
Migrates the transliterator to SDKv3. With SDKv3, we can replace the
custom retry mechanism with adaptive throttling that's built-in to the
sdk.

Migration included CodeArtifact login and removal of `fs-extra` in favor
of native node features.

Also upgrades the Node.js version to node10 and the Amazon Linux version
to AL2023.

----

*By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license*

---------

Signed-off-by: github-actions <[email protected]>
Co-authored-by: github-actions <[email protected]>
  • Loading branch information
mrgrain and github-actions authored Sep 10, 2024
1 parent 9224bc5 commit 27f404a
Show file tree
Hide file tree
Showing 20 changed files with 752 additions and 330 deletions.
24 changes: 24 additions & 0 deletions .projen/deps.json

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

6 changes: 3 additions & 3 deletions .projen/tasks.json

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

10 changes: 8 additions & 2 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ const project = new CdklabsConstructLibrary({
'@types/tough-cookie',
'@types/uuid',
cdkCli,
'aws-embedded-metrics',
'dotenv',
'@aws-sdk/client-codeartifact',
'@aws-sdk/client-s3',
'@smithy/types',
'@smithy/util-stream',
'async-sema',
'aws-embedded-metrics',
'aws-sdk-client-mock',
'aws-sdk-client-mock-jest',
'aws-sdk-mock',
'aws-sdk',
'aws-xray-sdk-core',
'case',
'cdk-dia',
'chalk',
'dotenv',
'esbuild',
'feed',
'fs-extra',
Expand Down
6 changes: 6 additions & 0 deletions package.json

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

3 changes: 2 additions & 1 deletion projenrc/bundle-javascript-for-ecs.exec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { basename, dirname, posix } from 'path';
import { Instance as Chalk } from 'chalk';
import * as esbuild from 'esbuild';
import { ECS_TASK_NODE_VERSION } from './magic-ecs';

const chalk = new Chalk({
level: process.env.NO_COLOR ? 0 : 1,
Expand All @@ -24,7 +25,7 @@ async function main(args: string[]) {
entryPoints: [entrypoint],
outfile,
sourcemap: true,
target: 'node18',
target: `node${ECS_TASK_NODE_VERSION}`,
platform: 'node',
metafile: true,

Expand Down
26 changes: 15 additions & 11 deletions projenrc/magic-ecs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { basename, dirname, join, relative } from 'path';
import Case from 'case';
import * as Case from 'case';
import * as glob from 'glob';
import { SourceCode } from 'projen';
import { TypeScriptProject } from 'projen/lib/typescript';

export const ECS_TASK_NODE_VERSION = '20';
const BUNDLE_DIR_ENV = 'BUNDLE_DIR';
const ECS_TASK_MEMORY_LIMIT_DEFINITIONS: { [key: string]: number } = {
'backend/transliterator/transliterator.ecstask.ts': 8192,
Expand Down Expand Up @@ -208,22 +209,25 @@ function newEcsTask(project: TypeScriptProject, entrypoint: string) {

const df = new SourceCode(project, dockerfile);
df.line(`# ${df.marker}`);
// Based off amazonlinux:2 for... reasons. (Do not change!)
df.line('FROM public.ecr.aws/amazonlinux/amazonlinux:2');
// Based off amazonlinux:2023 for... reasons. Slim node images don't work here.
df.line('FROM public.ecr.aws/amazonlinux/amazonlinux:2023');
df.line();
// Install node the regular way...
df.line(
'RUN yum install https://rpm.nodesource.com/pub_16.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm -y \\'
`RUN yum install https://rpm.nodesource.com/pub_${ECS_TASK_NODE_VERSION}.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm -y \\`
);
df.line(' && yum install nodejs -y \\');
// @see https://github.com/nodesource/distributions/blob/e42782301931c357cec2a09e1246d7849e084345/scripts/rpm/setup_22.x#L75-L76
df.line(
' && yum install nodejs -y --setopt=nodesource-nodejs.module_hotfixes=1 \\'
' --setopt=nodesource-nodejs.gpgkey=https://rpm.nodesource.com/gpgkey/ns-operations-public.key \\'
);
df.line(' && yum update -y \\');
df.line(' && yum upgrade -y \\');
df.line(' && yum install -y git lsof nodejs \\');
df.line(' --setopt=nodesource-nodejs.module_hotfixes=1 \\');
df.line(' && yum update -y \\');
df.line(' && yum upgrade -y \\');
df.line(' && yum install -y git lsof nodejs \\');
// Clean up the yum cache in the interest of image size
df.line(' && yum clean all \\');
df.line(' && rm -rf /var/cache/yum');
df.line(' && yum clean all \\');
df.line(' && rm -rf /var/cache/yum');
df.line();
df.line('COPY . /bundle');
df.line();
Expand Down Expand Up @@ -290,7 +294,7 @@ export function discoverEcsTasks(project: TypeScriptProject) {
...entrypoints.map((file) =>
file.replace('ecstask.ts', 'ecs-entrypoint.ts')
),
'--target="node18"',
`--target="node${ECS_TASK_NODE_VERSION}"`,
'--platform="node"',
`--outbase="${project.srcdir}"`,
`--outdir="${project.libdir}"`,
Expand Down
20 changes: 10 additions & 10 deletions src/__tests__/__snapshots__/construct-hub.test.ts.snap

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

51 changes: 22 additions & 29 deletions src/__tests__/backend/shared/code-artifact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import type * as child_process from 'child_process';
import { randomBytes } from 'crypto';

import { EventEmitter } from 'stream';
import * as AWS from 'aws-sdk';
import * as AWSMock from 'aws-sdk-mock';
import {
CodeartifactClient,
GetAuthorizationTokenCommand,
} from '@aws-sdk/client-codeartifact';
import { mockClient } from 'aws-sdk-client-mock';
import 'aws-sdk-client-mock-jest';

import { logInWithCodeArtifact } from '../../../backend/shared/code-artifact.lambda-shared';

jest.mock('child_process');

beforeEach((done) => {
AWSMock.setSDKInstance(AWS);
done();
});
const codeArtifactMock = mockClient(CodeartifactClient);

afterEach((done) => {
AWSMock.restore();
done();
beforeEach(() => {
codeArtifactMock.reset();
});

test('logInWithCodeArtifact', async () => {
Expand All @@ -28,23 +28,9 @@ test('logInWithCodeArtifact', async () => {
const apiEndpoint = 'https://fake.codeartifact.api.endpoint';

const authorizationToken = randomBytes(64).toString('base64');
AWSMock.mock(
'CodeArtifact',
'getAuthorizationToken',
(
param: AWS.CodeArtifact.GetAuthorizationTokenRequest,
cb: Response<AWS.CodeArtifact.GetAuthorizationTokenResult>
) => {
try {
expect(param.domain).toBe(domain);
expect(param.domainOwner).toBe(domainOwner);
expect(param.durationSeconds).toBe(0);
} catch (e: any) {
return cb(e);
}
cb(null, { authorizationToken });
}
);
codeArtifactMock.on(GetAuthorizationTokenCommand).resolves({
authorizationToken,
});

// eslint-disable-next-line @typescript-eslint/no-require-imports
const mockSpawn = require('child_process').spawn as jest.MockedFunction<
Expand Down Expand Up @@ -83,12 +69,19 @@ test('logInWithCodeArtifact', async () => {
// THEN
await expect(
logInWithCodeArtifact({ endpoint, domain, domainOwner, apiEndpoint })
).resolves.not.toThrowError();
).resolves.not.toThrow();

expect(Array.from(configToSet)).toEqual([]); // All config was set as expected.
expect(codeArtifactMock).toHaveReceivedCommandWith(
GetAuthorizationTokenCommand,
{
domain,
domainOwner,
durationSeconds: 0,
}
);
});

type Response<T> = (err: AWS.AWSError | null, data?: T) => void;

class MockChildProcess
extends EventEmitter
implements child_process.ChildProcess
Expand Down
Loading

0 comments on commit 27f404a

Please sign in to comment.